allow deleting threads from the main screen

This commit is contained in:
tibbi 2020-04-05 13:25:24 +02:00
parent 560935b61e
commit 2a319f9550
3 changed files with 56 additions and 2 deletions

View File

@ -6,13 +6,18 @@ import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.extensions.formatDateOrTime
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.deleteThread
import com.simplemobiletools.smsmessenger.models.Events
import com.simplemobiletools.smsmessenger.models.Message
import kotlinx.android.synthetic.main.item_message.view.*
import org.greenrobot.eventbus.EventBus
class MessagesAdapter(
activity: SimpleActivity, var messages: ArrayList<Message>,
@ -36,6 +41,7 @@ class MessagesAdapter(
when (id) {
R.id.cab_select_all -> selectAll()
R.id.cab_delete -> askConfirmDelete()
}
}
@ -63,9 +69,41 @@ class MessagesAdapter(
override fun getItemCount() = messages.size
private fun getItemWithKey(key: Int): Message? = messages.firstOrNull { it.id == key }
private fun askConfirmDelete() {
val itemsCnt = selectedKeys.size
val items = resources.getQuantityString(R.plurals.delete_conversations, itemsCnt, itemsCnt)
private fun getSelectedItems() = messages.filter { selectedKeys.contains(it.id) } as ArrayList<Message>
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.id) } as java.util.ArrayList<Message>
val positions = getSelectedItemPositions()
messagesToRemove.forEach {
activity.deleteThread(it.thread)
}
messages.removeAll(messagesToRemove)
activity.runOnUiThread {
if (messagesToRemove.isEmpty()) {
EventBus.getDefault().post(Events.RefreshMessages())
finishActMode()
} else {
removeSelectedItems(positions)
}
}
}
private fun setupView(view: View, message: Message) {
view.apply {

View File

@ -175,6 +175,13 @@ fun Context.insertNewSMS(address: String, subject: String, body: String, date: L
contentResolver.insert(uri, contentValues)
}
fun Context.deleteThread(id: Int) {
val uri = Telephony.Sms.CONTENT_URI
val selection = "${Telephony.Sms.THREAD_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

@ -8,4 +8,13 @@
<!-- Notifications -->
<string name="channel_received_sms">Received SMS</string>
<string name="new_message">New message</string>
<!-- Confirmation dialog -->
<string name="delete_conversations_confirmation">Are you sure you want to delete %s?</string> <!-- Are you sure you want to delete 5 conversations? -->
<!-- Are you sure you want to delete 5 conversations? -->
<plurals name="delete_conversations">
<item quantity="one">%d conversation</item>
<item quantity="other">%d conversations</item>
</plurals>
</resources>