Allow to mute/unmute from the list with a new icon

This commit is contained in:
Thomas 2022-12-14 14:29:20 +01:00
parent 807ff6c9da
commit d8de016407
8 changed files with 66 additions and 20 deletions

View File

@ -16,8 +16,6 @@ package app.fedilab.android.activities;
import static app.fedilab.android.BaseMainActivity.currentAccount; 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 static app.fedilab.android.ui.drawer.StatusAdapter.sendAction;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
@ -191,7 +189,7 @@ public class ProfileActivity extends BaseActivity {
finish(); finish();
} }
//Check if account is homeMuted //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)); 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) builderInner.setPositiveButton(R.string.action_unmute, (dialog, which) -> accountsVM.unmuteHome(currentAccount, account)
.observe(ProfileActivity.this, account -> { .observe(ProfileActivity.this, account -> {
homeMuted = false; homeMuted = false;
if (account != null) { invalidateOptionsMenu();
removeMutedAccount(account);
}
Toasty.info(ProfileActivity.this, getString(R.string.toast_unmute), Toasty.LENGTH_LONG).show(); Toasty.info(ProfileActivity.this, getString(R.string.toast_unmute), Toasty.LENGTH_LONG).show();
})); }));
} else { } else {
builderInner.setTitle(R.string.mute_home); builderInner.setTitle(R.string.mute_home);
builderInner.setPositiveButton(R.string.action_mute, (dialog, which) -> accountsVM.muteHome(currentAccount, account) builderInner.setPositiveButton(R.string.action_mute, (dialog, which) -> accountsVM.muteHome(currentAccount, account)
.observe(ProfileActivity.this, account -> { .observe(ProfileActivity.this, account -> {
if (account != null) {
addMutedAccount(account);
}
homeMuted = true; homeMuted = true;
invalidateOptionsMenu();
sendAction(ProfileActivity.this, Helper.ARG_STATUS_ACCOUNT_ID_DELETED, null, account.id); 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(); Toasty.info(ProfileActivity.this, getString(R.string.toast_mute), Toasty.LENGTH_LONG).show();
})); }));

View File

@ -19,6 +19,8 @@ import android.content.Context;
import android.text.Spannable; import android.text.Spannable;
import android.view.View; import android.view.View;
import androidx.annotation.Nullable;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.io.Serializable; import java.io.Serializable;
@ -148,4 +150,14 @@ public class Account implements Serializable {
public LinkedHashMap<Integer, Field.FieldParams> fields; public LinkedHashMap<Integer, Field.FieldParams> fields;
} }
@Override
public boolean equals(@Nullable Object obj) {
boolean same = false;
if (obj instanceof Account) {
same = this.id.equals(((Account) obj).id);
}
return same;
}
} }

View File

@ -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 * Insert an Account in muted account in db

View File

