correctly update the menu when muting domains (#1848)

This commit is contained in:
Konrad Pozniak 2020-06-23 19:59:49 +02:00 committed by GitHub
parent 58a1046348
commit dfd30ec52a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 12 deletions

View file

@ -81,6 +81,7 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidI
private var followState: FollowState = FollowState.NOT_FOLLOWING private var followState: FollowState = FollowState.NOT_FOLLOWING
private var blocking: Boolean = false private var blocking: Boolean = false
private var muting: Boolean = false private var muting: Boolean = false
private var blockingDomain: Boolean = false
private var showingReblogs: Boolean = false private var showingReblogs: Boolean = false
private var loadedAccount: Account? = null private var loadedAccount: Account? = null
@ -528,6 +529,7 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidI
} }
blocking = relation.blocking blocking = relation.blocking
muting = relation.muting muting = relation.muting
blockingDomain = relation.blockingDomain
showingReblogs = relation.showingReblogs showingReblogs = relation.showingReblogs
accountFollowsYouTextView.visible(relation.followedBy) 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... // If we can't get the domain, there's no way we can mute it anyway...
menu.removeItem(R.id.action_mute_domain) menu.removeItem(R.id.action_mute_domain)
} else { } 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() .show()
} }
private fun showMuteDomainWarningDialog(instance: String) { private fun toggleBlockDomain(instance: String) {
AlertDialog.Builder(this) if(blockingDomain) {
.setMessage(getString(R.string.mute_domain_warning, instance)) viewModel.unblockDomain(instance)
.setPositiveButton(getString(R.string.mute_domain_warning_dialog_ok)) { _, _ -> viewModel.muteDomain(instance) } } else {
.setNegativeButton(android.R.string.cancel, null) AlertDialog.Builder(this)
.show() .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() { private fun toggleBlock() {
@ -757,7 +767,7 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidI
return true return true
} }
R.id.action_mute_domain -> { R.id.action_mute_domain -> {
showMuteDomainWarningDialog(domain) toggleBlockDomain(domain)
return true return true
} }
R.id.action_show_reblogs -> { R.id.action_show_reblogs -> {

View file

@ -24,5 +24,6 @@ data class Relationship (
val blocking: Boolean, val blocking: Boolean,
val muting: Boolean, val muting: Boolean,
val requested: Boolean, val requested: Boolean,
@SerializedName("showing_reblogs") val showingReblogs: Boolean @SerializedName("showing_reblogs") val showingReblogs: Boolean,
@SerializedName("domain_blocking") val blockingDomain: Boolean
) )

View file

@ -156,18 +156,41 @@ class AccountViewModel @Inject constructor(
} }
} }
fun muteDomain(instance: String) { fun blockDomain(instance: String) {
mastodonApi.blockDomain(instance).enqueue(object: Callback<Any> { mastodonApi.blockDomain(instance).enqueue(object: Callback<Any> {
override fun onResponse(call: Call<Any>, response: Response<Any>) { override fun onResponse(call: Call<Any>, response: Response<Any>) {
if (response.isSuccessful) { if (response.isSuccessful) {
eventHub.dispatch(DomainMuteEvent(instance)) eventHub.dispatch(DomainMuteEvent(instance))
val relation = relationshipData.value?.data
if(relation != null) {
relationshipData.postValue(Success(relation.copy(blockingDomain = true)))
}
} else { } else {
Log.e(TAG, String.format("Error muting %s", instance)) Log.e(TAG, "Error muting %s".format(instance))
} }
} }
override fun onFailure(call: Call<Any>, t: Throwable) { override fun onFailure(call: Call<Any>, 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<Any> {
override fun onResponse(call: Call<Any>, response: Response<Any>) {
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<Any>, t: Throwable) {
Log.e(TAG, "Error unmuting %s".format(instance), t)
} }
}) })
} }

View file

@ -109,6 +109,7 @@
<string name="action_mute">Mute</string> <string name="action_mute">Mute</string>
<string name="action_unmute">Unmute</string> <string name="action_unmute">Unmute</string>
<string name="action_mute_domain">Mute %s</string> <string name="action_mute_domain">Mute %s</string>
<string name="action_unmute_domain">Unmute %s</string>
<string name="action_mute_conversation">Mute conversation</string> <string name="action_mute_conversation">Mute conversation</string>
<string name="action_unmute_conversation">Unmute conversation</string> <string name="action_unmute_conversation">Unmute conversation</string>
<string name="action_mention">Mention</string> <string name="action_mention">Mention</string>