feat: add mute timer

This commit is contained in:
FineFindus
2023-03-06 21:55:32 +01:00
parent db0b4fb615
commit 3619be71ab
5 changed files with 113 additions and 9 deletions

View File

@@ -4,8 +4,15 @@ import org.joinmastodon.android.api.MastodonAPIRequest;
import org.joinmastodon.android.model.Relationship;
public class SetAccountMuted extends MastodonAPIRequest<Relationship>{
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;
}
}
}

View File

@@ -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<Relationship> 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<Duration> 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<Status> resultCallback){
confirmDeletePost(activity, accountID, status, resultCallback, false);

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:gravity="center"
android:paddingVertical="12dp">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginEnd="8dp"
android:textColor="?android:textColorPrimary"
android:text="@string/mo_mute_label"
android:textSize="16sp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_weight="0"
android:layout_marginEnd="16dp"
android:maxWidth="140dp"
android:background="@drawable/bg_inline_button"
android:elevation="0dp"
android:ellipsize="middle"
android:fontFamily="sans-serif-medium"
android:singleLine="true"
android:stateListAnimator="@null"
android:textColor="?android:textColorPrimary"
android:textSize="16sp"
android:text="Duration" />
</LinearLayout>

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/duration_indefinite" android:title="@string/mo_duration_indefinite" />
<item android:id="@+id/duration_minutes_5" android:title="@string/mo_duration_minutes_5"/>
<item android:id="@+id/duration_minutes_30" android:title="@string/mo_duration_minutes_30"/>
<item android:id="@+id/duration_hours_1" android:title="@string/mo_duration_hours_1"/>
<item android:id="@+id/duration_hours_6" android:title="@string/mo_duration_hours_6"/>
<item android:id="@+id/duration_days_1" android:title="@string/mo_duration_days_1"/>
<item android:id="@+id/duration_days_3" android:title="@string/mo_duration_days_3"/>
<item android:id="@+id/duration_days_7" android:title="@string/mo_duration_days_7"/>
</menu>

View File

@@ -34,4 +34,16 @@
<string name="mo_fab_compose">Compose</string>
<string name="mo_sending_error">Error publishing</string>
<!-- duration labels-->
<string name="mo_mute_label">Duration:</string>
<string name="mo_duration_indefinite">Indefinite</string>
<string name="mo_duration_minutes_5">5 minutes</string>
<string name="mo_duration_minutes_30">30 minutes</string>
<string name="mo_duration_hours_1">1 hour</string>
<string name="mo_duration_hours_6">6 hours</string>
<string name="mo_duration_days_1">1 day</string>
<string name="mo_duration_days_3">3 days</string>
<string name="mo_duration_days_7">7 day</string>
</resources>