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 5e9f8e941..c20d7dc2a 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 @@ -141,7 +141,7 @@ import me.grishka.appkit.utils.V; import okhttp3.MediaType; public class UiUtils { - private static Handler mainHandler = new Handler(Looper.getMainLooper()); + private static final Handler mainHandler = new Handler(Looper.getMainLooper()); private static final DateTimeFormatter DATE_FORMATTER_SHORT_WITH_YEAR = DateTimeFormatter.ofPattern("d MMM uuuu"), DATE_FORMATTER_SHORT = DateTimeFormatter.ofPattern("d MMM"); public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT); public static int MAX_WIDTH; @@ -324,9 +324,8 @@ public class UiUtils { public static void loadCustomEmojiInTextView(TextView view){ CharSequence _text=view.getText(); - if(!(_text instanceof Spanned)) + if(!(_text instanceof Spanned text)) return; - Spanned text=(Spanned)_text; CustomEmojiSpan[] spans=text.getSpans(0, text.length(), CustomEmojiSpan.class); if(spans.length==0) return; @@ -391,12 +390,16 @@ public class UiUtils { showConfirmationAlert(context, context.getString(title), context.getString(message), context.getString(confirmButton), icon, onConfirmed); } - public static void showConfirmationAlert(Context context, CharSequence title, CharSequence message, CharSequence confirmButton, int icon, Runnable onConfirmed){ + public static void showConfirmationAlert(Context context, CharSequence title, CharSequence message, CharSequence confirmButton, int icon, Runnable onConfirmed) { + showConfirmationAlert(context, title, message, confirmButton, icon, onConfirmed, null); + } + + public static void showConfirmationAlert(Context context, CharSequence title, CharSequence message, CharSequence confirmButton, int icon, Runnable onConfirmed, Runnable onDenied){ new M3AlertDialogBuilder(context) .setTitle(title) .setMessage(message) .setPositiveButton(confirmButton, (dlg, i)->onConfirmed.run()) - .setNegativeButton(R.string.cancel, null) + .setNegativeButton(R.string.cancel, (dialog, which) -> onDenied.run()) .setIcon(icon) .show(); } @@ -760,57 +763,59 @@ public class UiUtils { public static void performAccountAction(Activity activity, Account account, String accountID, Relationship relationship, Button button, Consumer progressCallback, Consumer resultCallback) { if(relationship == null){ - UiUtils.lookupAccount(button.getContext(), account, accountID, null, account1 -> { - if(account1 == null){ - return; + UiUtils.lookupAccount(button.getContext(), account, accountID, null, lookUpAccount -> { + if (lookUpAccount != null) { + progressCallback.accept(true); + follow(activity, accountID, lookUpAccount, true, progressCallback, resultCallback); } - progressCallback.accept(true); - new SetAccountFollowed(account1.id, true, true, false) - .setCallback(new Callback<>(){ - @Override - public void onSuccess(Relationship result){ - resultCallback.accept(result); - progressCallback.accept(false); - if(!result.following && !result.requested){ - E.post(new RemoveAccountPostsEvent(accountID, account.id, true)); - } - } - - @Override - public void onError(ErrorResponse error){ - error.showToast(activity); - progressCallback.accept(false); - } - }) - .exec(accountID); }); return; } + if (relationship.blocking) { confirmToggleBlockUser(activity, accountID, account, true, resultCallback); - }else if(relationship.muting){ - confirmToggleMuteUser(activity, accountID, account, true, resultCallback); - }else{ - progressCallback.accept(true); - new SetAccountFollowed(account.id, !relationship.following && !relationship.requested, true, false) - .setCallback(new Callback<>(){ - @Override - public void onSuccess(Relationship result){ - resultCallback.accept(result); - progressCallback.accept(false); - if(!result.following && !result.requested){ - E.post(new RemoveAccountPostsEvent(accountID, account.id, true)); - } - } - - @Override - public void onError(ErrorResponse error){ - error.showToast(activity); - progressCallback.accept(false); - } - }) - .exec(accountID); + return; } + + if(relationship.muting){ + confirmToggleMuteUser(activity, accountID, account, true, resultCallback); + return; + } + + progressCallback.accept(true); + if (!relationship.following && !relationship.requested) { + follow(activity, accountID, account, true, progressCallback, resultCallback); + } else { + showConfirmationAlert(activity, + activity.getString(R.string.mo_confirm_unfollow_title), + activity.getString(R.string.mo_confirm_unfollow, account.getDisplayUsername()), + activity.getString(R.string.unfollow), + 0, + () -> follow(activity, accountID, account, false, progressCallback, resultCallback), + () -> progressCallback.accept(false)); + } + + } + + private static void follow(Activity activity, String accountID, Account account, boolean followed, Consumer progressCallback, Consumer resultCallback) { + new SetAccountFollowed(account.id, followed, true, false) + .setCallback(new Callback<>(){ + @Override + public void onSuccess(Relationship result){ + resultCallback.accept(result); + progressCallback.accept(false); + if(!result.following && !result.requested){ + E.post(new RemoveAccountPostsEvent(accountID, account.id, true)); + } + } + + @Override + public void onError(ErrorResponse error){ + error.showToast(activity); + progressCallback.accept(false); + } + }) + .exec(accountID); } diff --git a/mastodon/src/main/res/values/strings_mo.xml b/mastodon/src/main/res/values/strings_mo.xml index 07a83c40a..da6abf408 100644 --- a/mastodon/src/main/res/values/strings_mo.xml +++ b/mastodon/src/main/res/values/strings_mo.xml @@ -55,4 +55,6 @@ Download latest nightly release Load remote profile follows and followers Automatically mention account who reblogged the post in replies + Unfollow Account + Confirm to unfollow %s \ No newline at end of file