mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
add option to delete a single contact #37
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<org.thoughtcrime.securesms.contacts.ContactSelectionListItem
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
@@ -8,7 +7,7 @@
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:focusable="true"
|
||||
android:background="@drawable/conversation_item_background"
|
||||
android:background="?attr/conversation_list_item_background"
|
||||
android:paddingLeft="48dp"
|
||||
android:paddingStart="48dp"
|
||||
android:paddingRight="24dp"
|
||||
|
||||
@@ -749,6 +749,7 @@
|
||||
<!-- ContactSelectionListFragment-->
|
||||
<string name="ContactSelectionListFragment_signal_requires_the_contacts_permission_in_order_to_display_your_contacts">Delta Chat requires the Contacts permission in order to display your contacts, but it has been permanently denied. Please continue to the app settings menu, select \"Permissions\", and enable \"Contacts\".</string>
|
||||
<string name="ContactSelectionListFragment_error_retrieving_contacts_check_your_network_connection">Error retrieving contacts, check your network connection</string>
|
||||
<string name="ContactSelectionListFragment_error_deleting_contacts_check_existing_conversations">Error deleting contacts, some contacts couldn\'t get deleted. Please delete existing conversations before.</string>
|
||||
|
||||
<!-- blocked_contacts_fragment -->
|
||||
<string name="blocked_contacts_fragment__no_blocked_contacts">No blocked contacts</string>
|
||||
|
||||
@@ -30,7 +30,12 @@ import android.support.v4.widget.SwipeRefreshLayout;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.Log;
|
||||
import android.util.SparseIntArray;
|
||||
import android.view.ActionMode;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.WindowManager;
|
||||
@@ -59,6 +64,7 @@ import org.thoughtcrime.securesms.util.ViewUtil;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -93,6 +99,8 @@ public class ContactSelectionListFragment extends Fragment
|
||||
private String cursorFilter;
|
||||
private RecyclerView recyclerView;
|
||||
private RecyclerViewFastScroller fastScroller;
|
||||
private ActionMode actionMode;
|
||||
private ActionMode.Callback actionModeCallback;
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle icicle) {
|
||||
@@ -148,6 +156,41 @@ public class ContactSelectionListFragment extends Fragment
|
||||
showContactsDescription = view.findViewById(R.id.show_contacts_description);
|
||||
showContactsProgress = view.findViewById(R.id.progress);
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
|
||||
actionModeCallback = new ActionMode.Callback() {
|
||||
@Override
|
||||
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
|
||||
MenuInflater inflater = getActivity().getMenuInflater();
|
||||
inflater.inflate(R.menu.action_mode_delete, menu);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
getActivity().getWindow().setStatusBarColor(getResources().getColor(R.color.action_mode_status_bar));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
|
||||
switch (menuItem.getItemId()) {
|
||||
case R.id.menu_select_all:
|
||||
handleSelectAll();
|
||||
return true;
|
||||
case R.id.menu_delete_selected:
|
||||
handleDeleteSelected();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyActionMode(ActionMode actionMode) {
|
||||
ContactSelectionListFragment.this.actionMode = null;
|
||||
getContactSelectionListAdapter().resetActionModeSelection();
|
||||
}
|
||||
};
|
||||
|
||||
swipeRefresh.setEnabled(getActivity().getIntent().getBooleanExtra(REFRESHABLE, true) &&
|
||||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN);
|
||||
@@ -155,6 +198,33 @@ public class ContactSelectionListFragment extends Fragment
|
||||
return view;
|
||||
}
|
||||
|
||||
private void handleSelectAll() {
|
||||
getContactSelectionListAdapter().selectAll();
|
||||
}
|
||||
|
||||
private void handleDeleteSelected() {
|
||||
ContactSelectionListAdapter adapter = getContactSelectionListAdapter();
|
||||
SparseIntArray actionModeSelection = adapter.getActionModeSelection();
|
||||
boolean failed = false;
|
||||
for (int index = 0; index < actionModeSelection.size(); index++) {
|
||||
int contactId = actionModeSelection.valueAt(index);
|
||||
boolean currentFailed = DcHelper.getContext(getContext()).deleteContact(contactId) == 0;
|
||||
failed = currentFailed || failed;
|
||||
if (!currentFailed) {
|
||||
adapter.removeActionModeSelection(contactId);
|
||||
}
|
||||
}
|
||||
if (failed) {
|
||||
Toast.makeText(getActivity(), R.string.ContactSelectionListFragment_error_deleting_contacts_check_existing_conversations, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
adapter.resetActionModeSelection();
|
||||
actionMode.finish();
|
||||
}
|
||||
|
||||
private ContactSelectionListAdapter getContactSelectionListAdapter() {
|
||||
return (ContactSelectionListAdapter) recyclerView.getAdapter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
Permissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
|
||||
@@ -302,8 +372,14 @@ public class ContactSelectionListFragment extends Fragment
|
||||
|
||||
private class ListClickListener implements ContactSelectionListAdapter.ItemClickListener {
|
||||
@Override
|
||||
public void onItemClick(ContactSelectionListItem contact)
|
||||
public void onItemClick(ContactSelectionListItem contact, boolean handleActionMode)
|
||||
{
|
||||
if (handleActionMode) {
|
||||
if (actionMode != null) {
|
||||
finishActionModeIfSelectionIsEmpty();
|
||||
}
|
||||
return;
|
||||
}
|
||||
int specialId = contact.getSpecialId();
|
||||
String addr = contact.getNumber();
|
||||
if (!isMulti() || !selectedContacts.contains(addr))
|
||||
@@ -337,9 +413,24 @@ public class ContactSelectionListFragment extends Fragment
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemLongClick(ContactSelectionListItem view) {
|
||||
if (actionMode == null) {
|
||||
actionMode = Objects.requireNonNull(getActivity()).startActionMode(actionModeCallback);
|
||||
} else {
|
||||
finishActionModeIfSelectionIsEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setOnContactSelectedListener(OnContactSelectedListener onContactSelectedListener) {
|
||||
private void finishActionModeIfSelectionIsEmpty() {
|
||||
if (getContactSelectionListAdapter().getActionModeSelection().size() == 0) {
|
||||
actionMode.finish();
|
||||
}
|
||||
}
|
||||
|
||||
public void setOnContactSelectedListener(OnContactSelectedListener onContactSelectedListener) {
|
||||
this.onContactSelectedListener = onContactSelectedListener;
|
||||
}
|
||||
|
||||
|
||||
@@ -410,7 +410,7 @@ public class ConversationListFragment extends Fragment
|
||||
if (archive) inflater.inflate(R.menu.conversation_list_batch_unarchive, menu);
|
||||
else inflater.inflate(R.menu.conversation_list_batch_archive, menu);
|
||||
|
||||
inflater.inflate(R.menu.conversation_list_batch, menu);
|
||||
inflater.inflate(R.menu.action_mode_delete, menu);
|
||||
|
||||
mode.setTitle("1");
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.text.TextUtils;
|
||||
import android.util.SparseIntArray;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@@ -78,14 +79,14 @@ public class ContactSelectionListAdapter extends RecyclerView.Adapter
|
||||
private final ItemClickListener clickListener;
|
||||
private final GlideRequests glideRequests;
|
||||
private final Set<String> selectedContacts = new HashSet<>(); // TODO: maybe better use contact-id here
|
||||
private final SparseIntArray actionModeSelection = new SparseIntArray();
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return dcContactList.length;
|
||||
}
|
||||
|
||||
private
|
||||
@NonNull DcContact getContact(int position) {
|
||||
private @NonNull DcContact getContact(int position) {
|
||||
if(position<0 || position>=dcContactList.length) {
|
||||
return new DcContact(0);
|
||||
}
|
||||
@@ -103,33 +104,93 @@ public class ContactSelectionListAdapter extends RecyclerView.Adapter
|
||||
return fromDb;
|
||||
}
|
||||
|
||||
public void resetActionModeSelection() {
|
||||
actionModeSelection.clear();
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public void selectAll() {
|
||||
actionModeSelection.clear();
|
||||
for(int index = 0; index < dcContactList.length; index++) {
|
||||
int value = dcContactList[index];
|
||||
if (value > 0) {
|
||||
actionModeSelection.put(index, value);
|
||||
}
|
||||
}
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public void removeActionModeSelection(int contactId) {
|
||||
int indexOfContactId = actionModeSelection.indexOfValue(contactId);
|
||||
actionModeSelection.removeAt(indexOfContactId);
|
||||
}
|
||||
|
||||
private boolean isActionModeEnabled() {
|
||||
return actionModeSelection.size() != 0;
|
||||
}
|
||||
|
||||
public abstract static class ViewHolder extends RecyclerView.ViewHolder {
|
||||
|
||||
public ViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
}
|
||||
|
||||
public abstract void bind(@NonNull GlideRequests glideRequests, int type, DcContact contact, String name, String number, String label, int color, boolean multiSelect);
|
||||
public abstract void bind(@NonNull GlideRequests glideRequests, int type, DcContact contact, String name, String number, String label, int color, boolean multiSelect, boolean enabled);
|
||||
public abstract void unbind(@NonNull GlideRequests glideRequests);
|
||||
public abstract void setChecked(boolean checked);
|
||||
}
|
||||
public abstract void setSelected(boolean enabled);
|
||||
public abstract void setEnabled(boolean enabled);
|
||||
}
|
||||
|
||||
public class ContactViewHolder extends ViewHolder {
|
||||
|
||||
public static class ContactViewHolder extends ViewHolder {
|
||||
ContactViewHolder(@NonNull final View itemView,
|
||||
@Nullable final ItemClickListener clickListener)
|
||||
{
|
||||
@Nullable final ItemClickListener clickListener) {
|
||||
super(itemView);
|
||||
itemView.setOnClickListener(v -> {
|
||||
if (clickListener != null) clickListener.onItemClick(getView());
|
||||
itemView.setOnClickListener(view -> {
|
||||
if (clickListener != null) {
|
||||
if (isActionModeEnabled()) {
|
||||
toggleSelection();
|
||||
clickListener.onItemClick(getView(), true);
|
||||
} else {
|
||||
clickListener.onItemClick(getView(), false);
|
||||
}
|
||||
}
|
||||
});
|
||||
itemView.setOnLongClickListener(view -> {
|
||||
if (clickListener != null) {
|
||||
int contactId = getContactId(getAdapterPosition());
|
||||
if (contactId > 0) {
|
||||
toggleSelection();
|
||||
clickListener.onItemLongClick(getView());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
private int getContactId(int adapterPosition) {
|
||||
return ContactSelectionListAdapter.this.dcContactList[adapterPosition];
|
||||
}
|
||||
|
||||
private void toggleSelection() {
|
||||
int adapterPosition = getAdapterPosition();
|
||||
int contactId = getContactId(adapterPosition);
|
||||
boolean enabled = actionModeSelection.indexOfKey(adapterPosition) > -1;
|
||||
if (enabled) {
|
||||
ContactSelectionListAdapter.this.actionModeSelection.delete(adapterPosition);
|
||||
} else {
|
||||
ContactSelectionListAdapter.this.actionModeSelection.put(adapterPosition, contactId);
|
||||
}
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public ContactSelectionListItem getView() {
|
||||
return (ContactSelectionListItem) itemView;
|
||||
}
|
||||
|
||||
public void bind(@NonNull GlideRequests glideRequests, int type, DcContact contact, String name, String addr, String label, int color, boolean multiSelect) {
|
||||
getView().set(glideRequests, type, contact, name, addr, label, color, multiSelect);
|
||||
public void bind(@NonNull GlideRequests glideRequests, int type, DcContact contact, String name, String addr, String label, int color, boolean multiSelect, boolean enabled) {
|
||||
getView().set(glideRequests, type, contact, name, addr, label, color, multiSelect, enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -141,6 +202,16 @@ public class ContactSelectionListAdapter extends RecyclerView.Adapter
|
||||
public void setChecked(boolean checked) {
|
||||
getView().setChecked(checked);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelected(boolean enabled) {
|
||||
getView().setSelected(enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnabled(boolean enabled) {
|
||||
getView().setEnabled(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
public static class DividerViewHolder extends ViewHolder {
|
||||
@@ -153,7 +224,7 @@ public class ContactSelectionListAdapter extends RecyclerView.Adapter
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(@NonNull GlideRequests glideRequests, int type, DcContact contact, String name, String number, String label, int color, boolean multiSelect) {
|
||||
public void bind(@NonNull GlideRequests glideRequests, int type, DcContact contact, String name, String number, String label, int color, boolean multiSelect, boolean enabled) {
|
||||
this.label.setText(name);
|
||||
}
|
||||
|
||||
@@ -162,6 +233,15 @@ public class ContactSelectionListAdapter extends RecyclerView.Adapter
|
||||
|
||||
@Override
|
||||
public void setChecked(boolean checked) {}
|
||||
|
||||
@Override
|
||||
public void setSelected(boolean enabled) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnabled(boolean enabled) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static class HeaderViewHolder extends RecyclerView.ViewHolder {
|
||||
@@ -197,40 +277,46 @@ public class ContactSelectionListAdapter extends RecyclerView.Adapter
|
||||
@Override
|
||||
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
|
||||
|
||||
int id = dcContactList[i];
|
||||
int color = drawables.getColor(0, 0xff000000);
|
||||
int id = dcContactList[i];
|
||||
int color = drawables.getColor(0, 0xff000000);
|
||||
DcContact dcContact = null;
|
||||
String label = null;
|
||||
String name;
|
||||
String addr = null;
|
||||
boolean itemMultiSelect = multiSelect;
|
||||
String label = null;
|
||||
String name;
|
||||
String addr = null;
|
||||
boolean itemMultiSelect = multiSelect;
|
||||
|
||||
if(id==DcContact.DC_CONTACT_ID_NEW_CONTACT) {
|
||||
if (id == DcContact.DC_CONTACT_ID_NEW_CONTACT) {
|
||||
name = context.getString(R.string.contact_selection_list__new_contact);
|
||||
itemMultiSelect = false; // the item creates a new contact in the list that will be selected instead
|
||||
if(dcContext.mayBeValidAddr(query)) {
|
||||
if (dcContext.mayBeValidAddr(query)) {
|
||||
addr = query == null ? "" : query;
|
||||
label = "\u2026";
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
addr = context.getString(R.string.contact_selection_list__type_email_above);
|
||||
}
|
||||
}
|
||||
else if(id==DcContact.DC_CONTACT_ID_NEW_GROUP) {
|
||||
} else if (id == DcContact.DC_CONTACT_ID_NEW_GROUP) {
|
||||
name = context.getString(R.string.contact_selection_list__new_group);
|
||||
}
|
||||
else if(id==DcContact.DC_CONTACT_ID_NEW_VERIFIED_GROUP) {
|
||||
} else if (id == DcContact.DC_CONTACT_ID_NEW_VERIFIED_GROUP) {
|
||||
name = context.getString(R.string.contact_selection_list__new_verified_group);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
dcContact = getContact(i);
|
||||
name = dcContact.getDisplayName();
|
||||
addr = dcContact.getAddr();
|
||||
name = dcContact.getDisplayName();
|
||||
addr = dcContact.getAddr();
|
||||
}
|
||||
|
||||
ViewHolder holder = (ViewHolder)viewHolder;
|
||||
ViewHolder holder = (ViewHolder) viewHolder;
|
||||
holder.unbind(glideRequests);
|
||||
holder.bind(glideRequests, id, dcContact, name, addr, label, color, itemMultiSelect);
|
||||
boolean enabled = true;
|
||||
if (dcContact == null) {
|
||||
holder.setEnabled(!isActionModeEnabled());
|
||||
if (isActionModeEnabled()) {
|
||||
enabled = false;
|
||||
}
|
||||
} else {
|
||||
boolean selected = actionModeSelection.indexOfValue(id) > -1;
|
||||
holder.setSelected(selected);
|
||||
}
|
||||
holder.bind(glideRequests, id, dcContact, name, addr, label, color, itemMultiSelect, enabled);
|
||||
holder.setChecked(selectedContacts.contains(addr));
|
||||
}
|
||||
|
||||
@@ -248,10 +334,15 @@ public class ContactSelectionListAdapter extends RecyclerView.Adapter
|
||||
return selectedContacts;
|
||||
}
|
||||
|
||||
public interface ItemClickListener {
|
||||
void onItemClick(ContactSelectionListItem item);
|
||||
public SparseIntArray getActionModeSelection() {
|
||||
return actionModeSelection;
|
||||
}
|
||||
|
||||
public interface ItemClickListener {
|
||||
void onItemClick(ContactSelectionListItem item, boolean handleActionMode);
|
||||
|
||||
void onItemLongClick(ContactSelectionListItem view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getHeaderId(int position) {
|
||||
|
||||
@@ -17,7 +17,6 @@ import org.thoughtcrime.securesms.database.Address;
|
||||
import org.thoughtcrime.securesms.mms.GlideRequests;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientModifiedListener;
|
||||
import org.thoughtcrime.securesms.util.GroupUtil;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
import org.thoughtcrime.securesms.util.ViewUtil;
|
||||
|
||||
@@ -59,7 +58,7 @@ public class ContactSelectionListItem extends LinearLayout implements RecipientM
|
||||
ViewUtil.setTextViewGravityStart(this.nameView, getContext());
|
||||
}
|
||||
|
||||
public void set(@NonNull GlideRequests glideRequests, int specialId, DcContact contact, String name, String number, String label, int color, boolean multiSelect) {
|
||||
public void set(@NonNull GlideRequests glideRequests, int specialId, DcContact contact, String name, String number, String label, int color, boolean multiSelect, boolean enabled) {
|
||||
this.glideRequests = glideRequests;
|
||||
this.specialId = specialId;
|
||||
this.number = number;
|
||||
@@ -67,15 +66,20 @@ public class ContactSelectionListItem extends LinearLayout implements RecipientM
|
||||
if(specialId==DcContact.DC_CONTACT_ID_NEW_CONTACT || specialId==DcContact.DC_CONTACT_ID_NEW_GROUP || specialId==DcContact.DC_CONTACT_ID_NEW_VERIFIED_GROUP) {
|
||||
this.recipient = null;
|
||||
this.contactPhotoImage.setAvatar(glideRequests, Recipient.from(getContext(), Address.UNKNOWN, true), false);
|
||||
if (!enabled) {
|
||||
this.setBackgroundColor(getResources().getColor(R.color.gray27));
|
||||
} else {
|
||||
resetBackground();
|
||||
}
|
||||
}
|
||||
else {
|
||||
resetBackground();
|
||||
this.recipient = DcHelper.getContext(getContext()).getRecipient(contact);
|
||||
this.recipient.addListener(this);
|
||||
if (this.recipient.getName() != null) {
|
||||
name = this.recipient.getName();
|
||||
}
|
||||
}
|
||||
|
||||
this.nameView.setTextColor(color);
|
||||
this.numberView.setTextColor(color);
|
||||
this.contactPhotoImage.setAvatar(glideRequests, recipient, false);
|
||||
@@ -86,6 +90,11 @@ public class ContactSelectionListItem extends LinearLayout implements RecipientM
|
||||
else this.checkBox.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
private void resetBackground() {
|
||||
this.setBackgroundColor(getResources().getColor(R.color.transparent));
|
||||
this.setBackgroundResource(R.drawable.conversation_list_item_background);
|
||||
}
|
||||
|
||||
public void setChecked(boolean selected) {
|
||||
this.checkBox.setChecked(selected);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user