From 02ccaf4610c47976c89223f8514ced863e44b34e Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 9 Mar 2017 17:51:44 +0100 Subject: [PATCH] Muting/unmuting accounts --- .../keylesspalace/tusky/AccountActivity.java | 69 +++++++++++++++++-- app/src/main/res/menu/account_toolbar.xml | 4 ++ app/src/main/res/values/strings.xml | 6 +- 3 files changed, 71 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/AccountActivity.java b/app/src/main/java/com/keylesspalace/tusky/AccountActivity.java index a4b30f62..14e6834e 100644 --- a/app/src/main/java/com/keylesspalace/tusky/AccountActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/AccountActivity.java @@ -49,15 +49,15 @@ import java.util.List; import retrofit2.Call; import retrofit2.Callback; +import retrofit2.Response; public class AccountActivity extends BaseActivity { private static final String TAG = "AccountActivity"; // Volley request tag and logging tag - private String domain; - private String accessToken; private String accountId; private boolean following = false; private boolean blocking = false; + private boolean muting = false; private boolean isSelf; private String openInWebUrl; private TabLayout tabLayout; @@ -72,8 +72,6 @@ public class AccountActivity extends BaseActivity { SharedPreferences preferences = getSharedPreferences( getString(R.string.preferences_file_key), Context.MODE_PRIVATE); - domain = preferences.getString("domain", null); - accessToken = preferences.getString("accessToken", null); String loggedInAccountId = preferences.getString("loggedInAccountId", null); final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); @@ -244,7 +242,7 @@ public class AccountActivity extends BaseActivity { @Override public void onResponse(Call> call, retrofit2.Response> response) { Relationship relationship = response.body().get(0); - onObtainRelationshipsSuccess(relationship.following, relationship.blocking); + onObtainRelationshipsSuccess(relationship.following, relationship.blocking, relationship.muting); } @Override @@ -254,11 +252,12 @@ public class AccountActivity extends BaseActivity { }); } - private void onObtainRelationshipsSuccess(boolean following, boolean blocking) { + private void onObtainRelationshipsSuccess(boolean following, boolean blocking, boolean muting) { this.following = following; this.blocking = blocking; + this.muting = muting; - if (!following || !blocking) { + if (!following || !blocking || !muting) { invalidateOptionsMenu(); } @@ -310,10 +309,18 @@ public class AccountActivity extends BaseActivity { title = getString(R.string.action_block); } block.setTitle(title); + MenuItem mute = menu.findItem(R.id.action_mute); + if (muting) { + title = getString(R.string.action_unmute); + } else { + title = getString(R.string.action_mute); + } + mute.setTitle(title); } else { // It shouldn't be possible to block or follow yourself. menu.removeItem(R.id.action_follow); menu.removeItem(R.id.action_block); + menu.removeItem(R.id.action_mute); } return super.onPrepareOptionsMenu(menu); } @@ -396,6 +403,50 @@ public class AccountActivity extends BaseActivity { .show(); } + + private void mute(final String id) { + Callback cb = new Callback() { + @Override + public void onResponse(Call call, Response response) { + muting = response.body().muting; + updateButtons(); + } + + @Override + public void onFailure(Call call, Throwable t) { + onMuteFailure(id); + } + }; + + if (muting) { + mastodonAPI.unmuteAccount(id).enqueue(cb); + } else { + mastodonAPI.muteAccount(id).enqueue(cb); + } + } + + private void onMuteFailure(final String id) { + int messageId; + + if (muting) { + messageId = R.string.error_unmuting; + } else { + messageId = R.string.error_muting; + } + + View.OnClickListener listener = new View.OnClickListener() { + @Override + public void onClick(View v) { + mute(id); + } + }; + + Snackbar.make(findViewById(R.id.activity_account), messageId, Snackbar.LENGTH_LONG) + .setAction(R.string.action_retry, listener) + .show(); + } + + @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { @@ -417,6 +468,10 @@ public class AccountActivity extends BaseActivity { block(accountId); return true; } + case R.id.action_mute: { + mute(accountId); + return true; + } } return super.onOptionsItemSelected(item); } diff --git a/app/src/main/res/menu/account_toolbar.xml b/app/src/main/res/menu/account_toolbar.xml index 3188882e..64b2f22f 100644 --- a/app/src/main/res/menu/account_toolbar.xml +++ b/app/src/main/res/menu/account_toolbar.xml @@ -10,6 +10,10 @@ android:title="@string/action_follow" app:showAsAction="never" /> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fbbb2218..20c9d4d8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -114,7 +114,7 @@ Preferences Favourites Blocked Users - Open In Web + Open in browser Set Toot! @@ -158,5 +158,9 @@ Welcome back! Share Share toot URL to... + Mute + Unmute + That user wasn\'t unmuted. + That user wasn\'t muted.