allow blocking numbers directly from the main screen

This commit is contained in:
tibbi 2020-05-23 18:56:24 +02:00
parent d8e2a774e3
commit 3ac08e2b21
2 changed files with 39 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.smsmessenger.adapters
import android.content.Intent import android.content.Intent
import android.graphics.Typeface import android.graphics.Typeface
import android.text.TextUtils
import android.view.Menu import android.view.Menu
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -9,6 +10,7 @@ import android.widget.TextView
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.extensions.addBlockedNumber
import com.simplemobiletools.commons.extensions.formatDateOrTime import com.simplemobiletools.commons.extensions.formatDateOrTime
import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.helpers.KEY_PHONE import com.simplemobiletools.commons.helpers.KEY_PHONE
@ -45,6 +47,7 @@ class ConversationsAdapter(activity: SimpleActivity, var conversations: ArrayLis
when (id) { when (id) {
R.id.cab_add_number_to_contact -> addNumberToContact() R.id.cab_add_number_to_contact -> addNumberToContact()
R.id.cab_block_number -> askConfirmBlock()
R.id.cab_select_all -> selectAll() R.id.cab_select_all -> selectAll()
R.id.cab_delete -> askConfirmDelete() R.id.cab_delete -> askConfirmDelete()
} }
@ -74,6 +77,37 @@ class ConversationsAdapter(activity: SimpleActivity, var conversations: ArrayLis
override fun getItemCount() = conversations.size override fun getItemCount() = conversations.size
private fun askConfirmBlock() {
val numbers = TextUtils.join(", ", getSelectedItems().distinctBy { it.phoneNumber }.map { it.phoneNumber })
val baseString = R.string.block_confirmation
val question = String.format(resources.getString(baseString), numbers)
ConfirmationDialog(activity, question) {
blockNumbers()
}
}
private fun blockNumbers() {
if (selectedKeys.isEmpty()) {
return
}
val numbersToBlock = getSelectedItems()
val positions = getSelectedItemPositions()
conversations.removeAll(numbersToBlock)
ensureBackgroundThread {
numbersToBlock.map { it.phoneNumber }.forEach { number ->
activity.addBlockedNumber(number)
}
activity.runOnUiThread {
removeSelectedItems(positions)
finishActMode()
}
}
}
private fun askConfirmDelete() { private fun askConfirmDelete() {
val itemsCnt = selectedKeys.size val itemsCnt = selectedKeys.size
val items = resources.getQuantityString(R.plurals.delete_conversations, itemsCnt, itemsCnt) val items = resources.getQuantityString(R.plurals.delete_conversations, itemsCnt, itemsCnt)

View File

@ -6,6 +6,11 @@
android:icon="@drawable/ic_add_person_vector" android:icon="@drawable/ic_add_person_vector"
android:title="@string/add_number_to_contact" android:title="@string/add_number_to_contact"
app:showAsAction="ifRoom" /> app:showAsAction="ifRoom" />
<item
android:id="@+id/cab_block_number"
android:icon="@drawable/ic_minus_circle_vector"
android:title="@string/block_number"
app:showAsAction="ifRoom" />
<item <item
android:id="@+id/cab_select_all" android:id="@+id/cab_select_all"
android:icon="@drawable/ic_select_all_vector" android:icon="@drawable/ic_select_all_vector"