Refactor extensions/Context.kt slightly

No behaviour change.

- Simplify insertOrUpdateConversation(), since the previous code was long and
  hard to follow.

- Inline constants into selection queries instead of using '?' arguments.
This commit is contained in:
Tom Levy 2024-03-23 17:25:54 +00:00
parent 1c7376c0f2
commit dd2cc7f0e2

View file

@ -278,11 +278,11 @@ fun Context.getConversations(threadId: Long? = null, privateContacts: ArrayList<
projection += Threads.ARCHIVED
}
var selection = "${Threads.MESSAGE_COUNT} > ?"
var selectionArgs = arrayOf("0")
var selection = "${Threads.MESSAGE_COUNT} > 0"
var selectionArgs = arrayOf<String>()
if (threadId != null) {
selection += " AND ${Threads._ID} = ?"
selectionArgs = arrayOf("0", threadId.toString())
selectionArgs += threadId.toString()
}
val sortOrder = "${Threads.DATE} DESC"
@ -355,11 +355,10 @@ private fun Context.queryCursorUnsafe(
fun Context.getConversationIds(): List<Long> {
val uri = Uri.parse("${Threads.CONTENT_URI}?simple=true")
val projection = arrayOf(Threads._ID)
val selection = "${Threads.MESSAGE_COUNT} > ?"
val selectionArgs = arrayOf("0")
val selection = "${Threads.MESSAGE_COUNT} > 0"
val sortOrder = "${Threads.DATE} ASC"
val conversationIds = mutableListOf<Long>()
queryCursor(uri, projection, selection, selectionArgs, sortOrder, true) { cursor ->
queryCursor(uri, projection, selection, null, sortOrder, true) { cursor ->
val id = cursor.getLongValue(Threads._ID)
conversationIds.add(id)
}
@ -1058,16 +1057,9 @@ fun Context.insertOrUpdateConversation(
conversation: Conversation,
cachedConv: Conversation? = conversationsDB.getConversationWithThreadId(conversation.threadId)
) {
val updatedConv = if (cachedConv != null) {
val usesCustomTitle = cachedConv.usesCustomTitle
val title = if (usesCustomTitle) {
cachedConv.title
} else {
conversation.title
}
conversation.copy(title = title, usesCustomTitle = usesCustomTitle)
} else {
conversation
var updatedConv = conversation
if (cachedConv != null && cachedConv.usesCustomTitle) {
updatedConv = updatedConv.copy(title = cachedConv.title, usesCustomTitle = cachedConv.usesCustomTitle)
}
conversationsDB.insertOrUpdate(updatedConv)
}