From e97a479e65af57b18cbab957814e05244d61897a Mon Sep 17 00:00:00 2001 From: FineFindus Date: Thu, 4 Jul 2024 20:42:29 +0200 Subject: [PATCH] feat: use AccountSwitcherSheet for account picker --- .../android/ExternalShareActivity.java | 6 ++- .../ui/sheets/AccountSwitcherSheet.java | 39 +++++++++++-------- .../android/ui/utils/UiUtils.java | 16 ++------ 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/ExternalShareActivity.java b/mastodon/src/main/java/org/joinmastodon/android/ExternalShareActivity.java index 9c2aef514..2df80dd96 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ExternalShareActivity.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ExternalShareActivity.java @@ -42,7 +42,11 @@ public class ExternalShareActivity extends FragmentStackActivity{ Toast.makeText(this, R.string.err_not_logged_in, Toast.LENGTH_SHORT).show(); finish(); } else if (isOpenable || sessions.size() > 1) { - AccountSwitcherSheet sheet = new AccountSwitcherSheet(this, null, true, isOpenable); + AccountSwitcherSheet sheet = new AccountSwitcherSheet(this, null, R.drawable.ic_fluent_share_28_regular, + isOpenable + ? R.string.sk_external_share_or_open_title + : R.string.sk_external_share_title, + null, isOpenable); sheet.setOnClick((accountId, open) -> { if (open && text.isPresent()) { BiConsumer, Bundle> callback = (clazz, args) -> { diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/sheets/AccountSwitcherSheet.java b/mastodon/src/main/java/org/joinmastodon/android/ui/sheets/AccountSwitcherSheet.java index 71646249e..94731c8a1 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/sheets/AccountSwitcherSheet.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/sheets/AccountSwitcherSheet.java @@ -35,7 +35,6 @@ import org.joinmastodon.android.ui.views.CheckableRelativeLayout; import java.util.ArrayList; import java.util.List; -import java.util.Objects; import java.util.function.BiConsumer; import java.util.stream.Collectors; @@ -63,7 +62,7 @@ import me.grishka.appkit.views.UsableRecyclerView; public class AccountSwitcherSheet extends BottomSheet{ private final Activity activity; private final HomeFragment fragment; - private final boolean externalShare, openInApp; + private final boolean accountChooser, openInApp; private BiConsumer onClick; private UsableRecyclerView list; private List accounts; @@ -72,17 +71,22 @@ public class AccountSwitcherSheet extends BottomSheet{ private Runnable onLoggedOutCallback; public AccountSwitcherSheet(@NonNull Activity activity, @Nullable HomeFragment fragment){ - this(activity, fragment, false, false); + this(activity, fragment, 0, 0, null, false); } - public AccountSwitcherSheet(@NonNull Activity activity, @Nullable HomeFragment fragment, boolean externalShare, boolean openInApp){ + + public AccountSwitcherSheet(@NonNull Activity activity, @Nullable HomeFragment fragment, @DrawableRes int headerIcon, @StringRes int headerTitle, String exceptFor, boolean openInApp){ super(activity); this.activity=activity; this.fragment=fragment; - this.externalShare = externalShare; - this.openInApp = openInApp; + this.accountChooser=headerTitle!=0; + // currently there is only one use case for a end row button (openInApp) + // if more are needed ti should be generified + this.openInApp=openInApp; - accounts=AccountSessionManager.getInstance().getLoggedInAccounts().stream().map(WrappedAccount::new).collect(Collectors.toList()); + accounts=AccountSessionManager.getInstance().getLoggedInAccounts().stream() + .filter(accountSession -> !accountSession.getID().equals(exceptFor)) + .map(WrappedAccount::new).collect(Collectors.toList()); list=new UsableRecyclerView(activity); imgLoader=new ListImageLoaderWrapper(activity, list, new RecyclerViewDelegate(list), null); @@ -96,20 +100,21 @@ public class AccountSwitcherSheet extends BottomSheet{ adapter.addAdapter(new SingleViewRecyclerAdapter(handle)); - if (externalShare) { + if (accountChooser) { FrameLayout shareHeading = new FrameLayout(activity); activity.getLayoutInflater().inflate(R.layout.item_external_share_heading, shareHeading); - ((TextView) shareHeading.findViewById(R.id.title)).setText(openInApp - ? R.string.sk_external_share_or_open_title - : R.string.sk_external_share_title); + ((ImageView) shareHeading.findViewById(R.id.icon)).setImageDrawable(getContext().getDrawable(headerIcon)); + ((TextView) shareHeading.findViewById(R.id.title)).setText(getContext().getString(headerTitle)); + adapter.addAdapter(new SingleViewRecyclerAdapter(shareHeading)); - setOnDismissListener((d) -> activity.finish()); + // we're using the sheet for interactAs picking, so the activity should not be closed + setOnDismissListener(exceptFor!=null ? null : (d) -> activity.finish()); } adapter.addAdapter(accountsAdapter = new AccountsAdapter()); - if (!externalShare) { + if (!accountChooser) { adapter.addAdapter(new ClickableSingleViewRecyclerAdapter(makeSimpleListItem(R.string.add_account, R.drawable.ic_fluent_add_24_regular), () -> { Nav.go(activity, CustomWelcomeFragment.class, null); dismiss(); @@ -302,9 +307,9 @@ public class AccountSwitcherSheet extends BottomSheet{ public void onBind(AccountSession item){ HtmlParser.setTextWithCustomEmoji(name, item.self.getDisplayName(), item.self.emojis); username.setText(item.getFullUsername()); - radioButton.setVisibility(externalShare ? View.GONE : View.VISIBLE); - extraBtnWrap.setVisibility(externalShare && openInApp ? View.VISIBLE : View.GONE); - if (externalShare) view.setCheckable(false); + radioButton.setVisibility(accountChooser ? View.GONE : View.VISIBLE); + extraBtnWrap.setVisibility(accountChooser && openInApp ? View.VISIBLE : View.GONE); + if (accountChooser) view.setCheckable(false); else { String accountId = fragment != null ? fragment.getAccountID() @@ -348,7 +353,7 @@ public class AccountSwitcherSheet extends BottomSheet{ @Override public boolean onLongClick(){ - if (externalShare) return false; + if (accountChooser) return false; confirmLogOut(item.getID()); return true; } 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 c8473bf60..dd8de0073 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 @@ -131,6 +131,7 @@ import org.joinmastodon.android.model.Searchable; import org.joinmastodon.android.model.Status; import org.joinmastodon.android.ui.M3AlertDialogBuilder; import org.joinmastodon.android.ui.Snackbar; +import org.joinmastodon.android.ui.sheets.AccountSwitcherSheet; import org.joinmastodon.android.ui.sheets.BlockAccountConfirmationSheet; import org.joinmastodon.android.ui.sheets.MuteAccountConfirmationSheet; import org.joinmastodon.android.ui.text.CustomEmojiSpan; @@ -1213,18 +1214,9 @@ public class UiUtils { } public static void pickAccount(Context context, String exceptFor, @StringRes int titleRes, @DrawableRes int iconRes, Consumer sessionConsumer, Consumer transformDialog) { - List sessions = AccountSessionManager.getInstance().getLoggedInAccounts() - .stream().filter(s -> !s.getID().equals(exceptFor)).collect(Collectors.toList()); - - AlertDialog.Builder builder = new M3AlertDialogBuilder(context) - .setItems( - sessions.stream().map(AccountSession::getFullUsername).toArray(String[]::new), - (dialog, which) -> sessionConsumer.accept(sessions.get(which)) - ) - .setTitle(titleRes == 0 ? R.string.choose_account : titleRes) - .setIcon(iconRes); - if (transformDialog != null) transformDialog.accept(builder); - builder.show(); + AccountSwitcherSheet sheet = new AccountSwitcherSheet((Activity) context, null, iconRes, titleRes == 0 ? R.string.choose_account : titleRes, exceptFor, false); + sheet.setOnClick((accountId, open) ->sessionConsumer.accept(AccountSessionManager.get(accountId))); + sheet.show(); } public static void restartApp() {