From 91fba293f2d2bbeeb55944c471a9a5e17c2d9249 Mon Sep 17 00:00:00 2001 From: Levi Bard Date: Wed, 16 Jan 2019 20:50:09 +0100 Subject: [PATCH] Implement "Open status as (another account)" #958 (#988) --- .../com/keylesspalace/tusky/MainActivity.java | 13 ++++ .../tusky/fragment/SFragment.java | 66 +++++++++++++++++++ app/src/main/res/menu/status_more.xml | 3 + .../main/res/menu/status_more_for_user.xml | 3 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 86 insertions(+) diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java index e8e4bdff..e4a7b32b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java @@ -94,6 +94,7 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut private static final long DRAWER_ITEM_ABOUT = 7; private static final long DRAWER_ITEM_LOG_OUT = 8; private static final long DRAWER_ITEM_FOLLOW_REQUESTS = 9; + public static final String STATUS_URL = "statusUrl"; @Inject public DispatchingAndroidInjector fragmentInjector; @@ -273,6 +274,18 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut return super.onKeyDown(keyCode, event); } + @Override + public void onPostCreate(Bundle savedInstanceState) { + super.onPostCreate(savedInstanceState); + Intent intent = getIntent(); + if (intent != null) { + String statusUrl = intent.getStringExtra(STATUS_URL); + if (statusUrl != null) { + viewUrl(statusUrl); + } + } + } + private void tintTab(TabLayout.Tab tab, boolean tinted) { int color = (tinted) ? R.attr.tab_icon_selected_tint : R.attr.toolbar_icon_tint; ThemeUtils.setDrawableTint(this, tab.getIcon(), color); diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java index 8a7c5606..ea144cfb 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java @@ -19,12 +19,16 @@ import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; + import android.text.Spanned; import android.view.Menu; +import android.view.MenuItem; import android.view.View; +import com.keylesspalace.tusky.BaseActivity; import com.keylesspalace.tusky.BottomSheetActivity; import com.keylesspalace.tusky.ComposeActivity; +import com.keylesspalace.tusky.MainActivity; import com.keylesspalace.tusky.R; import com.keylesspalace.tusky.ReportActivity; import com.keylesspalace.tusky.ViewMediaActivity; @@ -139,6 +143,9 @@ public abstract class SFragment extends BaseFragment { final String accountUsername = status.getActionableStatus().getAccount().getUsername(); final Spanned content = status.getActionableStatus().getContent(); final String statusUrl = status.getActionableStatus().getUrl(); + List accounts = accountManager.getAllAccountsOrderedByActive(); + String openAsTitle = null; + String loggedInAccountId = null; AccountEntity activeAccount = accountManager.getActiveAccount(); if(activeAccount != null) { @@ -169,6 +176,28 @@ public abstract class SFragment extends BaseFragment { } } } + + Menu menu = popup.getMenu(); + MenuItem openAsItem = menu.findItem(R.id.status_open_as); + switch(accounts.size()) { + case 0: + case 1: + openAsItem.setVisible(false); + break; + case 2: + for (AccountEntity account : accounts) { + if (account != activeAccount) { + openAsTitle = String.format(getString(R.string.action_open_as), account.getFullName()); + break; + } + } + break; + default: + openAsTitle = String.format(getString(R.string.action_open_as), "…"); + break; + } + openAsItem.setTitle(openAsTitle); + popup.setOnMenuItemClickListener(item -> { switch (item.getItemId()) { case R.id.status_share_content: { @@ -201,6 +230,10 @@ public abstract class SFragment extends BaseFragment { clipboard.setPrimaryClip(clip); return true; } + case R.id.status_open_as: { + showOpenAsDialog(statusUrl, item.getTitle()); + return true; + } case R.id.status_mute: { timelineCases().mute(accountId); return true; @@ -293,4 +326,37 @@ public abstract class SFragment extends BaseFragment { .setNegativeButton(android.R.string.cancel, null) .show(); } + + private void openAsAccount(String statusUrl, AccountEntity account) { + accountManager.setActiveAccount(account); + Intent intent = new Intent(getContext(), MainActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + intent.putExtra(MainActivity.STATUS_URL, statusUrl); + startActivity(intent); + ((BaseActivity)getActivity()).finishWithoutSlideOutAnimation(); + } + + private void showOpenAsDialog(String statusUrl, CharSequence dialogTitle) { + List accounts = accountManager.getAllAccountsOrderedByActive(); + AccountEntity activeAccount = accountManager.getActiveAccount(); + + if (accounts.size() == 2) { + for (AccountEntity account : accounts) { + if (activeAccount != account) { + openAsAccount(statusUrl, account); + break; + } + } + } else { + accounts.remove(activeAccount); + CharSequence[] accountNames = new CharSequence[accounts.size()]; + for (int i = 0; i < accounts.size(); ++i) { + accountNames[i] = accounts.get(i).getFullName(); + } + new AlertDialog.Builder(getActivity()) + .setTitle(dialogTitle) + .setItems(accountNames, (dialogInterface, index) -> openAsAccount(statusUrl, accounts.get(index))) + .show(); + } + } } diff --git a/app/src/main/res/menu/status_more.xml b/app/src/main/res/menu/status_more.xml index 8400116b..e9f78fc0 100644 --- a/app/src/main/res/menu/status_more.xml +++ b/app/src/main/res/menu/status_more.xml @@ -15,6 +15,9 @@ + diff --git a/app/src/main/res/menu/status_more_for_user.xml b/app/src/main/res/menu/status_more_for_user.xml index 222d03e7..2fce654a 100644 --- a/app/src/main/res/menu/status_more_for_user.xml +++ b/app/src/main/res/menu/status_more_for_user.xml @@ -15,6 +15,9 @@ + Downloading %1$s Copy the link + Open as %s Share toot URL to… Share toot to…