Implement "Open status as (another account)" #958 (#988)

This commit is contained in:
Levi Bard 2019-01-16 20:50:09 +01:00 committed by Konrad Pozniak
parent 922f165f95
commit 91fba293f2
5 changed files with 86 additions and 0 deletions

View file

@ -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_ABOUT = 7;
private static final long DRAWER_ITEM_LOG_OUT = 8; private static final long DRAWER_ITEM_LOG_OUT = 8;
private static final long DRAWER_ITEM_FOLLOW_REQUESTS = 9; private static final long DRAWER_ITEM_FOLLOW_REQUESTS = 9;
public static final String STATUS_URL = "statusUrl";
@Inject @Inject
public DispatchingAndroidInjector<Fragment> fragmentInjector; public DispatchingAndroidInjector<Fragment> fragmentInjector;
@ -273,6 +274,18 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut
return super.onKeyDown(keyCode, event); 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) { private void tintTab(TabLayout.Tab tab, boolean tinted) {
int color = (tinted) ? R.attr.tab_icon_selected_tint : R.attr.toolbar_icon_tint; int color = (tinted) ? R.attr.tab_icon_selected_tint : R.attr.toolbar_icon_tint;
ThemeUtils.setDrawableTint(this, tab.getIcon(), color); ThemeUtils.setDrawableTint(this, tab.getIcon(), color);

View file

@ -19,12 +19,16 @@ import android.content.ClipData;
import android.content.ClipboardManager; import android.content.ClipboardManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.text.Spanned; import android.text.Spanned;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import com.keylesspalace.tusky.BaseActivity;
import com.keylesspalace.tusky.BottomSheetActivity; import com.keylesspalace.tusky.BottomSheetActivity;
import com.keylesspalace.tusky.ComposeActivity; import com.keylesspalace.tusky.ComposeActivity;
import com.keylesspalace.tusky.MainActivity;
import com.keylesspalace.tusky.R; import com.keylesspalace.tusky.R;
import com.keylesspalace.tusky.ReportActivity; import com.keylesspalace.tusky.ReportActivity;
import com.keylesspalace.tusky.ViewMediaActivity; import com.keylesspalace.tusky.ViewMediaActivity;
@ -139,6 +143,9 @@ public abstract class SFragment extends BaseFragment {
final String accountUsername = status.getActionableStatus().getAccount().getUsername(); final String accountUsername = status.getActionableStatus().getAccount().getUsername();
final Spanned content = status.getActionableStatus().getContent(); final Spanned content = status.getActionableStatus().getContent();
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();
if(activeAccount != null) { 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 -> { popup.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.status_share_content: { case R.id.status_share_content: {
@ -201,6 +230,10 @@ public abstract class SFragment extends BaseFragment {
clipboard.setPrimaryClip(clip); clipboard.setPrimaryClip(clip);
return true; return true;
} }
case R.id.status_open_as: {
showOpenAsDialog(statusUrl, item.getTitle());
return true;
}
case R.id.status_mute: { case R.id.status_mute: {
timelineCases().mute(accountId); timelineCases().mute(accountId);
return true; return true;
@ -293,4 +326,37 @@ public abstract class SFragment extends BaseFragment {
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
.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) {
List<AccountEntity> 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();
}
}
} }

View file

@ -15,6 +15,9 @@
<item <item
android:id="@+id/status_copy_link" android:id="@+id/status_copy_link"
android:title="@string/action_copy_link" /> android:title="@string/action_copy_link" />
<item
android:id="@+id/status_open_as"
android:title="@string/action_open_as" />
<item <item
android:id="@+id/status_mute" android:id="@+id/status_mute"
android:title="@string/action_mute" /> android:title="@string/action_mute" />

View file

@ -15,6 +15,9 @@
<item <item
android:id="@+id/status_copy_link" android:id="@+id/status_copy_link"
android:title="@string/action_copy_link" /> android:title="@string/action_copy_link" />
<item
android:id="@+id/status_open_as"
android:title="@string/action_open_as" />
<item <item
android:id="@+id/status_reblog_private" android:id="@+id/status_reblog_private"
android:title="@string/reblog_private" android:title="@string/reblog_private"

View file

@ -112,6 +112,7 @@
<string name="download_image">Downloading %1$s</string> <string name="download_image">Downloading %1$s</string>
<string name="action_copy_link">Copy the link</string> <string name="action_copy_link">Copy the link</string>
<string name="action_open_as">Open as %s</string>
<string name="send_status_link_to">Share toot URL to…</string> <string name="send_status_link_to">Share toot URL to…</string>
<string name="send_status_content_to">Share toot to…</string> <string name="send_status_content_to">Share toot to…</string>