Merge pull request #442 from FineFindus/feat/account-switcher
feat: use AccountSwitcherSheet for account picker
This commit is contained in:
@@ -42,7 +42,11 @@ public class ExternalShareActivity extends FragmentStackActivity{
|
|||||||
Toast.makeText(this, R.string.err_not_logged_in, Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, R.string.err_not_logged_in, Toast.LENGTH_SHORT).show();
|
||||||
finish();
|
finish();
|
||||||
} else if (isOpenable || sessions.size() > 1) {
|
} 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) -> {
|
sheet.setOnClick((accountId, open) -> {
|
||||||
if (open && text.isPresent()) {
|
if (open && text.isPresent()) {
|
||||||
BiConsumer<Class<? extends Fragment>, Bundle> callback = (clazz, args) -> {
|
BiConsumer<Class<? extends Fragment>, Bundle> callback = (clazz, args) -> {
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ import org.joinmastodon.android.ui.views.CheckableRelativeLayout;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -63,7 +62,7 @@ import me.grishka.appkit.views.UsableRecyclerView;
|
|||||||
public class AccountSwitcherSheet extends BottomSheet{
|
public class AccountSwitcherSheet extends BottomSheet{
|
||||||
private final Activity activity;
|
private final Activity activity;
|
||||||
private final HomeFragment fragment;
|
private final HomeFragment fragment;
|
||||||
private final boolean externalShare, openInApp;
|
private final boolean accountChooser, openInApp;
|
||||||
private BiConsumer<String, Boolean> onClick;
|
private BiConsumer<String, Boolean> onClick;
|
||||||
private UsableRecyclerView list;
|
private UsableRecyclerView list;
|
||||||
private List<WrappedAccount> accounts;
|
private List<WrappedAccount> accounts;
|
||||||
@@ -72,17 +71,22 @@ public class AccountSwitcherSheet extends BottomSheet{
|
|||||||
private Runnable onLoggedOutCallback;
|
private Runnable onLoggedOutCallback;
|
||||||
|
|
||||||
public AccountSwitcherSheet(@NonNull Activity activity, @Nullable HomeFragment fragment){
|
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);
|
super(activity);
|
||||||
this.activity=activity;
|
this.activity=activity;
|
||||||
this.fragment=fragment;
|
this.fragment=fragment;
|
||||||
this.externalShare = externalShare;
|
this.accountChooser=headerTitle!=0;
|
||||||
this.openInApp = openInApp;
|
// 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);
|
list=new UsableRecyclerView(activity);
|
||||||
imgLoader=new ListImageLoaderWrapper(activity, list, new RecyclerViewDelegate(list), null);
|
imgLoader=new ListImageLoaderWrapper(activity, list, new RecyclerViewDelegate(list), null);
|
||||||
@@ -96,20 +100,21 @@ public class AccountSwitcherSheet extends BottomSheet{
|
|||||||
|
|
||||||
adapter.addAdapter(new SingleViewRecyclerAdapter(handle));
|
adapter.addAdapter(new SingleViewRecyclerAdapter(handle));
|
||||||
|
|
||||||
if (externalShare) {
|
if (accountChooser) {
|
||||||
FrameLayout shareHeading = new FrameLayout(activity);
|
FrameLayout shareHeading = new FrameLayout(activity);
|
||||||
activity.getLayoutInflater().inflate(R.layout.item_external_share_heading, shareHeading);
|
activity.getLayoutInflater().inflate(R.layout.item_external_share_heading, shareHeading);
|
||||||
((TextView) shareHeading.findViewById(R.id.title)).setText(openInApp
|
((ImageView) shareHeading.findViewById(R.id.icon)).setImageDrawable(getContext().getDrawable(headerIcon));
|
||||||
? R.string.sk_external_share_or_open_title
|
((TextView) shareHeading.findViewById(R.id.title)).setText(getContext().getString(headerTitle));
|
||||||
: R.string.sk_external_share_title);
|
|
||||||
adapter.addAdapter(new SingleViewRecyclerAdapter(shareHeading));
|
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());
|
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), () -> {
|
adapter.addAdapter(new ClickableSingleViewRecyclerAdapter(makeSimpleListItem(R.string.add_account, R.drawable.ic_fluent_add_24_regular), () -> {
|
||||||
Nav.go(activity, CustomWelcomeFragment.class, null);
|
Nav.go(activity, CustomWelcomeFragment.class, null);
|
||||||
dismiss();
|
dismiss();
|
||||||
@@ -302,9 +307,9 @@ public class AccountSwitcherSheet extends BottomSheet{
|
|||||||
public void onBind(AccountSession item){
|
public void onBind(AccountSession item){
|
||||||
HtmlParser.setTextWithCustomEmoji(name, item.self.getDisplayName(), item.self.emojis);
|
HtmlParser.setTextWithCustomEmoji(name, item.self.getDisplayName(), item.self.emojis);
|
||||||
username.setText(item.getFullUsername());
|
username.setText(item.getFullUsername());
|
||||||
radioButton.setVisibility(externalShare ? View.GONE : View.VISIBLE);
|
radioButton.setVisibility(accountChooser ? View.GONE : View.VISIBLE);
|
||||||
extraBtnWrap.setVisibility(externalShare && openInApp ? View.VISIBLE : View.GONE);
|
extraBtnWrap.setVisibility(accountChooser && openInApp ? View.VISIBLE : View.GONE);
|
||||||
if (externalShare) view.setCheckable(false);
|
if (accountChooser) view.setCheckable(false);
|
||||||
else {
|
else {
|
||||||
String accountId = fragment != null
|
String accountId = fragment != null
|
||||||
? fragment.getAccountID()
|
? fragment.getAccountID()
|
||||||
@@ -348,7 +353,7 @@ public class AccountSwitcherSheet extends BottomSheet{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onLongClick(){
|
public boolean onLongClick(){
|
||||||
if (externalShare) return false;
|
if (accountChooser) return false;
|
||||||
confirmLogOut(item.getID());
|
confirmLogOut(item.getID());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -131,6 +131,7 @@ import org.joinmastodon.android.model.Searchable;
|
|||||||
import org.joinmastodon.android.model.Status;
|
import org.joinmastodon.android.model.Status;
|
||||||
import org.joinmastodon.android.ui.M3AlertDialogBuilder;
|
import org.joinmastodon.android.ui.M3AlertDialogBuilder;
|
||||||
import org.joinmastodon.android.ui.Snackbar;
|
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.BlockAccountConfirmationSheet;
|
||||||
import org.joinmastodon.android.ui.sheets.MuteAccountConfirmationSheet;
|
import org.joinmastodon.android.ui.sheets.MuteAccountConfirmationSheet;
|
||||||
import org.joinmastodon.android.ui.text.CustomEmojiSpan;
|
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<AccountSession> sessionConsumer, Consumer<AlertDialog.Builder> transformDialog) {
|
public static void pickAccount(Context context, String exceptFor, @StringRes int titleRes, @DrawableRes int iconRes, Consumer<AccountSession> sessionConsumer, Consumer<AlertDialog.Builder> transformDialog) {
|
||||||
List<AccountSession> sessions = AccountSessionManager.getInstance().getLoggedInAccounts()
|
AccountSwitcherSheet sheet = new AccountSwitcherSheet((Activity) context, null, iconRes, titleRes == 0 ? R.string.choose_account : titleRes, exceptFor, false);
|
||||||
.stream().filter(s -> !s.getID().equals(exceptFor)).collect(Collectors.toList());
|
sheet.setOnClick((accountId, open) ->sessionConsumer.accept(AccountSessionManager.get(accountId)));
|
||||||
|
sheet.show();
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void restartApp() {
|
public static void restartApp() {
|
||||||
|
|||||||
Reference in New Issue
Block a user