handle deleting individual messages

This commit is contained in:
tibbi 2020-04-05 13:38:33 +02:00
parent 2a319f9550
commit 3cf6ce2e37
4 changed files with 58 additions and 7 deletions

View File

@ -78,17 +78,17 @@ class MessagesAdapter(
ConfirmationDialog(activity, question) {
ensureBackgroundThread {
deleteMessages()
deleteThreads()
}
}
}
private fun deleteMessages() {
private fun deleteThreads() {
if (selectedKeys.isEmpty()) {
return
}
val messagesToRemove = messages.filter { selectedKeys.contains(it.id) } as java.util.ArrayList<Message>
val messagesToRemove = messages.filter { selectedKeys.contains(it.id) } as ArrayList<Message>
val positions = getSelectedItemPositions()
messagesToRemove.forEach {
activity.deleteThread(it.thread)

View File

@ -4,21 +4,22 @@ import android.view.Menu
import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.views.FastScroller
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
import com.simplemobiletools.smsmessenger.extensions.deleteMessage
import com.simplemobiletools.smsmessenger.helpers.THREAD_DATE_TIME
import com.simplemobiletools.smsmessenger.helpers.THREAD_RECEIVED_MESSAGE
import com.simplemobiletools.smsmessenger.helpers.THREAD_SENT_MESSAGE
import com.simplemobiletools.smsmessenger.helpers.THREAD_SENT_MESSAGE_ERROR
import com.simplemobiletools.smsmessenger.models.Message
import com.simplemobiletools.smsmessenger.models.ThreadDateTime
import com.simplemobiletools.smsmessenger.models.ThreadError
import com.simplemobiletools.smsmessenger.models.ThreadItem
import com.simplemobiletools.smsmessenger.models.*
import kotlinx.android.synthetic.main.item_received_message.view.*
import kotlinx.android.synthetic.main.item_thread_date_time.view.*
import org.greenrobot.eventbus.EventBus
class ThreadAdapter(
activity: SimpleActivity, var messages: ArrayList<ThreadItem>,
@ -42,6 +43,7 @@ class ThreadAdapter(
when (id) {
R.id.cab_select_all -> selectAll()
R.id.cab_delete -> askConfirmDelete()
}
}
@ -91,6 +93,42 @@ class ThreadAdapter(
}
}
private fun askConfirmDelete() {
val itemsCnt = selectedKeys.size
val items = resources.getQuantityString(R.plurals.delete_messages, itemsCnt, itemsCnt)
val baseString = R.string.delete_conversations_confirmation
val question = String.format(resources.getString(baseString), items)
ConfirmationDialog(activity, question) {
ensureBackgroundThread {
deleteMessages()
}
}
}
private fun deleteMessages() {
if (selectedKeys.isEmpty()) {
return
}
val messagesToRemove = messages.filter { selectedKeys.contains((it as? Message)?.id ?: 0) } as ArrayList<ThreadItem>
val positions = getSelectedItemPositions()
messagesToRemove.forEach {
activity.deleteMessage((it as Message).id)
}
messages.removeAll(messagesToRemove)
activity.runOnUiThread {
if (messages.filter { it is Message }.isEmpty()) {
activity.finish()
} else {
removeSelectedItems(positions)
}
EventBus.getDefault().post(Events.RefreshMessages())
}
}
private fun isThreadDateTime(position: Int) = messages.getOrNull(position) is ThreadDateTime
private fun setupView(view: View, message: Message) {

View File

@ -182,6 +182,13 @@ fun Context.deleteThread(id: Int) {
contentResolver.delete(uri, selection, selectionArgs)
}
fun Context.deleteMessage(id: Int) {
val uri = Telephony.Sms.CONTENT_URI
val selection = "${Telephony.Sms._ID} = ?"
val selectionArgs = arrayOf(id.toString())
contentResolver.delete(uri, selection, selectionArgs)
}
fun Context.markSMSRead(id: Int) {
val uri = Telephony.Sms.CONTENT_URI
val contentValues = ContentValues().apply {

View File

@ -17,4 +17,10 @@
<item quantity="one">%d conversation</item>
<item quantity="other">%d conversations</item>
</plurals>
<!-- Are you sure you want to delete 5 messages? -->
<plurals name="delete_messages">
<item quantity="one">%d message</item>
<item quantity="other">%d messages</item>
</plurals>
</resources>