Always allow deleting operations

pull/159/head
M66B 6 years ago
parent 1f2074a26f
commit b6047488d0

@ -21,22 +21,23 @@ package eu.faircode.email;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.widget.PopupMenu;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver; import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent; import androidx.lifecycle.OnLifecycleEvent;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListUpdateCallback; import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -53,8 +54,6 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
private LifecycleOwner owner; private LifecycleOwner owner;
private LayoutInflater inflater; private LayoutInflater inflater;
private boolean debug;
private List<TupleOperationEx> items = new ArrayList<>(); private List<TupleOperationEx> items = new ArrayList<>();
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
@ -65,6 +64,8 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
private TextView tvTime; private TextView tvTime;
private TextView tvError; private TextView tvError;
private TwoStateOwner powner = new TwoStateOwner(owner, "OperationPopup");
ViewHolder(View itemView) { ViewHolder(View itemView) {
super(itemView); super(itemView);
@ -78,14 +79,12 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
private void wire() { private void wire() {
view.setOnClickListener(this); view.setOnClickListener(this);
if (BuildConfig.DEBUG || debug) view.setOnLongClickListener(this);
view.setOnLongClickListener(this);
} }
private void unwire() { private void unwire() {
view.setOnClickListener(null); view.setOnClickListener(null);
if (BuildConfig.DEBUG || debug) view.setOnLongClickListener(null);
view.setOnLongClickListener(null);
} }
private void bindTo(TupleOperationEx operation) { private void bindTo(TupleOperationEx operation) {
@ -183,30 +182,52 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
if (pos == RecyclerView.NO_POSITION) if (pos == RecyclerView.NO_POSITION)
return false; return false;
TupleOperationEx operation = items.get(pos); final TupleOperationEx operation = items.get(pos);
if (operation == null) if (operation == null)
return false; return false;
Bundle args = new Bundle(); PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(context, powner, view);
args.putLong("id", operation.id); popupMenu.getMenu().add(Menu.NONE, R.string.title_delete, 1, R.string.title_delete);
args.putLong("folder", operation.folder);
new SimpleTask<Void>() { popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override @Override
protected Void onExecute(Context context, Bundle args) { public boolean onMenuItemClick(MenuItem item) {
long id = args.getLong("id"); switch (item.getItemId()) {
long folder = args.getLong("folder"); case R.string.title_delete:
DB db = DB.getInstance(context); onActionDelete();
db.operation().deleteOperation(id); return true;
db.folder().setFolderError(folder, null); default:
return null; return false;
}
} }
@Override private void onActionDelete() {
protected void onException(Bundle args, Throwable ex) { Bundle args = new Bundle();
Helper.unexpectedError(parentFragment.getFragmentManager(), ex); args.putLong("id", operation.id);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
EntityOperation operation = db.operation().getOperation(id);
if (operation == null)
return null;
db.operation().deleteOperation(operation.id);
db.folder().setFolderError(operation.folder, null);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(parentFragment.getFragmentManager(), ex);
}
}.execute(context, owner, args, "operation:delete");
} }
}.execute(context, owner, args, "operation:delete"); });
popupMenu.show();
return true; return true;
} }
@ -218,9 +239,6 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
this.owner = parentFragment.getViewLifecycleOwner(); this.owner = parentFragment.getViewLifecycleOwner();
this.inflater = LayoutInflater.from(context); this.inflater = LayoutInflater.from(context);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
this.debug = prefs.getBoolean("debug", false);
setHasStableIds(true); setHasStableIds(true);
owner.getLifecycle().addObserver(new LifecycleObserver() { owner.getLifecycle().addObserver(new LifecycleObserver() {
@ -313,6 +331,11 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
return new ViewHolder(inflater.inflate(R.layout.item_operation, parent, false)); return new ViewHolder(inflater.inflate(R.layout.item_operation, parent, false));
} }
@Override
public void onViewRecycled(@NonNull ViewHolder holder) {
holder.powner.recreate();
}
@Override @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.unwire(); holder.unwire();

@ -89,6 +89,9 @@ public interface DaoOperation {
@Query("SELECT * FROM operation WHERE name = :name") @Query("SELECT * FROM operation WHERE name = :name")
List<EntityOperation> getOperations(String name); List<EntityOperation> getOperations(String name);
@Query("SELECT * FROM operation WHERE id = :id")
EntityOperation getOperation(long id);
@Query("SELECT * FROM operation WHERE error IS NOT NULL") @Query("SELECT * FROM operation WHERE error IS NOT NULL")
List<EntityOperation> getOperationsError(); List<EntityOperation> getOperationsError();

Loading…
Cancel
Save