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
This commit is contained in:
Levi Bard 2022-02-25 18:55:58 +01:00 committed by GitHub
parent fcc9265703
commit f822234995
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 58 deletions

View File

@ -197,6 +197,33 @@ public abstract class BaseActivity extends AppCompatActivity implements Injectab
.show(); .show();
} }
public @Nullable String getOpenAsText() {
List<AccountEntity> 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 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults); super.onRequestPermissionsResult(requestCode, permissions, grantResults);

View File

@ -325,9 +325,9 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje
super.onPostCreate(savedInstanceState) super.onPostCreate(savedInstanceState)
if (intent != null) { if (intent != null) {
val statusUrl = intent.getStringExtra(STATUS_URL) val redirectUrl = intent.getStringExtra(REDIRECT_URL)
if (statusUrl != null) { if (redirectUrl != null) {
viewUrl(statusUrl, PostLookupFallbackBehavior.DISPLAY_ERROR) viewUrl(redirectUrl, PostLookupFallbackBehavior.DISPLAY_ERROR)
} }
} }
} }
@ -834,7 +834,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje
private const val TAG = "MainActivity" // logging tag private const val TAG = "MainActivity" // logging tag
private const val DRAWER_ITEM_ADD_ACCOUNT: Long = -13 private const val DRAWER_ITEM_ADD_ACCOUNT: Long = -13
private const val DRAWER_ITEM_ANNOUNCEMENTS: Long = 14 private const val DRAWER_ITEM_ANNOUNCEMENTS: Long = 14
const val STATUS_URL = "statusUrl" const val REDIRECT_URL = "redirectUrl"
} }
} }

View File

