Disable unblock button

This commit is contained in:
andrew 2023-05-19 23:44:07 +09:30
parent f64fe4b652
commit 30d748e147
9 changed files with 98 additions and 79 deletions

View File

@ -276,7 +276,7 @@ public class RecipientDatabase extends Database {
notifyRecipientListeners();
}
public void setBlocked(@NonNull List<Recipient> recipients, boolean blocked) {
public void setBlocked(@NonNull Iterable<Recipient> recipients, boolean blocked) {
SQLiteDatabase db = getWritableDatabase();
db.beginTransaction();
try {

View File

@ -1010,7 +1010,7 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context,
DatabaseComponent.get(context).reactionDatabase().deleteMessageReactions(MessageId(messageId, mms))
}
override fun unblock(toUnblock: List<Recipient>) {
override fun unblock(toUnblock: Iterable<Recipient>) {
val recipientDb = DatabaseComponent.get(context).recipientDatabase()
recipientDb.setBlocked(toUnblock, false)
}

View File

@ -2,7 +2,6 @@ package org.thoughtcrime.securesms.preferences
import android.app.AlertDialog
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.core.view.isVisible
import dagger.hilt.android.AndroidEntryPoint
@ -11,58 +10,31 @@ import network.loki.messenger.databinding.ActivityBlockedContactsBinding
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
@AndroidEntryPoint
class BlockedContactsActivity: PassphraseRequiredActionBarActivity(), View.OnClickListener {
class BlockedContactsActivity: PassphraseRequiredActionBarActivity() {
lateinit var binding: ActivityBlockedContactsBinding
val viewModel: BlockedContactsViewModel by viewModels()
val adapter = BlockedContactsAdapter()
val adapter: BlockedContactsAdapter by lazy { BlockedContactsAdapter(viewModel) }
override fun onClick(v: View?) {
if (v === binding.unblockButton && adapter.getSelectedItems().isNotEmpty()) {
val contactsToUnblock = adapter.getSelectedItems()
// show dialog
val title = if (contactsToUnblock.size == 1) {
getString(R.string.Unblock_dialog__title_single, contactsToUnblock.first().name)
} else {
getString(R.string.Unblock_dialog__title_multiple)
fun unblock() {
// show dialog
val title = viewModel.getTitle(this)
val message = viewModel.getMessage(this)
AlertDialog.Builder(this)
.setTitle(title)
.setMessage(message)
.setPositiveButton(R.string.continue_2) { d, _ ->
viewModel.unblock()
d.dismiss()
}
val message = if (contactsToUnblock.size == 1) {
getString(R.string.Unblock_dialog__message, contactsToUnblock.first().name)
} else {
val stringBuilder = StringBuilder()
val iterator = contactsToUnblock.iterator()
var numberAdded = 0
while (iterator.hasNext() && numberAdded < 3) {
val nextRecipient = iterator.next()
if (numberAdded > 0) stringBuilder.append(", ")
stringBuilder.append(nextRecipient.name)
numberAdded++
}
val overflow = contactsToUnblock.size - numberAdded
if (overflow > 0) {
stringBuilder.append(" ")
val string = resources.getQuantityString(R.plurals.Unblock_dialog__message_multiple_overflow, overflow)
stringBuilder.append(string.format(overflow))
}
getString(R.string.Unblock_dialog__message, stringBuilder.toString())
.setNegativeButton(R.string.cancel) { d, _ ->
d.dismiss()
}
AlertDialog.Builder(this)
.setTitle(title)
.setMessage(message)
.setPositiveButton(R.string.continue_2) { d, _ ->
viewModel.unblock(contactsToUnblock)
d.dismiss()
}
.setNegativeButton(R.string.cancel) { d, _ ->
d.dismiss()
}
.show()
}
.show()
}
override fun onCreate(savedInstanceState: Bundle?, ready: Boolean) {
@ -73,15 +45,15 @@ class BlockedContactsActivity: PassphraseRequiredActionBarActivity(), View.OnCli
binding.recyclerView.adapter = adapter
viewModel.subscribe(this)
.observe(this) { newState ->
adapter.submitList(newState.blockedContacts)
val isEmpty = newState.blockedContacts.isEmpty()
binding.emptyStateMessageTextView.isVisible = isEmpty
binding.nonEmptyStateGroup.isVisible = !isEmpty
.observe(this) { state ->
adapter.submitList(state.blockedContacts)
binding.emptyStateMessageTextView.isVisible = state.emptyStateMessageTextViewVisible
binding.nonEmptyStateGroup.isVisible = state.nonEmptyStateGroupVisible
binding.unblockButton.isEnabled = state.unblockButtonEnabled
}
binding.unblockButton.setOnClickListener(this)
binding.unblockButton.setOnClickListener { unblock() }
}
}

View File

@ -11,16 +11,14 @@ import network.loki.messenger.databinding.BlockedContactLayoutBinding
import org.session.libsession.utilities.recipients.Recipient
import org.thoughtcrime.securesms.mms.GlideApp
class BlockedContactsAdapter: ListAdapter<Recipient,BlockedContactsAdapter.ViewHolder>(RecipientDiffer()) {
class BlockedContactsAdapter(val viewModel: BlockedContactsViewModel) : ListAdapter<Recipient,BlockedContactsAdapter.ViewHolder>(RecipientDiffer()) {
class RecipientDiffer: DiffUtil.ItemCallback<Recipient>() {
override fun areItemsTheSame(oldItem: Recipient, newItem: Recipient) = oldItem === newItem
override fun areContentsTheSame(oldItem: Recipient, newItem: Recipient) = oldItem == newItem
}
private val selectedItems = mutableListOf<Recipient>()
fun getSelectedItems() = selectedItems
fun getSelectedItems() = viewModel.state.selectedItems
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.blocked_contact_layout, parent, false)
@ -28,19 +26,15 @@ class BlockedContactsAdapter: ListAdapter<Recipient,BlockedContactsAdapter.ViewH
}
private fun toggleSelection(recipient: Recipient, isSelected: Boolean, position: Int) {
if (isSelected) {
selectedItems -= recipient
} else {
selectedItems += recipient
}
viewModel.select(recipient, isSelected)
notifyItemChanged(position)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val recipient = getItem(position)
val isSelected = recipient in selectedItems
val isSelected = recipient in viewModel.state.selectedItems
holder.bind(recipient, isSelected) {
toggleSelection(recipient, isSelected, position)
toggleSelection(recipient, !isSelected, position)
}
}

View File

@ -17,6 +17,7 @@ import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch
import kotlinx.coroutines.plus
import kotlinx.coroutines.withContext
import network.loki.messenger.R
import org.session.libsession.utilities.recipients.Recipient
import org.thoughtcrime.securesms.database.DatabaseContentProviders
import org.thoughtcrime.securesms.database.Storage
@ -29,7 +30,9 @@ class BlockedContactsViewModel @Inject constructor(private val storage: Storage)
private val listUpdateChannel = Channel<Unit>(capacity = Channel.CONFLATED)
private val _contacts = MutableLiveData(BlockedContactsViewState(emptyList()))
private val _state = MutableLiveData(BlockedContactsViewState(emptyList(), emptySet()))
val state get() = _state.value!!
fun subscribe(context: Context): LiveData<BlockedContactsViewState> {
executor.launch(IO) {
@ -45,21 +48,66 @@ class BlockedContactsViewModel @Inject constructor(private val storage: Storage)
}
executor.launch(IO) {
for (update in listUpdateChannel) {
val blockedContactState = BlockedContactsViewState(storage.blockedContacts().sortedBy { it.name })
val blockedContactState = state.copy(
blockedContacts = storage.blockedContacts().sortedBy { it.name }
)
withContext(Main) {
_contacts.value = blockedContactState
_state.value = blockedContactState
}
}
}
return _contacts
return _state
}
fun unblock(toUnblock: List<Recipient>) {
storage.unblock(toUnblock)
fun unblock() {
storage.unblock(state.selectedItems)
_state.value = state.copy(selectedItems = emptySet())
}
fun select(selectedItem: Recipient, isSelected: Boolean) {
_state.value = state.run {
if (isSelected) copy(selectedItems = selectedItems + selectedItem)
else copy(selectedItems = selectedItems - selectedItem)
}
}
fun getTitle(context: Context): String =
if (state.selectedItems.size == 1) {
context.getString(R.string.Unblock_dialog__title_single, state.selectedItems.first().name)
} else {
context.getString(R.string.Unblock_dialog__title_multiple)
}
fun getMessage(context: Context): String {
if (state.selectedItems.size == 1) {
return context.getString(R.string.Unblock_dialog__message, state.selectedItems.first().name)
}
val stringBuilder = StringBuilder()
val iterator = state.selectedItems.iterator()
var numberAdded = 0
while (iterator.hasNext() && numberAdded < 3) {
val nextRecipient = iterator.next()
if (numberAdded > 0) stringBuilder.append(", ")
stringBuilder.append(nextRecipient.name)
numberAdded++
}
val overflow = state.selectedItems.size - numberAdded
if (overflow > 0) {
stringBuilder.append(" ")
val string = context.resources.getQuantityString(R.plurals.Unblock_dialog__message_multiple_overflow, overflow)
stringBuilder.append(string.format(overflow))
}
return context.getString(R.string.Unblock_dialog__message, stringBuilder.toString())
}
data class BlockedContactsViewState(
val blockedContacts: List<Recipient>
)
val blockedContacts: List<Recipient>,
val selectedItems: Set<Recipient>
) {
val isEmpty get() = blockedContacts.isEmpty()
val unblockButtonEnabled get() = selectedItems.isNotEmpty()
val emptyStateMessageTextViewVisible get() = blockedContacts.isEmpty()
val nonEmptyStateGroupVisible get() = blockedContacts.isNotEmpty()
}
}

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="?android:textColorTertiary"/>
<item android:color="@color/destructive"/>
</selector>

View File

@ -7,5 +7,5 @@
<corners android:radius="@dimen/medium_button_corner_radius" />
<stroke android:width="@dimen/border_thickness" android:color="@color/destructive" />
<stroke android:width="@dimen/border_thickness" android:color="@color/button_destructive" />
</shape>

View File

@ -113,7 +113,7 @@
<style name="Widget.Session.Button.Common.DestructiveOutline">
<item name="android:background">@drawable/destructive_outline_button_medium_background</item>
<item name="android:textColor">@color/destructive</item>
<item name="android:textColor">@color/button_destructive</item>
<item name="android:drawableTint" tools:ignore="NewApi">?android:textColorPrimary</item>
</style>

View File

@ -202,6 +202,6 @@ interface StorageProtocol {
fun removeReaction(emoji: String, messageTimestamp: Long, author: String, notifyUnread: Boolean)
fun updateReactionIfNeeded(message: Message, sender: String, openGroupSentTimestamp: Long)
fun deleteReactions(messageId: Long, mms: Boolean)
fun unblock(toUnblock: List<Recipient>)
fun unblock(toUnblock: Iterable<Recipient>)
fun blockedContacts(): List<Recipient>
}