cleaning up getMessages, threadId cannot be null
This commit is contained in:
parent
21dd815f2a
commit
a4adf59323
|
@ -43,7 +43,7 @@ import kotlin.collections.ArrayList
|
||||||
|
|
||||||
val Context.config: Config get() = Config.newInstance(applicationContext)
|
val Context.config: Config get() = Config.newInstance(applicationContext)
|
||||||
|
|
||||||
fun Context.getMessages(threadId: Int? = null): ArrayList<Message> {
|
fun Context.getMessages(threadId: Int): ArrayList<Message> {
|
||||||
val uri = Sms.CONTENT_URI
|
val uri = Sms.CONTENT_URI
|
||||||
val projection = arrayOf(
|
val projection = arrayOf(
|
||||||
Sms._ID,
|
Sms._ID,
|
||||||
|
@ -55,17 +55,8 @@ fun Context.getMessages(threadId: Int? = null): ArrayList<Message> {
|
||||||
Sms.THREAD_ID
|
Sms.THREAD_ID
|
||||||
)
|
)
|
||||||
|
|
||||||
val selection = if (threadId == null) {
|
val selection = "${Sms.THREAD_ID} = ?"
|
||||||
"1 == 1) GROUP BY (${Sms.THREAD_ID}"
|
val selectionArgs = arrayOf(threadId.toString())
|
||||||
} else {
|
|
||||||
"${Sms.THREAD_ID} = ?"
|
|
||||||
}
|
|
||||||
|
|
||||||
val selectionArgs = if (threadId == null) {
|
|
||||||
null
|
|
||||||
} else {
|
|
||||||
arrayOf(threadId.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
var messages = ArrayList<Message>()
|
var messages = ArrayList<Message>()
|
||||||
queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
|
queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
|
||||||
|
@ -93,10 +84,6 @@ fun Context.getMessages(threadId: Int? = null): ArrayList<Message> {
|
||||||
messages = messages.filter { it.participants.isNotEmpty() }
|
messages = messages.filter { it.participants.isNotEmpty() }
|
||||||
.sortedByDescending { it.date }.toMutableList() as ArrayList<Message>
|
.sortedByDescending { it.date }.toMutableList() as ArrayList<Message>
|
||||||
|
|
||||||
if (threadId == null) {
|
|
||||||
messages = messages.distinctBy { it.thread }.toMutableList() as ArrayList<Message>
|
|
||||||
}
|
|
||||||
|
|
||||||
return messages
|
return messages
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue