Allow to mute/unmute from profile

This commit is contained in:
Thomas 2022-12-14 11:21:00 +01:00
parent 5e11438f0c
commit 93fce1f56e
7 changed files with 145 additions and 0 deletions

View file

@ -16,6 +16,7 @@ package app.fedilab.android.activities;
import static app.fedilab.android.BaseMainActivity.currentAccount; import static app.fedilab.android.BaseMainActivity.currentAccount;
import static app.fedilab.android.ui.drawer.StatusAdapter.sendAction;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ClipData; import android.content.ClipData;
@ -117,6 +118,7 @@ public class ProfileActivity extends BaseActivity {
private String mention_str; private String mention_str;
private WellKnownNodeinfo.NodeInfo nodeInfo; private WellKnownNodeinfo.NodeInfo nodeInfo;
private boolean checkRemotely; private boolean checkRemotely;
private boolean homeMuted;
private final BroadcastReceiver broadcast_data = new BroadcastReceiver() { private final BroadcastReceiver broadcast_data = new BroadcastReceiver() {
@Override @Override
@ -144,6 +146,7 @@ public class ProfileActivity extends BaseActivity {
Bundle b = getIntent().getExtras(); Bundle b = getIntent().getExtras();
binding.accountFollow.setEnabled(false); binding.accountFollow.setEnabled(false);
checkRemotely = false; checkRemotely = false;
homeMuted = false;
if (b != null) { if (b != null) {
account = (Account) b.getSerializable(Helper.ARG_ACCOUNT); account = (Account) b.getSerializable(Helper.ARG_ACCOUNT);
account_id = b.getString(Helper.ARG_USER_ID, null); 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(); Toasty.error(ProfileActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
finish(); 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)); 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_endorse).setVisible(false);
menu.findItem(R.id.action_direct_message).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_add_to_list).setVisible(false);
menu.findItem(R.id.action_mute_home).setVisible(false);
} else { } else {
menu.findItem(R.id.action_block).setVisible(true); menu.findItem(R.id.action_block).setVisible(true);
menu.findItem(R.id.action_mute).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_timed_mute).setVisible(true);
menu.findItem(R.id.action_mention).setVisible(true); menu.findItem(R.id.action_mention).setVisible(true);
} }
@ -696,6 +703,7 @@ public class ProfileActivity extends BaseActivity {
if (!relationship.following) { if (!relationship.following) {
menu.findItem(R.id.action_hide_boost).setVisible(false); menu.findItem(R.id.action_hide_boost).setVisible(false);
menu.findItem(R.id.action_endorse).setVisible(false); menu.findItem(R.id.action_endorse).setVisible(false);
menu.findItem(R.id.action_mute_home).setVisible(false);
} }
if (relationship.blocking) { if (relationship.blocking) {
menu.findItem(R.id.action_block).setTitle(R.string.action_unblock); menu.findItem(R.id.action_block).setTitle(R.string.action_unblock);
@ -713,6 +721,11 @@ public class ProfileActivity extends BaseActivity {
} else { } else {
menu.findItem(R.id.action_hide_boost).setTitle(getString(R.string.show_boost, account.username)); 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; return true;
@ -989,6 +1002,27 @@ public class ProfileActivity extends BaseActivity {
}); });
builderInner.show(); 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) { } else if (itemId == R.id.action_timed_mute) {
MastodonHelper.scheduleBoost(ProfileActivity.this, MastodonHelper.ScheduleType.TIMED_MUTED, null, account, rs -> { MastodonHelper.scheduleBoost(ProfileActivity.this, MastodonHelper.ScheduleType.TIMED_MUTED, null, account, rs -> {
this.relationship = rs; this.relationship = rs;

View file

@ -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 * Returns the MutedAccounts for an account
* *

View file

@ -1655,6 +1655,17 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
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();
})); }));
builderInner.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) { } else if (itemId == R.id.action_mute_conversation) {
if (statusToDeal.muted) { 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()); statusesVM.unMute(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, statusToDeal.id).observe((LifecycleOwner) context, status1 -> Toasty.info(context, context.getString(R.string.toast_unmute_conversation)).show());

View file

@ -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.Suggestions;
import app.fedilab.android.client.entities.api.Tag; import app.fedilab.android.client.entities.api.Tag;
import app.fedilab.android.client.entities.api.Token; 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.client.entities.app.StatusCache;
import app.fedilab.android.exception.DBException;
import app.fedilab.android.helper.Helper; import app.fedilab.android.helper.Helper;
import app.fedilab.android.helper.MastodonHelper; import app.fedilab.android.helper.MastodonHelper;
import okhttp3.MultipartBody; import okhttp3.MultipartBody;
@ -748,6 +751,70 @@ public class AccountsVM extends AndroidViewModel {
return relationShipMutableLiveData; return relationShipMutableLiveData;
} }
/**
* Mute the given account in db
*
* @return {@link LiveData} containing the {@link Account} to the given account
*/
public LiveData<Account> 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<Account> 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<Account> 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. * Unmute the given account.
* *

View file

@ -16,6 +16,11 @@
android:icon="@drawable/ic_baseline_open_with_24" android:icon="@drawable/ic_baseline_open_with_24"
android:title="@string/action_open_in_web" android:title="@string/action_open_in_web"
app:showAsAction="never" /> app:showAsAction="never" />
<item
android:id="@+id/action_mute_home"
android:icon="@drawable/ic_baseline_volume_mute_24"
android:title="@string/mute_home"
app:showAsAction="never" />
<item <item
android:id="@+id/action_mute" android:id="@+id/action_mute"
android:icon="@drawable/ic_baseline_volume_mute_24" android:icon="@drawable/ic_baseline_volume_mute_24"

View file

@ -46,6 +46,10 @@
android:id="@+id/action_mute" android:id="@+id/action_mute"
android:title="@string/more_action_1" android:title="@string/more_action_1"
app:showAsAction="never" /> app:showAsAction="never" />
<item
android:id="@+id/action_mute_home"
android:title="@string/mute_home"
app:showAsAction="never" />
<item <item
android:id="@+id/action_timed_mute" android:id="@+id/action_timed_mute"
android:title="@string/more_action_8" android:title="@string/more_action_8"

View file

@ -2059,4 +2059,6 @@
<string name="pin_tag">Pin tag</string> <string name="pin_tag">Pin tag</string>
<string name="unpin_tag">Unpin tag</string> <string name="unpin_tag">Unpin tag</string>
<string name="unfollow_tag">Unfollow tag</string> <string name="unfollow_tag">Unfollow tag</string>
<string name="mute_home">Mute for home</string>
<string name="unmute_home">Unmute for home</string>
</resources> </resources>