Merge pull request #196 from FineFindus/feat/unfollow-confirmation

Feat: show unfollow confirmation dialog
This commit is contained in:
LucasGGamerM 2023-05-20 09:54:06 -03:00 committed by GitHub
commit dfeba71abe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 49 deletions

View File

@ -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<Boolean> progressCallback, Consumer<Relationship> 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<Boolean> progressCallback, Consumer<Relationship> 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);
}

View File

@ -55,4 +55,6 @@
<string name="mo_download_latest_nightly_release">Download latest nightly release</string>
<string name="mo_load_remote_followers">Load remote profile follows and followers</string>
<string name="mo_mention_reblogger_automatically">Automatically mention account who reblogged the post in replies</string>
<string name="mo_confirm_unfollow_title">Unfollow Account</string>
<string name="mo_confirm_unfollow">Confirm to unfollow %s</string>
</resources>