From f822234995fa3d74a7cc4f9a62f87071f2bc5569 Mon Sep 17 00:00:00 2001 From: Levi Bard Date: Fri, 25 Feb 2022 18:55:58 +0100 Subject: [PATCH] Add "open as" to profiles (#2340) * Extract "open as" utilities to BaseActivity * Add "open as" to profiles. Fixes #2329 * Rename STATUS_URL intent extra key * Add nullability notations for new java code * Pacify ktlint --- .../com/keylesspalace/tusky/BaseActivity.java | 27 ++++++++++++++ .../com/keylesspalace/tusky/MainActivity.kt | 8 ++--- .../components/account/AccountActivity.kt | 22 ++++++++++++ .../fragments/SearchStatusesFragment.kt | 30 ++++------------ .../tusky/fragment/SFragment.java | 36 ++++--------------- app/src/main/res/menu/account_toolbar.xml | 4 +++ 6 files changed, 69 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java index e348f036..8e193c34 100644 --- a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java @@ -197,6 +197,33 @@ public abstract class BaseActivity extends AppCompatActivity implements Injectab .show(); } + public @Nullable String getOpenAsText() { + List accounts = accountManager.getAllAccountsOrderedByActive(); + switch (accounts.size()) { + case 0: + case 1: + return null; + case 2: + for (AccountEntity account : accounts) { + if (account != accountManager.getActiveAccount()) { + return String.format(getString(R.string.action_open_as), account.getFullName()); + } + } + return null; + default: + return String.format(getString(R.string.action_open_as), "…"); + } + } + + public void openAsAccount(@NonNull String url, @NonNull AccountEntity account) { + accountManager.setActiveAccount(account); + Intent intent = new Intent(this, MainActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + intent.putExtra(MainActivity.REDIRECT_URL, url); + startActivity(intent); + finishWithoutSlideOutAnimation(); + } + @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt b/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt index 2290e7d7..e7d748db 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt @@ -325,9 +325,9 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje super.onPostCreate(savedInstanceState) if (intent != null) { - val statusUrl = intent.getStringExtra(STATUS_URL) - if (statusUrl != null) { - viewUrl(statusUrl, PostLookupFallbackBehavior.DISPLAY_ERROR) + val redirectUrl = intent.getStringExtra(REDIRECT_URL) + if (redirectUrl != null) { + viewUrl(redirectUrl, PostLookupFallbackBehavior.DISPLAY_ERROR) } } } @@ -834,7 +834,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje private const val TAG = "MainActivity" // logging tag private const val DRAWER_ITEM_ADD_ACCOUNT: Long = -13 private const val DRAWER_ITEM_ANNOUNCEMENTS: Long = 14 - const val STATUS_URL = "statusUrl" + const val REDIRECT_URL = "redirectUrl" } } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt index a7e9abf1..064b43e6 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt @@ -60,9 +60,11 @@ import com.keylesspalace.tusky.ViewTagActivity import com.keylesspalace.tusky.components.compose.ComposeActivity import com.keylesspalace.tusky.components.report.ReportActivity import com.keylesspalace.tusky.databinding.ActivityAccountBinding +import com.keylesspalace.tusky.db.AccountEntity import com.keylesspalace.tusky.di.ViewModelFactory import com.keylesspalace.tusky.entity.Account import com.keylesspalace.tusky.entity.Relationship +import com.keylesspalace.tusky.interfaces.AccountSelectionListener import com.keylesspalace.tusky.interfaces.ActionButtonActivity import com.keylesspalace.tusky.interfaces.LinkListener import com.keylesspalace.tusky.interfaces.ReselectableFragment @@ -686,6 +688,14 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidI override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.account_toolbar, menu) + val openAsItem = menu.findItem(R.id.action_open_as) + val title = openAsText + if (title == null) { + openAsItem.isVisible = false + } else { + openAsItem.title = title + } + if (!viewModel.isSelf) { val block = menu.findItem(R.id.action_block) @@ -829,6 +839,18 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidI } return true } + R.id.action_open_as -> { + if (loadedAccount != null) { + showAccountChooserDialog( + item.title, false, + object : AccountSelectionListener { + override fun onAccountSelected(account: AccountEntity) { + openAsAccount(loadedAccount!!.url, account) + } + } + ) + } + } R.id.action_block -> { toggleBlock() return true diff --git a/app/src/main/java/com/keylesspalace/tusky/components/search/fragments/SearchStatusesFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/search/fragments/SearchStatusesFragment.kt index 4b5f6717..094b02e7 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/search/fragments/SearchStatusesFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/search/fragments/SearchStatusesFragment.kt @@ -40,7 +40,6 @@ import androidx.recyclerview.widget.LinearLayoutManager import autodispose2.androidx.lifecycle.AndroidLifecycleScopeProvider.from import autodispose2.autoDispose import com.keylesspalace.tusky.BaseActivity -import com.keylesspalace.tusky.MainActivity import com.keylesspalace.tusky.R import com.keylesspalace.tusky.ViewMediaActivity import com.keylesspalace.tusky.components.compose.ComposeActivity @@ -228,9 +227,6 @@ class SearchStatusesFragment : SearchFragment openAsItem.isVisible = false - 2 -> for (account in accounts) { - if (account !== viewModel.activeAccount) { - openAsTitle = String.format(getString(R.string.action_open_as), account.fullName) - break - } - } - else -> openAsTitle = String.format(getString(R.string.action_open_as), "…") + val openAsText = bottomSheetActivity?.openAsText + if (openAsText == null) { + openAsItem.isVisible = false + } else { + openAsItem.title = openAsText } - openAsItem.title = openAsTitle val mutable = statusIsByCurrentUser || accountIsInMentions(viewModel.activeAccount, status.mentions) val muteConversationItem = popup.menu.findItem(R.id.status_mute_conversation).apply { @@ -396,21 +387,12 @@ class SearchStatusesFragment : SearchFragment accounts = accountManager.getAllAccountsOrderedByActive(); - String openAsTitle = null; String loggedInAccountId = null; AccountEntity activeAccount = accountManager.getActiveAccount(); @@ -201,24 +198,12 @@ public abstract class SFragment extends Fragment implements Injectable { 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; + String openAsText = ((BaseActivity)getActivity()).getOpenAsText(); + if (openAsText == null) { + openAsItem.setVisible(false); + } else { + openAsItem.setTitle(openAsText); } - openAsItem.setTitle(openAsTitle); MenuItem muteConversationItem = menu.findItem(R.id.status_mute_conversation); boolean mutable = statusIsByCurrentUser || accountIsInMentions(activeAccount, status.getMentions()); @@ -456,18 +441,9 @@ public abstract class SFragment extends Fragment implements Injectable { .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) { BaseActivity activity = (BaseActivity) getActivity(); - activity.showAccountChooserDialog(dialogTitle, false, account -> openAsAccount(statusUrl, account)); + activity.showAccountChooserDialog(dialogTitle, false, account -> activity.openAsAccount(statusUrl, account)); } private void downloadAllMedia(Status status) { diff --git a/app/src/main/res/menu/account_toolbar.xml b/app/src/main/res/menu/account_toolbar.xml index d25bcdc1..8e0dc242 100644 --- a/app/src/main/res/menu/account_toolbar.xml +++ b/app/src/main/res/menu/account_toolbar.xml @@ -6,6 +6,10 @@ android:title="@string/action_open_in_web" app:showAsAction="never" /> + +