mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
Merge pull request #3191 from deltachat/issue-3190
replace trash icon in profile switcher with context menu
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.thoughtcrime.securesms.accounts;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.view.LayoutInflater;
|
||||
@@ -20,6 +21,7 @@ import org.thoughtcrime.securesms.mms.GlideRequests;
|
||||
|
||||
public class AccountSelectionListAdapter extends RecyclerView.Adapter
|
||||
{
|
||||
private final @NonNull AccountSelectionListFragment fragment;
|
||||
private final @NonNull Context context;
|
||||
private final @NonNull DcAccounts accounts;
|
||||
private @NonNull int[] accountList = new int[0];
|
||||
@@ -39,7 +41,7 @@ public class AccountSelectionListAdapter extends RecyclerView.Adapter
|
||||
super(itemView);
|
||||
}
|
||||
|
||||
public abstract void bind(@NonNull GlideRequests glideRequests, int accountId, DcContact self, String name, String addr, int unreadCount, boolean selected, boolean isMuted);
|
||||
public abstract void bind(@NonNull GlideRequests glideRequests, int accountId, DcContact self, String name, String addr, int unreadCount, boolean selected, boolean isMuted, AccountSelectionListFragment fragment);
|
||||
public abstract void unbind(@NonNull GlideRequests glideRequests);
|
||||
}
|
||||
|
||||
@@ -53,19 +55,14 @@ public class AccountSelectionListAdapter extends RecyclerView.Adapter
|
||||
clickListener.onItemClick(getView());
|
||||
}
|
||||
});
|
||||
getView().getDeleteBtn().setOnClickListener(view -> {
|
||||
if (clickListener != null) {
|
||||
clickListener.onDeleteButtonClick(getView().getAccountId());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public AccountSelectionListItem getView() {
|
||||
return (AccountSelectionListItem) itemView;
|
||||
}
|
||||
|
||||
public void bind(@NonNull GlideRequests glideRequests, int accountId, DcContact self, String name, String addr, int unreadCount, boolean selected, boolean isMuted) {
|
||||
getView().bind(glideRequests, accountId, self, name, addr, unreadCount, selected, isMuted);
|
||||
public void bind(@NonNull GlideRequests glideRequests, int accountId, DcContact self, String name, String addr, int unreadCount, boolean selected, boolean isMuted, AccountSelectionListFragment fragment) {
|
||||
getView().bind(glideRequests, accountId, self, name, addr, unreadCount, selected, isMuted, fragment);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -74,12 +71,13 @@ public class AccountSelectionListAdapter extends RecyclerView.Adapter
|
||||
}
|
||||
}
|
||||
|
||||
public AccountSelectionListAdapter(@NonNull Context context,
|
||||
public AccountSelectionListAdapter(@NonNull AccountSelectionListFragment fragment,
|
||||
@NonNull GlideRequests glideRequests,
|
||||
@Nullable ItemClickListener clickListener)
|
||||
{
|
||||
super();
|
||||
this.context = context;
|
||||
this.fragment = fragment;
|
||||
this.context = fragment.getActivity();
|
||||
this.accounts = DcHelper.getAccounts(context);
|
||||
this.li = LayoutInflater.from(context);
|
||||
this.glideRequests = glideRequests;
|
||||
@@ -116,12 +114,11 @@ public class AccountSelectionListAdapter extends RecyclerView.Adapter
|
||||
|
||||
ViewHolder holder = (ViewHolder) viewHolder;
|
||||
holder.unbind(glideRequests);
|
||||
holder.bind(glideRequests, id, dcContact, name, addr, unreadCount, id == selectedAccountId, dcContext.isMuted());
|
||||
holder.bind(glideRequests, id, dcContact, name, addr, unreadCount, id == selectedAccountId, dcContext.isMuted(), fragment);
|
||||
}
|
||||
|
||||
public interface ItemClickListener {
|
||||
void onItemClick(AccountSelectionListItem item);
|
||||
void onDeleteButtonClick(int accountId);
|
||||
}
|
||||
|
||||
public void changeData(int[] ids, int selectedAccountId) {
|
||||
|
||||
+70
-19
@@ -4,7 +4,9 @@ import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
@@ -14,12 +16,14 @@ import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.b44t.messenger.DcAccounts;
|
||||
import com.b44t.messenger.DcContext;
|
||||
|
||||
import org.thoughtcrime.securesms.ConnectivityActivity;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.connect.AccountManager;
|
||||
import org.thoughtcrime.securesms.connect.DcHelper;
|
||||
import org.thoughtcrime.securesms.mms.GlideApp;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
import org.thoughtcrime.securesms.util.ViewUtil;
|
||||
|
||||
import static com.b44t.messenger.DcContact.DC_CONTACT_ID_ADD_ACCOUNT;
|
||||
@@ -31,7 +35,7 @@ public class AccountSelectionListFragment extends DialogFragment
|
||||
@Override
|
||||
public void onActivityCreated(Bundle icicle) {
|
||||
super.onActivityCreated(icicle);
|
||||
AccountSelectionListAdapter adapter = new AccountSelectionListAdapter(getActivity(),
|
||||
AccountSelectionListAdapter adapter = new AccountSelectionListAdapter(this,
|
||||
GlideApp.with(getActivity()),
|
||||
new ListClickListener());
|
||||
recyclerView.setAdapter(adapter);
|
||||
@@ -66,6 +70,71 @@ public class AccountSelectionListFragment extends DialogFragment
|
||||
return builder.setView(view).create();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
|
||||
super.onCreateContextMenu(menu, v, menuInfo);
|
||||
requireActivity().getMenuInflater().inflate(R.menu.account_item_context, menu);
|
||||
|
||||
AccountSelectionListItem listItem = (AccountSelectionListItem) v;
|
||||
int accountId = listItem.getAccountId();
|
||||
DcAccounts dcAccounts = DcHelper.getAccounts(getActivity());
|
||||
|
||||
if (accountId == dcAccounts.getSelectedAccount().getAccountId()) {
|
||||
menu.findItem(R.id.delete).setVisible(false);
|
||||
} else {
|
||||
Util.redMenuItem(menu, R.id.delete);
|
||||
}
|
||||
|
||||
if (dcAccounts.getAccount(accountId).isMuted()) {
|
||||
menu.findItem(R.id.menu_mute_notifications).setTitle(R.string.menu_unmute);
|
||||
}
|
||||
|
||||
// hack to make onContextItemSelected() work with DialogFragment,
|
||||
// see https://stackoverflow.com/questions/15929026/oncontextitemselected-does-not-get-called-in-a-dialogfragment
|
||||
MenuItem.OnMenuItemClickListener listener = item -> {
|
||||
onContextItemSelected(item, accountId);
|
||||
return true;
|
||||
};
|
||||
for (int i = 0, n = menu.size(); i < n; i++) {
|
||||
menu.getItem(i).setOnMenuItemClickListener(listener);
|
||||
}
|
||||
// /hack
|
||||
}
|
||||
|
||||
private void onContextItemSelected(MenuItem item, int accountId) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.delete:
|
||||
onDeleteAccount(accountId);
|
||||
break;
|
||||
case R.id.menu_mute_notifications:
|
||||
onToggleMute(accountId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void onDeleteAccount(int accountId) {
|
||||
Activity activity = getActivity();
|
||||
AccountSelectionListFragment.this.dismiss();
|
||||
if (activity == null) return;
|
||||
DcAccounts accounts = DcHelper.getAccounts(activity);
|
||||
new AlertDialog.Builder(activity)
|
||||
.setTitle(accounts.getAccount(accountId).getNameNAddr())
|
||||
.setMessage(R.string.forget_login_confirmation_desktop)
|
||||
.setNegativeButton(R.string.cancel, (dialog, which) -> AccountManager.getInstance().showSwitchAccountMenu(activity))
|
||||
.setPositiveButton(R.string.ok, (dialog2, which2) -> {
|
||||
DcHelper.getNotificationCenter(activity).removeAllNotifiations(accountId);
|
||||
accounts.removeAccount(accountId);
|
||||
AccountManager.getInstance().showSwitchAccountMenu(activity);
|
||||
})
|
||||
.show();
|
||||
}
|
||||
|
||||
private void onToggleMute(int accountId) {
|
||||
DcAccounts dcAccounts = DcHelper.getAccounts(getActivity());
|
||||
DcContext dcContext = dcAccounts.getAccount(accountId);
|
||||
dcContext.setMuted(!dcContext.isMuted());
|
||||
recyclerView.getAdapter().notifyDataSetChanged();
|
||||
}
|
||||
|
||||
private class ListClickListener implements AccountSelectionListAdapter.ItemClickListener {
|
||||
|
||||
@@ -80,24 +149,6 @@ public class AccountSelectionListFragment extends DialogFragment
|
||||
AccountManager.getInstance().switchAccountAndStartActivity(activity, accountId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeleteButtonClick(int accountId) {
|
||||
Activity activity = getActivity();
|
||||
AccountSelectionListFragment.this.dismiss();
|
||||
if (activity == null) return;
|
||||
DcAccounts accounts = DcHelper.getAccounts(activity);
|
||||
new AlertDialog.Builder(activity)
|
||||
.setTitle(accounts.getAccount(accountId).getNameNAddr())
|
||||
.setMessage(R.string.forget_login_confirmation_desktop)
|
||||
.setNegativeButton(R.string.cancel, (dialog, which) -> AccountManager.getInstance().showSwitchAccountMenu(activity))
|
||||
.setPositiveButton(R.string.ok, (dialog2, which2) -> {
|
||||
DcHelper.getNotificationCenter(activity).removeAllNotifiations(accountId);
|
||||
accounts.removeAccount(accountId);
|
||||
AccountManager.getInstance().showSwitchAccountMenu(activity);
|
||||
})
|
||||
.show();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.thoughtcrime.securesms.accounts;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Typeface;
|
||||
@@ -31,7 +32,6 @@ public class AccountSelectionListItem extends LinearLayout {
|
||||
private TextView nameView;
|
||||
private ImageView unreadIndicator;
|
||||
private ImageView checkbox;
|
||||
private ImageButton deleteBtn;
|
||||
|
||||
private int accountId;
|
||||
|
||||
@@ -52,21 +52,17 @@ public class AccountSelectionListItem extends LinearLayout {
|
||||
this.nameView = findViewById(R.id.name);
|
||||
this.unreadIndicator = findViewById(R.id.unread_indicator);
|
||||
this.checkbox = findViewById(R.id.checkbox);
|
||||
this.deleteBtn = findViewById(R.id.delete);
|
||||
|
||||
deleteBtn.setColorFilter(DynamicTheme.isDarkTheme(getContext())? Color.WHITE : Color.BLACK);
|
||||
ViewUtil.setTextViewGravityStart(this.nameView, getContext());
|
||||
}
|
||||
|
||||
public void bind(@NonNull GlideRequests glideRequests, int accountId, DcContact self, String name, String addr, int unreadCount, boolean selected, boolean isMuted) {
|
||||
public void bind(@NonNull GlideRequests glideRequests, int accountId, DcContact self, String name, String addr, int unreadCount, boolean selected, boolean isMuted, AccountSelectionListFragment fragment) {
|
||||
this.accountId = accountId;
|
||||
|
||||
Recipient recipient;
|
||||
if (accountId != DcContact.DC_CONTACT_ID_ADD_ACCOUNT) {
|
||||
deleteBtn.setVisibility(selected? View.INVISIBLE : View.VISIBLE);
|
||||
recipient = new Recipient(getContext(), self, name);
|
||||
} else {
|
||||
deleteBtn.setVisibility(View.GONE);
|
||||
recipient = null;
|
||||
}
|
||||
this.contactPhotoImage.setAvatar(glideRequests, recipient, false);
|
||||
@@ -85,6 +81,12 @@ public class AccountSelectionListItem extends LinearLayout {
|
||||
|
||||
updateUnreadIndicator(unreadCount, isMuted);
|
||||
setText(name, addr);
|
||||
|
||||
if (accountId != DcContact.DC_CONTACT_ID_ADD_ACCOUNT) {
|
||||
fragment.registerForContextMenu(this);
|
||||
} else {
|
||||
fragment.unregisterForContextMenu(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void unbind(GlideRequests glideRequests) {
|
||||
@@ -119,10 +121,6 @@ public class AccountSelectionListItem extends LinearLayout {
|
||||
}
|
||||
}
|
||||
|
||||
public ImageButton getDeleteBtn() {
|
||||
return deleteBtn;
|
||||
}
|
||||
|
||||
public int getAccountId() {
|
||||
return accountId;
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@
|
||||
android:background="?attr/conversation_list_item_background"
|
||||
android:paddingLeft="16dp"
|
||||
android:paddingStart="16dp"
|
||||
android:paddingRight="10dp"
|
||||
android:paddingEnd="10dp">
|
||||
android:paddingRight="16dp"
|
||||
android:paddingEnd="16dp">
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="45dp"
|
||||
@@ -90,15 +90,4 @@
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"/>
|
||||
|
||||
<ImageButton
|
||||
android:id="@+id/delete"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_centerVertical="true"
|
||||
android:padding="6dp"
|
||||
android:contentDescription="@string/delete"
|
||||
android:background="#00ffffff"
|
||||
android:src="@drawable/ic_delete_white_24dp" />
|
||||
|
||||
</org.thoughtcrime.securesms.accounts.AccountSelectionListItem>
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<item android:title="@string/menu_mute"
|
||||
android:id="@+id/menu_mute_notifications"/>
|
||||
|
||||
<item android:title="@string/delete"
|
||||
android:id="@+id/delete"/>
|
||||
|
||||
</menu>
|
||||
Reference in New Issue
Block a user