Muting/unmuting accounts

This commit is contained in:
Eugen Rochko 2017-03-09 17:51:44 +01:00
parent 9e82b64423
commit 02ccaf4610
3 changed files with 71 additions and 8 deletions

View file

@ -49,15 +49,15 @@ import java.util.List;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response;
public class AccountActivity extends BaseActivity { public class AccountActivity extends BaseActivity {
private static final String TAG = "AccountActivity"; // Volley request tag and logging tag private static final String TAG = "AccountActivity"; // Volley request tag and logging tag
private String domain;
private String accessToken;
private String accountId; private String accountId;
private boolean following = false; private boolean following = false;
private boolean blocking = false; private boolean blocking = false;
private boolean muting = false;
private boolean isSelf; private boolean isSelf;
private String openInWebUrl; private String openInWebUrl;
private TabLayout tabLayout; private TabLayout tabLayout;
@ -72,8 +72,6 @@ public class AccountActivity extends BaseActivity {
SharedPreferences preferences = getSharedPreferences( SharedPreferences preferences = getSharedPreferences(
getString(R.string.preferences_file_key), Context.MODE_PRIVATE); 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); String loggedInAccountId = preferences.getString("loggedInAccountId", null);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
@ -244,7 +242,7 @@ public class AccountActivity extends BaseActivity {
@Override @Override
public void onResponse(Call<List<Relationship>> call, retrofit2.Response<List<Relationship>> response) { public void onResponse(Call<List<Relationship>> call, retrofit2.Response<List<Relationship>> response) {
Relationship relationship = response.body().get(0); Relationship relationship = response.body().get(0);
onObtainRelationshipsSuccess(relationship.following, relationship.blocking); onObtainRelationshipsSuccess(relationship.following, relationship.blocking, relationship.muting);
} }
@Override @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.following = following;
this.blocking = blocking; this.blocking = blocking;
this.muting = muting;
if (!following || !blocking) { if (!following || !blocking || !muting) {
invalidateOptionsMenu(); invalidateOptionsMenu();
} }
@ -310,10 +309,18 @@ public class AccountActivity extends BaseActivity {
title = getString(R.string.action_block); title = getString(R.string.action_block);
} }
block.setTitle(title); 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 { } else {
// It shouldn't be possible to block or follow yourself. // It shouldn't be possible to block or follow yourself.
menu.removeItem(R.id.action_follow); menu.removeItem(R.id.action_follow);
menu.removeItem(R.id.action_block); menu.removeItem(R.id.action_block);
menu.removeItem(R.id.action_mute);
} }
return super.onPrepareOptionsMenu(menu); return super.onPrepareOptionsMenu(menu);
} }
@ -396,6 +403,50 @@ public class AccountActivity extends BaseActivity {
.show(); .show();
} }
private void mute(final String id) {
Callback<Relationship> cb = new Callback<Relationship>() {
@Override
public void onResponse(Call<Relationship> call, Response<Relationship> response) {
muting = response.body().muting;
updateButtons();
}
@Override
public void onFailure(Call<Relationship> 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 @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
@ -417,6 +468,10 @@ public class AccountActivity extends BaseActivity {
block(accountId); block(accountId);
return true; return true;
} }
case R.id.action_mute: {
mute(accountId);
return true;
}
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }

View file

@ -10,6 +10,10 @@
android:title="@string/action_follow" android:title="@string/action_follow"
app:showAsAction="never" /> app:showAsAction="never" />
<item android:id="@+id/action_mute"
android:title="@string/action_mute"
app:showAsAction="never" />
<item android:id="@+id/action_block" <item android:id="@+id/action_block"
android:title="@string/action_block" android:title="@string/action_block"
app:showAsAction="never" /> app:showAsAction="never" />

View file

@ -114,7 +114,7 @@
<string name="action_view_preferences">Preferences</string> <string name="action_view_preferences">Preferences</string>
<string name="action_view_favourites">Favourites</string> <string name="action_view_favourites">Favourites</string>
<string name="action_view_blocks">Blocked Users</string> <string name="action_view_blocks">Blocked Users</string>
<string name="action_open_in_web">Open In Web</string> <string name="action_open_in_web">Open in browser</string>
<string name="action_set_time">Set</string> <string name="action_set_time">Set</string>
<string name="confirmation_send">Toot!</string> <string name="confirmation_send">Toot!</string>
@ -158,5 +158,9 @@
<string name="login_success">Welcome back!</string> <string name="login_success">Welcome back!</string>
<string name="action_share">Share</string> <string name="action_share">Share</string>
<string name="send_status_to">Share toot URL to...</string> <string name="send_status_to">Share toot URL to...</string>
<string name="action_mute">Mute</string>
<string name="action_unmute">Unmute</string>
<string name="error_unmuting">That user wasn\'t unmuted.</string>
<string name="error_muting">That user wasn\'t muted.</string>
</resources> </resources>