From dfd30ec52a3791bae64bf16f319e8745ba7a478f Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Tue, 23 Jun 2020 19:59:49 +0200 Subject: [PATCH] correctly update the menu when muting domains (#1848) --- .../keylesspalace/tusky/AccountActivity.kt | 26 ++++++++++++----- .../tusky/entity/Relationship.kt | 3 +- .../tusky/viewmodel/AccountViewModel.kt | 29 +++++++++++++++++-- app/src/main/res/values/strings.xml | 1 + 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/AccountActivity.kt b/app/src/main/java/com/keylesspalace/tusky/AccountActivity.kt index 9c72da3f8..7f6ee5894 100644 --- a/app/src/main/java/com/keylesspalace/tusky/AccountActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/AccountActivity.kt @@ -81,6 +81,7 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidI private var followState: FollowState = FollowState.NOT_FOLLOWING private var blocking: Boolean = false private var muting: Boolean = false + private var blockingDomain: Boolean = false private var showingReblogs: Boolean = false private var loadedAccount: Account? = null @@ -528,6 +529,7 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidI } blocking = relation.blocking muting = relation.muting + blockingDomain = relation.blockingDomain showingReblogs = relation.showingReblogs accountFollowsYouTextView.visible(relation.followedBy) @@ -626,7 +628,11 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidI // If we can't get the domain, there's no way we can mute it anyway... menu.removeItem(R.id.action_mute_domain) } else { - muteDomain.title = getString(R.string.action_mute_domain, domain) + if (blockingDomain) { + muteDomain.title = getString(R.string.action_unmute_domain, domain) + } else { + muteDomain.title = getString(R.string.action_mute_domain, domain) + } } } @@ -671,12 +677,16 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidI .show() } - private fun showMuteDomainWarningDialog(instance: String) { - AlertDialog.Builder(this) - .setMessage(getString(R.string.mute_domain_warning, instance)) - .setPositiveButton(getString(R.string.mute_domain_warning_dialog_ok)) { _, _ -> viewModel.muteDomain(instance) } - .setNegativeButton(android.R.string.cancel, null) - .show() + private fun toggleBlockDomain(instance: String) { + if(blockingDomain) { + viewModel.unblockDomain(instance) + } else { + AlertDialog.Builder(this) + .setMessage(getString(R.string.mute_domain_warning, instance)) + .setPositiveButton(getString(R.string.mute_domain_warning_dialog_ok)) { _, _ -> viewModel.blockDomain(instance) } + .setNegativeButton(android.R.string.cancel, null) + .show() + } } private fun toggleBlock() { @@ -757,7 +767,7 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidI return true } R.id.action_mute_domain -> { - showMuteDomainWarningDialog(domain) + toggleBlockDomain(domain) return true } R.id.action_show_reblogs -> { diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/Relationship.kt b/app/src/main/java/com/keylesspalace/tusky/entity/Relationship.kt index 3da157038..eb1d20b6a 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/Relationship.kt +++ b/app/src/main/java/com/keylesspalace/tusky/entity/Relationship.kt @@ -24,5 +24,6 @@ data class Relationship ( val blocking: Boolean, val muting: Boolean, val requested: Boolean, - @SerializedName("showing_reblogs") val showingReblogs: Boolean + @SerializedName("showing_reblogs") val showingReblogs: Boolean, + @SerializedName("domain_blocking") val blockingDomain: Boolean ) diff --git a/app/src/main/java/com/keylesspalace/tusky/viewmodel/AccountViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/viewmodel/AccountViewModel.kt index ad90cacc9..2495fce2a 100644 --- a/app/src/main/java/com/keylesspalace/tusky/viewmodel/AccountViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/viewmodel/AccountViewModel.kt @@ -156,18 +156,41 @@ class AccountViewModel @Inject constructor( } } - fun muteDomain(instance: String) { + fun blockDomain(instance: String) { mastodonApi.blockDomain(instance).enqueue(object: Callback { override fun onResponse(call: Call, response: Response) { if (response.isSuccessful) { eventHub.dispatch(DomainMuteEvent(instance)) + val relation = relationshipData.value?.data + if(relation != null) { + relationshipData.postValue(Success(relation.copy(blockingDomain = true))) + } } else { - Log.e(TAG, String.format("Error muting %s", instance)) + Log.e(TAG, "Error muting %s".format(instance)) } } override fun onFailure(call: Call, t: Throwable) { - Log.e(TAG, String.format("Error muting %s", instance), t) + Log.e(TAG, "Error muting %s".format(instance), t) + } + }) + } + + fun unblockDomain(instance: String) { + mastodonApi.unblockDomain(instance).enqueue(object: Callback { + override fun onResponse(call: Call, response: Response) { + if (response.isSuccessful) { + val relation = relationshipData.value?.data + if(relation != null) { + relationshipData.postValue(Success(relation.copy(blockingDomain = false))) + } + } else { + Log.e(TAG, "Error unmuting %s".format(instance)) + } + } + + override fun onFailure(call: Call, t: Throwable) { + Log.e(TAG, "Error unmuting %s".format(instance), t) } }) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fb4032f43..99bb228a3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -109,6 +109,7 @@ Mute Unmute Mute %s + Unmute %s Mute conversation Unmute conversation Mention