From d8de016407008d4496c7ebe6c17dabc7277ffb2c Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 14 Dec 2022 14:29:20 +0100 Subject: [PATCH] Allow to mute/unmute from the list with a new icon --- .../android/activities/ProfileActivity.java | 12 +++------ .../android/client/entities/api/Account.java | 12 +++++++++ .../client/entities/app/MutedAccounts.java | 3 +++ .../android/ui/drawer/AccountAdapter.java | 27 +++++++++++++++++-- .../android/ui/drawer/StatusAdapter.java | 4 --- .../timeline/FragmentMastodonAccount.java | 2 +- .../viewmodel/mastodon/AccountsVM.java | 14 +++++++--- app/src/main/res/layout/drawer_account.xml | 12 +++++++++ 8 files changed, 66 insertions(+), 20 deletions(-) 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 9eef75bf..022c6512 100644 --- a/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java @@ -16,8 +16,6 @@ package app.fedilab.android.activities; import static app.fedilab.android.BaseMainActivity.currentAccount; -import static app.fedilab.android.helper.Helper.addMutedAccount; -import static app.fedilab.android.helper.Helper.removeMutedAccount; import static app.fedilab.android.ui.drawer.StatusAdapter.sendAction; import android.content.BroadcastReceiver; @@ -191,7 +189,7 @@ public class ProfileActivity extends BaseActivity { finish(); } //Check if account is homeMuted - accountsVM.isMuted(currentAccount, account).observe(ProfileActivity.this, account1 -> homeMuted = account1 != null); + accountsVM.isMuted(currentAccount, account).observe(this, result -> homeMuted = result != null && result); LocalBroadcastManager.getInstance(ProfileActivity.this).registerReceiver(broadcast_data, new IntentFilter(Helper.BROADCAST_DATA)); } @@ -1013,19 +1011,15 @@ public class ProfileActivity extends BaseActivity { builderInner.setPositiveButton(R.string.action_unmute, (dialog, which) -> accountsVM.unmuteHome(currentAccount, account) .observe(ProfileActivity.this, account -> { homeMuted = false; - if (account != null) { - removeMutedAccount(account); - } + invalidateOptionsMenu(); 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 -> { - if (account != null) { - addMutedAccount(account); - } homeMuted = true; + invalidateOptionsMenu(); 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(); })); diff --git a/app/src/main/java/app/fedilab/android/client/entities/api/Account.java b/app/src/main/java/app/fedilab/android/client/entities/api/Account.java index ae0b71ae..9c423554 100644 --- a/app/src/main/java/app/fedilab/android/client/entities/api/Account.java +++ b/app/src/main/java/app/fedilab/android/client/entities/api/Account.java @@ -19,6 +19,8 @@ import android.content.Context; import android.text.Spannable; import android.view.View; +import androidx.annotation.Nullable; + import com.google.gson.annotations.SerializedName; import java.io.Serializable; @@ -148,4 +150,14 @@ public class Account implements Serializable { public LinkedHashMap fields; } + + + @Override + public boolean equals(@Nullable Object obj) { + boolean same = false; + if (obj instanceof Account) { + same = this.id.equals(((Account) obj).id); + } + return same; + } } 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 d36d310e..5932aac3 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 @@ -88,6 +88,9 @@ public class MutedAccounts implements Serializable { } } + public void delete() { + db.delete(Sqlite.TABLE_MUTED, null, null); + } /** * Insert an Account in muted account in db diff --git a/app/src/main/java/app/fedilab/android/ui/drawer/AccountAdapter.java b/app/src/main/java/app/fedilab/android/ui/drawer/AccountAdapter.java index 2b0448de..031030ba 100644 --- a/app/src/main/java/app/fedilab/android/ui/drawer/AccountAdapter.java +++ b/app/src/main/java/app/fedilab/android/ui/drawer/AccountAdapter.java @@ -42,6 +42,7 @@ import java.util.List; import app.fedilab.android.BaseMainActivity; import app.fedilab.android.R; +import app.fedilab.android.activities.MainActivity; import app.fedilab.android.activities.ProfileActivity; import app.fedilab.android.client.entities.api.Account; import app.fedilab.android.databinding.DrawerAccountBinding; @@ -56,12 +57,19 @@ public class AccountAdapter extends RecyclerView.Adapter accountList; private Context context; + private final boolean home_mute; + + public AccountAdapter(List accountList, boolean home_mute) { + this.accountList = accountList; + this.home_mute = home_mute; + } public AccountAdapter(List accountList) { this.accountList = accountList; + this.home_mute = false; } - public static void accountManagement(Context context, AccountViewHolder accountViewHolder, Account account, int position, RecyclerView.Adapter adapter) { + public static void accountManagement(Context context, AccountViewHolder accountViewHolder, Account account, int position, RecyclerView.Adapter adapter, boolean home_mute) { MastodonHelper.loadPPMastodon(accountViewHolder.binding.avatar, account); SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context); @@ -73,6 +81,21 @@ public class AccountAdapter extends RecyclerView.Adapter { + if (muted) { + accountsVM.unmuteHome(MainActivity.currentAccount, account).observe((LifecycleOwner) context, account1 -> adapter.notifyItemChanged(accountViewHolder.getLayoutPosition())); + } else { + accountsVM.muteHome(MainActivity.currentAccount, account).observe((LifecycleOwner) context, account1 -> adapter.notifyItemChanged(accountViewHolder.getLayoutPosition())); + } + }); + } else { + accountViewHolder.binding.muteHome.setVisibility(View.GONE); + } + accountViewHolder.binding.avatar.setOnClickListener(v -> { Intent intent = new Intent(context, ProfileActivity.class); Bundle b = new Bundle(); @@ -263,7 +286,7 @@ public class AccountAdapter extends RecyclerView.Adapter 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 -> { - if (account != null) { - addMutedAccount(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(); })); diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonAccount.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonAccount.java index 9cf65321..afef6168 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonAccount.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonAccount.java @@ -203,7 +203,7 @@ public class FragmentMastodonAccount extends Fragment { } this.accounts = accounts.accounts; - accountAdapter = new AccountAdapter(this.accounts); + accountAdapter = new AccountAdapter(this.accounts, timelineType == Timeline.TimeLineEnum.MUTED_TIMELINE_HOME); flagLoading = accounts.pagination.max_id == null; LinearLayoutManager mLayoutManager = new LinearLayoutManager(requireActivity()); binding.recyclerView.setLayoutManager(mLayoutManager); 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 7a09b09b..f24b257d 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 @@ -14,6 +14,8 @@ package app.fedilab.android.viewmodel.mastodon; * You should have received a copy of the GNU General Public License along with Fedilab; if not, * see . */ +import static app.fedilab.android.helper.Helper.addMutedAccount; +import static app.fedilab.android.helper.Helper.removeMutedAccount; import static app.fedilab.android.ui.drawer.StatusAdapter.sendAction; import android.app.Application; @@ -95,6 +97,8 @@ public class AccountsVM extends AndroidViewModel { private MutableLiveData tokenMutableLiveData; private MutableLiveData domainsMutableLiveData; private MutableLiveData reportMutableLiveData; + private MutableLiveData booleanMutableLiveData; + public AccountsVM(@NonNull Application application) { super(application); @@ -784,8 +788,8 @@ public class AccountsVM extends AndroidViewModel { * * @return {@link LiveData} containing the {@link Account} to the given account */ - public LiveData isMuted(@NonNull BaseAccount forAccount, @NonNull Account target) { - accountMutableLiveData = new MutableLiveData<>(); + public LiveData isMuted(@NonNull BaseAccount forAccount, @NonNull Account target) { + booleanMutableLiveData = new MutableLiveData<>(); new Thread(() -> { boolean isMuted = false; try { @@ -795,10 +799,10 @@ public class AccountsVM extends AndroidViewModel { } Handler mainHandler = new Handler(Looper.getMainLooper()); boolean finalIsMuted = isMuted; - Runnable myRunnable = () -> accountMutableLiveData.setValue(finalIsMuted ? target : null); + Runnable myRunnable = () -> booleanMutableLiveData.setValue(finalIsMuted); mainHandler.post(myRunnable); }).start(); - return accountMutableLiveData; + return booleanMutableLiveData; } /** @@ -812,6 +816,7 @@ public class AccountsVM extends AndroidViewModel { try { new MutedAccounts(getApplication().getApplicationContext()).muteAccount(forAccount, target); + addMutedAccount(target); } catch (DBException e) { e.printStackTrace(); } @@ -832,6 +837,7 @@ public class AccountsVM extends AndroidViewModel { new Thread(() -> { try { new MutedAccounts(getApplication().getApplicationContext()).unMuteAccount(forAccount, target); + removeMutedAccount(target); } catch (DBException e) { e.printStackTrace(); } diff --git a/app/src/main/res/layout/drawer_account.xml b/app/src/main/res/layout/drawer_account.xml index 449c8bef..61caa37a 100644 --- a/app/src/main/res/layout/drawer_account.xml +++ b/app/src/main/res/layout/drawer_account.xml @@ -116,6 +116,18 @@ app:iconPadding="0dp" app:strokeWidth="1dp" /> + +