From 93fce1f56e911cf4249746decb2dc46a31cebe5c Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 14 Dec 2022 11:21:00 +0100 Subject: [PATCH] Allow to mute/unmute from profile --- .../android/activities/ProfileActivity.java | 34 ++++++++++ .../client/entities/app/MutedAccounts.java | 22 ++++++ .../android/ui/drawer/StatusAdapter.java | 11 +++ .../viewmodel/mastodon/AccountsVM.java | 67 +++++++++++++++++++ app/src/main/res/menu/activity_profile.xml | 5 ++ app/src/main/res/menu/option_toot.xml | 4 ++ app/src/main/res/values/strings.xml | 2 + 7 files changed, 145 insertions(+) diff --git a/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java b/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java index 79ac5c58..9b795443 100644 --- a/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java @@ -16,6 +16,7 @@ package app.fedilab.android.activities; import static app.fedilab.android.BaseMainActivity.currentAccount; +import static app.fedilab.android.ui.drawer.StatusAdapter.sendAction; import android.content.BroadcastReceiver; import android.content.ClipData; @@ -117,6 +118,7 @@ public class ProfileActivity extends BaseActivity { private String mention_str; private WellKnownNodeinfo.NodeInfo nodeInfo; private boolean checkRemotely; + private boolean homeMuted; private final BroadcastReceiver broadcast_data = new BroadcastReceiver() { @Override @@ -144,6 +146,7 @@ public class ProfileActivity extends BaseActivity { Bundle b = getIntent().getExtras(); binding.accountFollow.setEnabled(false); checkRemotely = false; + homeMuted = false; if (b != null) { account = (Account) b.getSerializable(Helper.ARG_ACCOUNT); account_id = b.getString(Helper.ARG_USER_ID, null); @@ -185,6 +188,8 @@ public class ProfileActivity extends BaseActivity { Toasty.error(ProfileActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show(); finish(); } + //Check if account is homeMuted + accountsVM.isMuted(currentAccount, account).observe(ProfileActivity.this, account1 -> homeMuted = account1 != null); LocalBroadcastManager.getInstance(ProfileActivity.this).registerReceiver(broadcast_data, new IntentFilter(Helper.BROADCAST_DATA)); } @@ -685,9 +690,11 @@ public class ProfileActivity extends BaseActivity { menu.findItem(R.id.action_endorse).setVisible(false); menu.findItem(R.id.action_direct_message).setVisible(false); menu.findItem(R.id.action_add_to_list).setVisible(false); + menu.findItem(R.id.action_mute_home).setVisible(false); } else { menu.findItem(R.id.action_block).setVisible(true); menu.findItem(R.id.action_mute).setVisible(true); + menu.findItem(R.id.action_mute_home).setVisible(true); menu.findItem(R.id.action_timed_mute).setVisible(true); menu.findItem(R.id.action_mention).setVisible(true); } @@ -696,6 +703,7 @@ public class ProfileActivity extends BaseActivity { if (!relationship.following) { menu.findItem(R.id.action_hide_boost).setVisible(false); menu.findItem(R.id.action_endorse).setVisible(false); + menu.findItem(R.id.action_mute_home).setVisible(false); } if (relationship.blocking) { menu.findItem(R.id.action_block).setTitle(R.string.action_unblock); @@ -713,6 +721,11 @@ public class ProfileActivity extends BaseActivity { } else { menu.findItem(R.id.action_hide_boost).setTitle(getString(R.string.show_boost, account.username)); } + if (homeMuted) { + menu.findItem(R.id.action_mute_home).setTitle(getString(R.string.unmute_home)); + } else { + menu.findItem(R.id.action_mute_home).setTitle(getString(R.string.mute_home)); + } } } return true; @@ -989,6 +1002,27 @@ public class ProfileActivity extends BaseActivity { }); builderInner.show(); } + } else if (itemId == R.id.action_mute_home) { + AlertDialog.Builder builderInner = new AlertDialog.Builder(ProfileActivity.this, Helper.dialogStyle()); + builderInner.setMessage(account.acct); + builderInner.setNeutralButton(R.string.cancel, (dialog, which) -> dialog.dismiss()); + if (homeMuted) { + builderInner.setTitle(R.string.unmute_home); + builderInner.setPositiveButton(R.string.action_unmute, (dialog, which) -> accountsVM.unmuteHome(currentAccount, account) + .observe(ProfileActivity.this, account -> { + homeMuted = false; + Toasty.info(ProfileActivity.this, getString(R.string.toast_unmute), Toasty.LENGTH_LONG).show(); + })); + } else { + builderInner.setTitle(R.string.mute_home); + builderInner.setPositiveButton(R.string.action_mute, (dialog, which) -> accountsVM.muteHome(currentAccount, account) + .observe(ProfileActivity.this, account -> { + homeMuted = true; + sendAction(ProfileActivity.this, Helper.ARG_STATUS_ACCOUNT_ID_DELETED, null, account.id); + Toasty.info(ProfileActivity.this, getString(R.string.toast_mute), Toasty.LENGTH_LONG).show(); + })); + } + builderInner.show(); } else if (itemId == R.id.action_timed_mute) { MastodonHelper.scheduleBoost(ProfileActivity.this, MastodonHelper.ScheduleType.TIMED_MUTED, null, account, rs -> { this.relationship = rs; diff --git a/app/src/main/java/app/fedilab/android/client/entities/app/MutedAccounts.java b/app/src/main/java/app/fedilab/android/client/entities/app/MutedAccounts.java index 7ee494f5..355fece9 100644 --- a/app/src/main/java/app/fedilab/android/client/entities/app/MutedAccounts.java +++ b/app/src/main/java/app/fedilab/android/client/entities/app/MutedAccounts.java @@ -180,6 +180,28 @@ public class MutedAccounts implements Serializable { } } + /** + * Check if an account is muted in db + * + * @param forAccount {@link BaseAccount} + * @param target {@link Account} + * @return MutedAccounts - {@link MutedAccounts} + */ + public boolean isMuted(BaseAccount forAccount, Account target) throws DBException { + if (db == null) { + throw new DBException("db is null. Wrong initialization."); + } + MutedAccounts mutedAccounts = getMutedAccount(forAccount); + if (mutedAccounts != null && mutedAccounts.accounts != null) { + for (Account account : mutedAccounts.accounts) { + if (account.id.equals(target.id)) { + return true; + } + } + } + return false; + } + /** * Returns the MutedAccounts for an account * diff --git a/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java b/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java index 41b29f4b..5861c8b1 100644 --- a/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java +++ b/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java @@ -1655,6 +1655,17 @@ public class StatusAdapter extends RecyclerView.Adapter Toasty.info(context, context.getString(R.string.toast_mute), Toasty.LENGTH_LONG).show(); })); builderInner.show(); + } else if (itemId == R.id.action_mute_home) { + AlertDialog.Builder builderInner = new AlertDialog.Builder(context, Helper.dialogStyle()); + builderInner.setTitle(R.string.mute_home); + builderInner.setMessage(statusToDeal.account.acct); + builderInner.setNeutralButton(R.string.cancel, (dialog, which) -> dialog.dismiss()); + builderInner.setPositiveButton(R.string.action_mute, (dialog, which) -> accountsVM.muteHome(currentAccount, statusToDeal.account) + .observe((LifecycleOwner) context, account -> { + sendAction(context, Helper.ARG_STATUS_ACCOUNT_ID_DELETED, null, statusToDeal.account.id); + Toasty.info(context, context.getString(R.string.toast_mute), Toasty.LENGTH_LONG).show(); + })); + builderInner.show(); } else if (itemId == R.id.action_mute_conversation) { if (statusToDeal.muted) { statusesVM.unMute(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, statusToDeal.id).observe((LifecycleOwner) context, status1 -> Toasty.info(context, context.getString(R.string.toast_unmute_conversation)).show()); diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AccountsVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AccountsVM.java index 5d4e17cd..aa8a3876 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AccountsVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AccountsVM.java @@ -52,7 +52,10 @@ import app.fedilab.android.client.entities.api.Suggestion; import app.fedilab.android.client.entities.api.Suggestions; import app.fedilab.android.client.entities.api.Tag; import app.fedilab.android.client.entities.api.Token; +import app.fedilab.android.client.entities.app.BaseAccount; +import app.fedilab.android.client.entities.app.MutedAccounts; import app.fedilab.android.client.entities.app.StatusCache; +import app.fedilab.android.exception.DBException; import app.fedilab.android.helper.Helper; import app.fedilab.android.helper.MastodonHelper; import okhttp3.MultipartBody; @@ -748,6 +751,70 @@ public class AccountsVM extends AndroidViewModel { return relationShipMutableLiveData; } + + /** + * Mute the given account in db + * + * @return {@link LiveData} containing the {@link Account} to the given account + */ + public LiveData isMuted(@NonNull BaseAccount forAccount, @NonNull Account target) { + accountMutableLiveData = new MutableLiveData<>(); + new Thread(() -> { + boolean isMuted = false; + try { + isMuted = new MutedAccounts(getApplication().getApplicationContext()).isMuted(forAccount, target); + } catch (DBException e) { + e.printStackTrace(); + } + Handler mainHandler = new Handler(Looper.getMainLooper()); + boolean finalIsMuted = isMuted; + Runnable myRunnable = () -> accountMutableLiveData.setValue(finalIsMuted ? target : null); + mainHandler.post(myRunnable); + }).start(); + return accountMutableLiveData; + } + + /** + * Mute the given account in db + * + * @return {@link LiveData} containing the {@link Account} to the given account + */ + public LiveData muteHome(@NonNull BaseAccount forAccount, @NonNull Account target) { + accountMutableLiveData = new MutableLiveData<>(); + new Thread(() -> { + + try { + new MutedAccounts(getApplication().getApplicationContext()).muteAccount(forAccount, target); + } catch (DBException e) { + e.printStackTrace(); + } + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> accountMutableLiveData.setValue(target); + mainHandler.post(myRunnable); + }).start(); + return accountMutableLiveData; + } + + /** + * Unmute the given account in db + * + * @return {@link LiveData} containing the {@link Account} to the given account + */ + public LiveData unmuteHome(@NonNull BaseAccount forAccount, @NonNull Account target) { + accountMutableLiveData = new MutableLiveData<>(); + new Thread(() -> { + try { + new MutedAccounts(getApplication().getApplicationContext()).unMuteAccount(forAccount, target); + } catch (DBException e) { + e.printStackTrace(); + } + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> accountMutableLiveData.setValue(target); + mainHandler.post(myRunnable); + }).start(); + return accountMutableLiveData; + } + /** * Unmute the given account. * diff --git a/app/src/main/res/menu/activity_profile.xml b/app/src/main/res/menu/activity_profile.xml index d34cfd13..cd17ac2c 100644 --- a/app/src/main/res/menu/activity_profile.xml +++ b/app/src/main/res/menu/activity_profile.xml @@ -16,6 +16,11 @@ android:icon="@drawable/ic_baseline_open_with_24" android:title="@string/action_open_in_web" app:showAsAction="never" /> + + Pin tag Unpin tag Unfollow tag + Mute for home + Unmute for home \ No newline at end of file