@ -60,9 +60,11 @@ import com.keylesspalace.tusky.ViewTagActivity
import com.keylesspalace.tusky.components.compose.ComposeActivity import com.keylesspalace.tusky.components.compose.ComposeActivity
import com.keylesspalace.tusky.components.report.ReportActivity import com.keylesspalace.tusky.components.report.ReportActivity
import com.keylesspalace.tusky.databinding.ActivityAccountBinding import com.keylesspalace.tusky.databinding.ActivityAccountBinding
import com.keylesspalace.tusky.db.AccountEntity
import com.keylesspalace.tusky.di.ViewModelFactory import com.keylesspalace.tusky.di.ViewModelFactory
import com.keylesspalace.tusky.entity.Account import com.keylesspalace.tusky.entity.Account
import com.keylesspalace.tusky.entity.Relationship import com.keylesspalace.tusky.entity.Relationship
import com.keylesspalace.tusky.interfaces.AccountSelectionListener
import com.keylesspalace.tusky.interfaces.ActionButtonActivity import com.keylesspalace.tusky.interfaces.ActionButtonActivity
import com.keylesspalace.tusky.interfaces.LinkListener import com.keylesspalace.tusky.interfaces.LinkListener
import com.keylesspalace.tusky.interfaces.ReselectableFragment import com.keylesspalace.tusky.interfaces.ReselectableFragment
@ -686,6 +688,14 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidI
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.account_toolbar, menu) 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) { if (!viewModel.isSelf) {
val block = menu.findItem(R.id.action_block) val block = menu.findItem(R.id.action_block)
@ -829,6 +839,18 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidI
} }
return true 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 -> { R.id.action_block -> {
toggleBlock() toggleBlock()
return true return true

View File

@ -40,7 +40,6 @@ import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.androidx.lifecycle.AndroidLifecycleScopeProvider.from import autodispose2.androidx.lifecycle.AndroidLifecycleScopeProvider.from
import autodispose2.autoDispose import autodispose2.autoDispose
import com.keylesspalace.tusky.BaseActivity import com.keylesspalace.tusky.BaseActivity
import com.keylesspalace.tusky.MainActivity
import com.keylesspalace.tusky.R import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.ViewMediaActivity import com.keylesspalace.tusky.ViewMediaActivity
import com.keylesspalace.tusky.components.compose.ComposeActivity import com.keylesspalace.tusky.components.compose.ComposeActivity
@ -228,9 +227,6 @@ class SearchStatusesFragment : SearchFragment<Pair<Status, StatusViewData.Concre
val accountId = status.actionableStatus.account.id val accountId = status.actionableStatus.account.id
val accountUsername = status.actionableStatus.account.username val accountUsername = status.actionableStatus.account.username
val statusUrl = status.actionableStatus.url val statusUrl = status.actionableStatus.url
val accounts = viewModel.getAllAccountsOrderedByActive()
var openAsTitle: String? = null
val loggedInAccountId = viewModel.activeAccount?.accountId val loggedInAccountId = viewModel.activeAccount?.accountId
val popup = PopupMenu(view.context, view) val popup = PopupMenu(view.context, view)
@ -261,17 +257,12 @@ class SearchStatusesFragment : SearchFragment<Pair<Status, StatusViewData.Concre
} }
val openAsItem = popup.menu.findItem(R.id.status_open_as) val openAsItem = popup.menu.findItem(R.id.status_open_as)
when (accounts.size) { val openAsText = bottomSheetActivity?.openAsText
0, 1 -> openAsItem.isVisible = false if (openAsText == null) {
2 -> for (account in accounts) { openAsItem.isVisible = false
if (account !== viewModel.activeAccount) { } else {
openAsTitle = String.format(getString(R.string.action_open_as), account.fullName) openAsItem.title = openAsText
break
}
}
else -> openAsTitle = String.format(getString(R.string.action_open_as), "")
} }
openAsItem.title = openAsTitle
val mutable = statusIsByCurrentUser || accountIsInMentions(viewModel.activeAccount, status.mentions) val mutable = statusIsByCurrentUser || accountIsInMentions(viewModel.activeAccount, status.mentions)
val muteConversationItem = popup.menu.findItem(R.id.status_mute_conversation).apply { val muteConversationItem = popup.menu.findItem(R.id.status_mute_conversation).apply {
@ -396,21 +387,12 @@ class SearchStatusesFragment : SearchFragment<Pair<Status, StatusViewData.Concre
dialogTitle, false, dialogTitle, false,
object : AccountSelectionListener { object : AccountSelectionListener {
override fun onAccountSelected(account: AccountEntity) { override fun onAccountSelected(account: AccountEntity) {
openAsAccount(statusUrl, account) bottomSheetActivity?.openAsAccount(statusUrl, account)
} }
} }
) )
} }
private fun openAsAccount(statusUrl: String, account: AccountEntity) {
viewModel.activeAccount = account
val intent = Intent(context, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
intent.putExtra(MainActivity.STATUS_URL, statusUrl)
startActivity(intent)
(activity as BaseActivity).finishWithoutSlideOutAnimation()
}
private fun downloadAllMedia(status: Status) { private fun downloadAllMedia(status: Status) {
Toast.makeText(context, R.string.downloading_media, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.downloading_media, Toast.LENGTH_SHORT).show()
for ((_, url) in status.attachments) { for ((_, url) in status.attachments) {

View File

@ -41,7 +41,6 @@ import androidx.lifecycle.Lifecycle;
import com.keylesspalace.tusky.BaseActivity; import com.keylesspalace.tusky.BaseActivity;
import com.keylesspalace.tusky.BottomSheetActivity; import com.keylesspalace.tusky.BottomSheetActivity;
import com.keylesspalace.tusky.MainActivity;
import com.keylesspalace.tusky.PostLookupFallbackBehavior; import com.keylesspalace.tusky.PostLookupFallbackBehavior;
import com.keylesspalace.tusky.R; import com.keylesspalace.tusky.R;
import com.keylesspalace.tusky.ViewMediaActivity; import com.keylesspalace.tusky.ViewMediaActivity;
@ -162,8 +161,6 @@ public abstract class SFragment extends Fragment implements Injectable {
final String accountId = status.getActionableStatus().getAccount().getId(); final String accountId = status.getActionableStatus().getAccount().getId();
final String accountUsername = status.getActionableStatus().getAccount().getUsername(); final String accountUsername = status.getActionableStatus().getAccount().getUsername();
final String statusUrl = status.getActionableStatus().getUrl(); final String statusUrl = status.getActionableStatus().getUrl();
List<AccountEntity> accounts = accountManager.getAllAccountsOrderedByActive();
String openAsTitle = null;
String loggedInAccountId = null; String loggedInAccountId = null;
AccountEntity activeAccount = accountManager.getActiveAccount(); AccountEntity activeAccount = accountManager.getActiveAccount();
@ -201,24 +198,12 @@ public abstract class SFragment extends Fragment implements Injectable {
Menu menu = popup.getMenu(); Menu menu = popup.getMenu();
MenuItem openAsItem = menu.findItem(R.id.status_open_as); MenuItem openAsItem = menu.findItem(R.id.status_open_as);
switch (accounts.size()) { String openAsText = ((BaseActivity)getActivity()).getOpenAsText();
case 0: if (openAsText == null) {
case 1: openAsItem.setVisible(false);
openAsItem.setVisible(false); } else {
break; openAsItem.setTitle(openAsText);
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);
MenuItem muteConversationItem = menu.findItem(R.id.status_mute_conversation); MenuItem muteConversationItem = menu.findItem(R.id.status_mute_conversation);
boolean mutable = statusIsByCurrentUser || accountIsInMentions(activeAccount, status.getMentions()); boolean mutable = statusIsByCurrentUser || accountIsInMentions(activeAccount, status.getMentions());
@ -456,18 +441,9 @@ public abstract class SFragment extends Fragment implements Injectable {
.show(); .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) { private void showOpenAsDialog(String statusUrl, CharSequence dialogTitle) {
BaseActivity activity = (BaseActivity) getActivity(); 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) { private void downloadAllMedia(Status status) {

View File

@ -6,6 +6,10 @@
android:title="@string/action_open_in_web" android:title="@string/action_open_in_web"
app:showAsAction="never" /> app:showAsAction="never" />
<item android:id="@+id/action_open_as"
android:title="@string/action_open_as"
app:showAsAction="never" />
<item android:id="@+id/action_mute" <item android:id="@+id/action_mute"
android:title="@string/action_mute" android:title="@string/action_mute"
app:showAsAction="never" /> app:showAsAction="never" />