@ -42,6 +42,7 @@ import java.util.List;
import app.fedilab.android.BaseMainActivity; import app.fedilab.android.BaseMainActivity;
import app.fedilab.android.R; import app.fedilab.android.R;
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.activities.ProfileActivity; import app.fedilab.android.activities.ProfileActivity;
import app.fedilab.android.client.entities.api.Account; import app.fedilab.android.client.entities.api.Account;
import app.fedilab.android.databinding.DrawerAccountBinding; import app.fedilab.android.databinding.DrawerAccountBinding;
@ -56,12 +57,19 @@ public class AccountAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
private final List<Account> accountList; private final List<Account> accountList;
private Context context; private Context context;
private final boolean home_mute;
public AccountAdapter(List<Account> accountList, boolean home_mute) {
this.accountList = accountList;
this.home_mute = home_mute;
}
public AccountAdapter(List<Account> accountList) { public AccountAdapter(List<Account> accountList) {
this.accountList = accountList; this.accountList = accountList;
this.home_mute = false;
} }
public static void accountManagement(Context context, AccountViewHolder accountViewHolder, Account account, int position, RecyclerView.Adapter<RecyclerView.ViewHolder> adapter) { public static void accountManagement(Context context, AccountViewHolder accountViewHolder, Account account, int position, RecyclerView.Adapter<RecyclerView.ViewHolder> adapter, boolean home_mute) {
MastodonHelper.loadPPMastodon(accountViewHolder.binding.avatar, account); MastodonHelper.loadPPMastodon(accountViewHolder.binding.avatar, account);
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
@ -73,6 +81,21 @@ public class AccountAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
accountViewHolder.binding.dividerCard.setVisibility(View.GONE); accountViewHolder.binding.dividerCard.setVisibility(View.GONE);
} }
if (home_mute) {
accountViewHolder.binding.muteHome.setVisibility(View.VISIBLE);
boolean muted = MainActivity.filteredAccounts != null && MainActivity.filteredAccounts.contains(account);
accountViewHolder.binding.muteHome.setChecked(muted);
accountViewHolder.binding.muteHome.setOnClickListener(v -> {
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 -> { accountViewHolder.binding.avatar.setOnClickListener(v -> {
Intent intent = new Intent(context, ProfileActivity.class); Intent intent = new Intent(context, ProfileActivity.class);
Bundle b = new Bundle(); Bundle b = new Bundle();
@ -263,7 +286,7 @@ public class AccountAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) { public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
Account account = accountList.get(position); Account account = accountList.get(position);
AccountViewHolder holder = (AccountViewHolder) viewHolder; AccountViewHolder holder = (AccountViewHolder) viewHolder;
accountManagement(context, holder, account, position, this); accountManagement(context, holder, account, position, this, home_mute);
} }

View File

@ -28,7 +28,6 @@ import static app.fedilab.android.helper.Helper.ARG_TIMELINE_REFRESH_ALL;
import static app.fedilab.android.helper.Helper.PREF_USER_ID; import static app.fedilab.android.helper.Helper.PREF_USER_ID;
import static app.fedilab.android.helper.Helper.PREF_USER_INSTANCE; import static app.fedilab.android.helper.Helper.PREF_USER_INSTANCE;
import static app.fedilab.android.helper.Helper.PREF_USER_TOKEN; import static app.fedilab.android.helper.Helper.PREF_USER_TOKEN;
import static app.fedilab.android.helper.Helper.addMutedAccount;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
@ -1663,9 +1662,6 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
builderInner.setNeutralButton(R.string.cancel, (dialog, which) -> dialog.dismiss()); builderInner.setNeutralButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
builderInner.setPositiveButton(R.string.action_mute, (dialog, which) -> accountsVM.muteHome(currentAccount, statusToDeal.account) builderInner.setPositiveButton(R.string.action_mute, (dialog, which) -> accountsVM.muteHome(currentAccount, statusToDeal.account)
.observe((LifecycleOwner) context, account -> { .observe((LifecycleOwner) context, account -> {
if (account != null) {
addMutedAccount(account);
}
sendAction(context, Helper.ARG_STATUS_ACCOUNT_ID_DELETED, null, statusToDeal.account.id); 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(); Toasty.info(context, context.getString(R.string.toast_mute), Toasty.LENGTH_LONG).show();
})); }));

View File

@ -203,7 +203,7 @@ public class FragmentMastodonAccount extends Fragment {
} }
this.accounts = accounts.accounts; 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; flagLoading = accounts.pagination.max_id == null;
LinearLayoutManager mLayoutManager = new LinearLayoutManager(requireActivity()); LinearLayoutManager mLayoutManager = new LinearLayoutManager(requireActivity());
binding.recyclerView.setLayoutManager(mLayoutManager); binding.recyclerView.setLayoutManager(mLayoutManager);

View File

@ -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, * You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
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 static app.fedilab.android.ui.drawer.StatusAdapter.sendAction;
import android.app.Application; import android.app.Application;
@ -95,6 +97,8 @@ public class AccountsVM extends AndroidViewModel {
private MutableLiveData<Token> tokenMutableLiveData; private MutableLiveData<Token> tokenMutableLiveData;
private MutableLiveData<Domains> domainsMutableLiveData; private MutableLiveData<Domains> domainsMutableLiveData;
private MutableLiveData<Report> reportMutableLiveData; private MutableLiveData<Report> reportMutableLiveData;
private MutableLiveData<Boolean> booleanMutableLiveData;
public AccountsVM(@NonNull Application application) { public AccountsVM(@NonNull Application application) {
super(application); super(application);
@ -784,8 +788,8 @@ public class AccountsVM extends AndroidViewModel {
* *
* @return {@link LiveData} containing the {@link Account} to the given account * @return {@link LiveData} containing the {@link Account} to the given account
*/ */
public LiveData<Account> isMuted(@NonNull BaseAccount forAccount, @NonNull Account target) { public LiveData<Boolean> isMuted(@NonNull BaseAccount forAccount, @NonNull Account target) {
accountMutableLiveData = new MutableLiveData<>(); booleanMutableLiveData = new MutableLiveData<>();
new Thread(() -> { new Thread(() -> {
boolean isMuted = false; boolean isMuted = false;
try { try {
@ -795,10 +799,10 @@ public class AccountsVM extends AndroidViewModel {
} }
Handler mainHandler = new Handler(Looper.getMainLooper()); Handler mainHandler = new Handler(Looper.getMainLooper());
boolean finalIsMuted = isMuted; boolean finalIsMuted = isMuted;
Runnable myRunnable = () -> accountMutableLiveData.setValue(finalIsMuted ? target : null); Runnable myRunnable = () -> booleanMutableLiveData.setValue(finalIsMuted);
mainHandler.post(myRunnable); mainHandler.post(myRunnable);
}).start(); }).start();
return accountMutableLiveData; return booleanMutableLiveData;
} }
/** /**
@ -812,6 +816,7 @@ public class AccountsVM extends AndroidViewModel {
try { try {
new MutedAccounts(getApplication().getApplicationContext()).muteAccount(forAccount, target); new MutedAccounts(getApplication().getApplicationContext()).muteAccount(forAccount, target);
addMutedAccount(target);
} catch (DBException e) { } catch (DBException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -832,6 +837,7 @@ public class AccountsVM extends AndroidViewModel {
new Thread(() -> { new Thread(() -> {
try { try {
new MutedAccounts(getApplication().getApplicationContext()).unMuteAccount(forAccount, target); new MutedAccounts(getApplication().getApplicationContext()).unMuteAccount(forAccount, target);
removeMutedAccount(target);
} catch (DBException e) { } catch (DBException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -116,6 +116,18 @@
app:iconPadding="0dp" app:iconPadding="0dp"
app:strokeWidth="1dp" /> app:strokeWidth="1dp" />
<com.google.android.material.button.MaterialButton
android:id="@+id/mute_home"
style="@style/Widget.Material3.Button.OutlinedButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="0dp"
android:visibility="gone"
app:icon="@drawable/ic_baseline_home_24"
app:iconGravity="textStart"
app:iconPadding="0dp"
app:strokeWidth="1dp" />
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/mute_timed" android:id="@+id/mute_timed"
style="@style/Widget.Material3.Button.OutlinedButton" style="@style/Widget.Material3.Button.OutlinedButton"