From 3619be71abf14763401222cd640a7bdbb4ce1f14 Mon Sep 17 00:00:00 2001 From: FineFindus <63370021+FineFindus@users.noreply.github.com> Date: Mon, 6 Mar 2023 21:55:32 +0100 Subject: [PATCH] feat: add mute timer --- .../requests/accounts/SetAccountMuted.java | 11 +++- .../android/ui/utils/UiUtils.java | 54 ++++++++++++++++--- .../main/res/layout/item_mute_duration.xml | 34 ++++++++++++ mastodon/src/main/res/menu/mute_duration.xml | 11 ++++ mastodon/src/main/res/values/strings_mo.xml | 12 +++++ 5 files changed, 113 insertions(+), 9 deletions(-) create mode 100644 mastodon/src/main/res/layout/item_mute_duration.xml create mode 100644 mastodon/src/main/res/menu/mute_duration.xml diff --git a/mastodon/src/main/java/org/joinmastodon/android/api/requests/accounts/SetAccountMuted.java b/mastodon/src/main/java/org/joinmastodon/android/api/requests/accounts/SetAccountMuted.java index 17f0a583d..7d6afadf6 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/api/requests/accounts/SetAccountMuted.java +++ b/mastodon/src/main/java/org/joinmastodon/android/api/requests/accounts/SetAccountMuted.java @@ -4,8 +4,15 @@ import org.joinmastodon.android.api.MastodonAPIRequest; import org.joinmastodon.android.model.Relationship; public class SetAccountMuted extends MastodonAPIRequest{ - public SetAccountMuted(String id, boolean muted){ + public SetAccountMuted(String id, boolean muted, long duration){ super(HttpMethod.POST, "/accounts/"+id+"/"+(muted ? "mute" : "unmute"), Relationship.class); - setRequestBody(new Object()); + setRequestBody(new Request(duration)); + } + + private static class Request{ + public long duration; + public Request(long duration){ + this.duration=duration; + } } } diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/utils/UiUtils.java b/mastodon/src/main/java/org/joinmastodon/android/ui/utils/UiUtils.java index 2aa903867..a7f4d2174 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/utils/UiUtils.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/utils/UiUtils.java @@ -36,7 +36,9 @@ import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.TextUtils; import android.util.Log; +import android.view.Gravity; import android.view.HapticFeedbackConstants; +import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.SubMenu; @@ -99,6 +101,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.net.URI; import java.net.URISyntaxException; +import java.time.Duration; import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; @@ -109,6 +112,7 @@ import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiPredicate; import java.util.function.Consumer; import java.util.function.Function; @@ -464,12 +468,45 @@ public class UiUtils{ } public static void confirmToggleMuteUser(Activity activity, String accountID, Account account, boolean currentlyMuted, Consumer resultCallback){ - showConfirmationAlert(activity, activity.getString(currentlyMuted ? R.string.confirm_unmute_title : R.string.confirm_mute_title), - activity.getString(currentlyMuted ? R.string.confirm_unmute : R.string.confirm_mute, account.displayName), - activity.getString(currentlyMuted ? R.string.do_unmute : R.string.do_mute), - currentlyMuted ? R.drawable.ic_fluent_speaker_0_28_regular : R.drawable.ic_fluent_speaker_off_28_regular, - ()->{ - new SetAccountMuted(account.id, !currentlyMuted) + View menu = LayoutInflater.from(activity).inflate(R.layout.item_mute_duration, null); + Button button = menu.findViewById(R.id.button); + + AtomicReference muteDuration = new AtomicReference<>(Duration.ZERO); + + PopupMenu popupMenu=new PopupMenu(activity, button, Gravity.CENTER_HORIZONTAL); + popupMenu.inflate(R.menu.mute_duration); + popupMenu.setOnMenuItemClickListener(item -> { + int id = item.getItemId(); + if (id == R.id.duration_indefinite) + muteDuration.set(Duration.ZERO); + else if (id == R.id.duration_minutes_5) { + muteDuration.set(Duration.ofMinutes(5)); + }else if (id == R.id.duration_minutes_30) { + muteDuration.set(Duration.ofMinutes(30)); + }else if (id == R.id.duration_hours_1) { + muteDuration.set(Duration.ofHours(1)); + }else if (id == R.id.duration_hours_6) { + muteDuration.set(Duration.ofHours(6)); + }else if (id == R.id.duration_days_1) { + muteDuration.set(Duration.ofDays(1)); + }else if (id == R.id.duration_days_3) { + muteDuration.set(Duration.ofDays(3)); + }else if (id == R.id.duration_days_7) { + muteDuration.set(Duration.ofDays(7)); + } + button.setText(item.getTitle()); + return true; + }); + button.setOnTouchListener(popupMenu.getDragToOpenListener()); + button.setOnClickListener(v->popupMenu.show()); + button.setText(popupMenu.getMenu().getItem(0).getTitle()); + + new M3AlertDialogBuilder(activity) + .setTitle(activity.getString(currentlyMuted ? R.string.confirm_unmute_title : R.string.confirm_mute_title)) + .setMessage(activity.getString(currentlyMuted ? R.string.confirm_unmute : R.string.confirm_mute, account.displayName)) + .setView(currentlyMuted ? null : menu) + .setPositiveButton(activity.getString(currentlyMuted ? R.string.do_unmute : R.string.do_mute), (dlg, i)-> { + new SetAccountMuted(account.id, !currentlyMuted, muteDuration.get().getSeconds()) .setCallback(new Callback<>(){ @Override public void onSuccess(Relationship result){ @@ -486,7 +523,10 @@ public class UiUtils{ }) .wrapProgress(activity, R.string.loading, false) .exec(accountID); - }); + }) + .setNegativeButton(R.string.cancel, null) + .setIcon(currentlyMuted ? R.drawable.ic_fluent_speaker_0_28_regular : R.drawable.ic_fluent_speaker_off_28_regular) + .show(); } public static void confirmDeletePost(Activity activity, String accountID, Status status, Consumer resultCallback){ confirmDeletePost(activity, accountID, status, resultCallback, false); diff --git a/mastodon/src/main/res/layout/item_mute_duration.xml b/mastodon/src/main/res/layout/item_mute_duration.xml new file mode 100644 index 000000000..37bbc2e71 --- /dev/null +++ b/mastodon/src/main/res/layout/item_mute_duration.xml @@ -0,0 +1,34 @@ + + + + + +