Added swipe to delete local contact

This commit is contained in:
M66B 2023-10-15 09:07:24 +02:00
parent c29b82763d
commit 3779945d51
4 changed files with 134 additions and 0 deletions

View File

@ -557,6 +557,8 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
@Override
public long getItemId(int position) {
if (position < 0 || position >= selected.size())
return -1L;
return selected.get(position).id;
}

View File

@ -23,6 +23,7 @@ import static android.app.Activity.RESULT_OK;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
@ -34,16 +35,21 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.SearchView;
import androidx.constraintlayout.widget.Group;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.Observer;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
@ -80,6 +86,7 @@ public class FragmentContacts extends FragmentBase {
private boolean junk = false;
private String searching = null;
private long selected_account;
private boolean deleting = false;
private AdapterContact adapter;
@ -127,6 +134,90 @@ public class FragmentContacts extends FragmentBase {
adapter = new AdapterContact(this);
rvContacts.setAdapter(adapter);
ItemTouchHelper.Callback touchHelper = new ItemTouchHelper.Callback() {
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
return makeMovementFlags(0, ItemTouchHelper.LEFT);
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
try {
int pos = viewHolder.getAdapterPosition();
long id = adapter.getItemId(pos);
if (deleting)
delete(id);
else {
adapter.notifyItemChanged(pos);
Context context = getContext();
if (context == null)
return;
LayoutInflater inflater = LayoutInflater.from(context);
View dview = inflater.inflate(R.layout.dialog_contact_delete, null);
CheckBox cbNotAgain = dview.findViewById(R.id.cbNotAgain);
cbNotAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
deleting = isChecked;
}
});
new AlertDialog.Builder(context)
.setView(dview)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
delete(id);
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
}
} catch (Throwable ex) {
Log.e(ex);
}
}
private void delete(long id) {
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Integer>() {
@Override
protected Integer onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
DB db = DB.getInstance(context);
return db.contact().deleteContact(id);
}
@Override
protected void onExecuted(Bundle args, Integer count) {
if (count <= 0 && adapter != null)
adapter.notifyDataSetChanged();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.e(ex);
if (adapter != null)
adapter.notifyDataSetChanged();
}
}.execute(FragmentContacts.this, args, "contact:delete");
}
};
new ItemTouchHelper(touchHelper).attachToRecyclerView(rvContacts);
// Initialize
grpReady.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
@ -140,6 +231,7 @@ public class FragmentContacts extends FragmentBase {
outState.putBoolean("fair:junk", junk);
outState.putString("fair:searching", searching);
outState.putLong("fair:selected_account", selected_account);
outState.putBoolean("fair:deleting", deleting);
super.onSaveInstanceState(outState);
}
@ -152,6 +244,7 @@ public class FragmentContacts extends FragmentBase {
junk = savedInstanceState.getBoolean("fair:junk");
searching = savedInstanceState.getString("fair:searching");
selected_account = savedInstanceState.getLong("fair:selected_account");
deleting = savedInstanceState.getBoolean("fair:deleting");
if (account < 0)
account = null;

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<eu.faircode.email.ScrollViewEx xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="24dp"
android:scrollbarStyle="outsideOverlay">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawableStart="@drawable/twotone_person_24"
android:drawablePadding="6dp"
android:drawableTint="?android:attr/textColorPrimary"
android:paddingTop="6dp"
android:text="@string/title_delete_contact"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="?android:attr/textColorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/cbNotAgain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_no_ask_again"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvMessage" />
</androidx.constraintlayout.widget.ConstraintLayout>
</eu.faircode.email.ScrollViewEx>

View File

@ -1287,6 +1287,7 @@
<string name="title_delete_channel">Delete notification channel</string>
<string name="title_insert_contact">Add contact</string>
<string name="title_edit_contact">Edit contact</string>
<string name="title_delete_contact">Delete contact?</string>
<string name="title_contact_email">Email</string>
<string name="title_contact_name">Name</string>
<string name="title_contact_group">Group</string>