Don't show blocking dialog if not necessary

This commit is contained in:
moezbhatti
2019-08-23 01:30:50 +02:00
parent 1439e3f126
commit c5fa1ce0f5
11 changed files with 103 additions and 51 deletions
@@ -50,7 +50,7 @@ abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkVi
val selectionChanges: Subject<List<Long>> = BehaviorSubject.create()
private val selection = mutableListOf<Long>()
private var selection = listOf<Long>()
/**
* Toggles the selected state for a particular view
@@ -61,9 +61,9 @@ abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkVi
protected fun toggleSelection(id: Long, force: Boolean = true): Boolean {
if (!force && selection.isEmpty()) return false
when (selection.contains(id)) {
true -> selection.remove(id)
false -> selection.add(id)
selection = when (selection.contains(id)) {
true -> selection - id
false -> selection + id
}
selectionChanges.onNext(selection)
@@ -75,7 +75,7 @@ abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkVi
}
fun clearSelection() {
selection.clear()
selection = listOf()
selectionChanges.onNext(selection)
notifyDataSetChanged()
}
@@ -23,10 +23,15 @@ import android.app.AlertDialog
import android.content.Context
import com.moez.QKSMS.R
import com.moez.QKSMS.blocking.BlockingClient
import com.moez.QKSMS.blocking.BlockingClientCapability
import com.moez.QKSMS.interactor.MarkBlocked
import com.moez.QKSMS.interactor.MarkUnblocked
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.util.Preferences
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
// TODO: Once we have a custom dialog based on conductor, turn this into a controller
@@ -39,28 +44,45 @@ class BlockingDialog @Inject constructor(
private val markUnblocked: MarkUnblocked
) {
fun show(activity: Activity, conversationIds: List<Long>, block: Boolean) {
fun show(activity: Activity, conversationIds: List<Long>, block: Boolean) = GlobalScope.launch {
val addresses = conversationIds.toLongArray()
.let(conversationRepo::getConversations)
.let { conversationRepo.getConversations(*it) }
.flatMap { conversation -> conversation.recipients }
.map { it.address }
.distinct()
// If we can block/unblock in the external manager, then just fire that off and exit
if (block) {
markBlocked.execute(conversationIds)
if (blockingManager.canBlock()) {
blockingManager.block(addresses).subscribe()
return
}
} else {
markUnblocked.execute(conversationIds)
if (blockingManager.canUnblock()) {
blockingManager.unblock(addresses).subscribe()
return
}
if (addresses.isEmpty()) {
return@launch
}
if (blockingManager.getClientCapability() == BlockingClientCapability.BLOCK_WITHOUT_PERMISSION) {
// If we can block/unblock in the external manager, then just fire that off and exit
if (block) {
markBlocked.execute(conversationIds)
blockingManager.block(addresses).subscribe()
} else {
markUnblocked.execute(conversationIds)
blockingManager.unblock(addresses).subscribe()
}
} else if (addresses.all { address -> block == blockingManager.isBlocked(address).blockingGet() }) {
// If all of the addresses are already in their correct state in the blocking manager, just marked the
// conversations blocked and exit
when (block) {
true -> markBlocked.execute(conversationIds)
false -> markUnblocked.execute(conversationIds)
}
} else {
// Otherwise, show the UI that lets the users know they need to mark the number as blocked in the client
showDialog(activity, conversationIds, addresses, block)
}
}
private suspend fun showDialog(
activity: Activity,
conversationIds: List<Long>,
addresses: List<String>,
block: Boolean
) = withContext(MainScope().coroutineContext) {
val res = when (block) {
true -> R.plurals.blocking_block_external
false -> R.plurals.blocking_unblock_external
@@ -77,13 +99,19 @@ class BlockingDialog @Inject constructor(
// Otherwise, show a dialog asking the user if they want to be directed to the external
// blocking manager
AlertDialog.Builder(activity)
.setTitle(R.string.main_menu_block)
.setTitle(when (block) {
true -> R.string.blocking_block_title
false -> R.string.blocking_unblock_title
})
.setMessage(message)
.setPositiveButton(R.string.button_yes) { _, _ ->
when (block) {
true -> blockingManager.block(addresses)
false -> blockingManager.unblock(addresses)
}.subscribe()
.setPositiveButton(R.string.button_continue) { _, _ ->
if (block) {
markBlocked.execute(conversationIds)
blockingManager.block(addresses).subscribe()
} else {
markUnblocked.execute(conversationIds)
blockingManager.unblock(addresses).subscribe()
}
}
.setNegativeButton(R.string.button_cancel) { _, _ -> }
.create()