2019-01-17 13:29:35 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
/*
|
|
|
|
This file is part of FairEmail.
|
|
|
|
|
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-01-05 17:32:53 +00:00
|
|
|
Copyright 2018-2020 by Marcel Bokhorst (M66B)
|
2019-01-17 13:29:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2020-01-21 09:52:34 +00:00
|
|
|
import android.graphics.Typeface;
|
2019-05-03 19:30:11 +00:00
|
|
|
import android.os.Bundle;
|
2020-01-21 09:52:34 +00:00
|
|
|
import android.text.SpannableString;
|
2019-05-02 14:32:24 +00:00
|
|
|
import android.text.TextUtils;
|
2020-01-21 09:52:34 +00:00
|
|
|
import android.text.style.StyleSpan;
|
2019-01-17 13:29:35 +00:00
|
|
|
import android.view.LayoutInflater;
|
2019-05-03 19:30:11 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2019-01-17 13:29:35 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2019-01-18 11:26:02 +00:00
|
|
|
import android.widget.ImageView;
|
2019-01-17 13:29:35 +00:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
2019-05-03 19:30:11 +00:00
|
|
|
import androidx.appcompat.widget.PopupMenu;
|
2019-07-01 07:30:07 +00:00
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.lifecycle.Lifecycle;
|
|
|
|
import androidx.lifecycle.LifecycleObserver;
|
2019-01-17 13:29:35 +00:00
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
2019-07-01 07:30:07 +00:00
|
|
|
import androidx.lifecycle.OnLifecycleEvent;
|
2019-01-17 13:29:35 +00:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
|
|
import androidx.recyclerview.widget.DiffUtil;
|
|
|
|
import androidx.recyclerview.widget.ListUpdateCallback;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2019-06-19 08:55:11 +00:00
|
|
|
import com.google.android.material.snackbar.Snackbar;
|
|
|
|
|
2019-06-09 12:48:43 +00:00
|
|
|
import org.json.JSONException;
|
2019-05-02 11:48:02 +00:00
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2019-06-09 12:48:43 +00:00
|
|
|
import java.io.IOException;
|
2019-10-19 14:02:28 +00:00
|
|
|
import java.text.NumberFormat;
|
2019-04-17 18:21:44 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-06-09 12:48:43 +00:00
|
|
|
import javax.mail.MessagingException;
|
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
|
2019-07-01 07:30:07 +00:00
|
|
|
private Fragment parentFragment;
|
2019-01-17 13:29:35 +00:00
|
|
|
private Context context;
|
|
|
|
private LifecycleOwner owner;
|
|
|
|
private LayoutInflater inflater;
|
|
|
|
|
2019-03-15 11:53:22 +00:00
|
|
|
private List<TupleRuleEx> items = new ArrayList<>();
|
2019-01-17 13:29:35 +00:00
|
|
|
|
2019-05-03 19:30:11 +00:00
|
|
|
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
|
2019-03-15 11:53:22 +00:00
|
|
|
private View view;
|
2019-01-17 13:29:35 +00:00
|
|
|
private TextView tvName;
|
2019-01-18 08:23:53 +00:00
|
|
|
private TextView tvOrder;
|
2019-01-18 11:26:02 +00:00
|
|
|
private ImageView ivStop;
|
2019-05-02 14:32:24 +00:00
|
|
|
private TextView tvCondition;
|
2019-05-02 11:48:02 +00:00
|
|
|
private TextView tvAction;
|
2019-10-19 14:02:28 +00:00
|
|
|
private TextView tvApplied;
|
2019-01-17 13:29:35 +00:00
|
|
|
|
2019-10-19 14:02:28 +00:00
|
|
|
private NumberFormat NF = NumberFormat.getNumberInstance();
|
2019-05-03 19:30:11 +00:00
|
|
|
private TwoStateOwner powner = new TwoStateOwner(owner, "RulePopup");
|
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
ViewHolder(View itemView) {
|
|
|
|
super(itemView);
|
|
|
|
|
2019-03-15 11:53:22 +00:00
|
|
|
view = itemView.findViewById(R.id.clItem);
|
2019-01-17 13:29:35 +00:00
|
|
|
tvName = itemView.findViewById(R.id.tvName);
|
2019-01-18 08:23:53 +00:00
|
|
|
tvOrder = itemView.findViewById(R.id.tvOrder);
|
2019-01-18 11:26:02 +00:00
|
|
|
ivStop = itemView.findViewById(R.id.ivStop);
|
2019-05-02 14:32:24 +00:00
|
|
|
tvCondition = itemView.findViewById(R.id.tvCondition);
|
2019-05-02 11:48:02 +00:00
|
|
|
tvAction = itemView.findViewById(R.id.tvAction);
|
2019-10-19 14:02:28 +00:00
|
|
|
tvApplied = itemView.findViewById(R.id.tvApplied);
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void wire() {
|
2019-03-15 11:53:22 +00:00
|
|
|
view.setOnClickListener(this);
|
2019-05-03 19:30:11 +00:00
|
|
|
view.setOnLongClickListener(this);
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void unwire() {
|
2019-03-15 11:53:22 +00:00
|
|
|
view.setOnClickListener(null);
|
2019-05-03 19:30:11 +00:00
|
|
|
view.setOnLongClickListener(null);
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 21:41:00 +00:00
|
|
|
private void bindTo(TupleRuleEx rule) {
|
2019-03-15 11:53:22 +00:00
|
|
|
view.setActivated(!rule.enabled);
|
2019-01-17 13:29:35 +00:00
|
|
|
tvName.setText(rule.name);
|
2019-01-18 08:23:53 +00:00
|
|
|
tvOrder.setText(Integer.toString(rule.order));
|
2019-12-10 19:37:00 +00:00
|
|
|
ivStop.setVisibility(rule.stop ? View.VISIBLE : View.INVISIBLE);
|
2019-05-02 11:48:02 +00:00
|
|
|
|
2019-05-02 14:32:24 +00:00
|
|
|
try {
|
|
|
|
List<String> condition = new ArrayList<>();
|
|
|
|
JSONObject jcondition = new JSONObject(rule.condition);
|
|
|
|
if (jcondition.has("sender"))
|
|
|
|
condition.add(context.getString(R.string.title_rule_sender));
|
|
|
|
if (jcondition.has("recipient"))
|
|
|
|
condition.add(context.getString(R.string.title_rule_recipient));
|
|
|
|
if (jcondition.has("subject"))
|
|
|
|
condition.add(context.getString(R.string.title_rule_subject));
|
|
|
|
if (jcondition.has("header"))
|
|
|
|
condition.add(context.getString(R.string.title_rule_header));
|
2019-08-04 17:14:53 +00:00
|
|
|
if (jcondition.has("schedule"))
|
|
|
|
condition.add(context.getString(R.string.title_rule_time));
|
2019-05-22 19:26:02 +00:00
|
|
|
tvCondition.setText(TextUtils.join(" & ", condition));
|
2019-05-02 14:32:24 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
tvCondition.setText(ex.getMessage());
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:48:02 +00:00
|
|
|
try {
|
|
|
|
JSONObject jaction = new JSONObject(rule.action);
|
|
|
|
int type = jaction.getInt("type");
|
|
|
|
switch (type) {
|
2019-10-01 10:40:54 +00:00
|
|
|
case EntityRule.TYPE_NOOP:
|
|
|
|
tvAction.setText(R.string.title_rule_noop);
|
|
|
|
break;
|
2019-05-02 11:48:02 +00:00
|
|
|
case EntityRule.TYPE_SEEN:
|
2019-05-22 19:26:02 +00:00
|
|
|
tvAction.setText(R.string.title_rule_seen);
|
2019-05-02 11:48:02 +00:00
|
|
|
break;
|
|
|
|
case EntityRule.TYPE_UNSEEN:
|
2019-05-22 19:26:02 +00:00
|
|
|
tvAction.setText(R.string.title_rule_unseen);
|
|
|
|
break;
|
2019-10-12 09:09:54 +00:00
|
|
|
case EntityRule.TYPE_HIDE:
|
|
|
|
tvAction.setText(R.string.title_rule_hide);
|
|
|
|
break;
|
2019-09-22 09:54:29 +00:00
|
|
|
case EntityRule.TYPE_IGNORE:
|
|
|
|
tvAction.setText(R.string.title_rule_ignore);
|
|
|
|
break;
|
2019-06-01 13:27:21 +00:00
|
|
|
case EntityRule.TYPE_SNOOZE:
|
|
|
|
tvAction.setText(R.string.title_rule_snooze);
|
|
|
|
break;
|
2019-05-22 19:26:02 +00:00
|
|
|
case EntityRule.TYPE_FLAG:
|
|
|
|
tvAction.setText(R.string.title_rule_flag);
|
2019-05-02 11:48:02 +00:00
|
|
|
break;
|
2019-10-09 09:09:00 +00:00
|
|
|
case EntityRule.TYPE_KEYWORD:
|
|
|
|
tvAction.setText(R.string.title_rule_keyword);
|
|
|
|
break;
|
2019-05-02 11:48:02 +00:00
|
|
|
case EntityRule.TYPE_MOVE:
|
2019-05-22 19:26:02 +00:00
|
|
|
tvAction.setText(R.string.title_rule_move);
|
|
|
|
break;
|
|
|
|
case EntityRule.TYPE_COPY:
|
|
|
|
tvAction.setText(R.string.title_rule_copy);
|
2019-05-02 11:48:02 +00:00
|
|
|
break;
|
|
|
|
case EntityRule.TYPE_ANSWER:
|
2019-05-22 19:26:02 +00:00
|
|
|
tvAction.setText(R.string.title_rule_answer);
|
2019-05-02 11:48:02 +00:00
|
|
|
break;
|
|
|
|
case EntityRule.TYPE_AUTOMATION:
|
|
|
|
tvAction.setText(R.string.title_rule_automation);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Unknown action type=" + type);
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
tvAction.setText(ex.getMessage());
|
|
|
|
}
|
2019-10-19 14:02:28 +00:00
|
|
|
|
|
|
|
tvApplied.setText(NF.format(rule.applied));
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
int pos = getAdapterPosition();
|
|
|
|
if (pos == RecyclerView.NO_POSITION)
|
|
|
|
return;
|
|
|
|
|
2019-03-15 11:53:22 +00:00
|
|
|
TupleRuleEx rule = items.get(pos);
|
2019-01-17 13:29:35 +00:00
|
|
|
|
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
|
|
|
|
lbm.sendBroadcast(
|
|
|
|
new Intent(ActivityView.ACTION_EDIT_RULE)
|
2019-01-18 08:23:53 +00:00
|
|
|
.putExtra("id", rule.id)
|
|
|
|
.putExtra("account", rule.account)
|
|
|
|
.putExtra("folder", rule.folder));
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
2019-05-03 19:30:11 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onLongClick(View v) {
|
|
|
|
int pos = getAdapterPosition();
|
|
|
|
if (pos == RecyclerView.NO_POSITION)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
final TupleRuleEx rule = items.get(pos);
|
|
|
|
|
|
|
|
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(context, powner, view);
|
|
|
|
|
2020-01-21 09:52:34 +00:00
|
|
|
SpannableString ss = new SpannableString(rule.name);
|
|
|
|
ss.setSpan(new StyleSpan(Typeface.ITALIC), 0, ss.length(), 0);
|
|
|
|
popupMenu.getMenu().add(Menu.NONE, 0, 0, ss).setEnabled(false);
|
2019-06-25 08:53:06 +00:00
|
|
|
|
2019-05-16 08:42:01 +00:00
|
|
|
popupMenu.getMenu().add(Menu.NONE, R.string.title_rule_enabled, 1, R.string.title_rule_enabled)
|
2019-05-03 19:30:11 +00:00
|
|
|
.setCheckable(true).setChecked(rule.enabled);
|
2019-06-09 12:48:43 +00:00
|
|
|
popupMenu.getMenu().add(Menu.NONE, R.string.title_rule_execute, 2, R.string.title_rule_execute)
|
2019-08-13 08:27:17 +00:00
|
|
|
.setEnabled(ActivityBilling.isPro(context));
|
2019-10-19 14:02:28 +00:00
|
|
|
popupMenu.getMenu().add(Menu.NONE, R.string.title_reset, 3, R.string.title_reset);
|
|
|
|
popupMenu.getMenu().add(Menu.NONE, R.string.title_move_to_folder, 4, R.string.title_move_to_folder);
|
|
|
|
popupMenu.getMenu().add(Menu.NONE, R.string.title_copy, 5, R.string.title_copy);
|
2019-05-03 19:30:11 +00:00
|
|
|
|
|
|
|
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onMenuItemClick(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
2019-05-16 08:42:01 +00:00
|
|
|
case R.string.title_rule_enabled:
|
2019-05-03 19:30:11 +00:00
|
|
|
onActionEnabled(!item.isChecked());
|
|
|
|
return true;
|
2019-08-20 20:08:38 +00:00
|
|
|
|
2019-06-09 12:48:43 +00:00
|
|
|
case R.string.title_rule_execute:
|
|
|
|
onActionExecute();
|
|
|
|
return true;
|
2019-08-20 20:08:38 +00:00
|
|
|
|
2019-10-19 14:02:28 +00:00
|
|
|
case R.string.title_reset:
|
|
|
|
onActionReset();
|
|
|
|
return true;
|
|
|
|
|
2019-09-08 07:54:03 +00:00
|
|
|
case R.string.title_move_to_folder:
|
|
|
|
onActionMove();
|
|
|
|
return true;
|
|
|
|
|
2019-08-20 20:08:38 +00:00
|
|
|
case R.string.title_copy:
|
|
|
|
onActionCopy();
|
|
|
|
return true;
|
|
|
|
|
2019-05-03 19:30:11 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onActionEnabled(boolean enabled) {
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putLong("id", rule.id);
|
|
|
|
args.putBoolean("enabled", enabled);
|
|
|
|
|
|
|
|
new SimpleTask<Boolean>() {
|
|
|
|
@Override
|
|
|
|
protected Boolean onExecute(Context context, Bundle args) {
|
|
|
|
long id = args.getLong("id");
|
|
|
|
boolean enabled = args.getBoolean("enabled");
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
db.rule().setRuleEnabled(id, enabled);
|
|
|
|
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2019-12-06 07:50:46 +00:00
|
|
|
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
|
2019-05-03 19:30:11 +00:00
|
|
|
}
|
|
|
|
}.execute(context, owner, args, "rule:enable");
|
|
|
|
}
|
2019-06-09 12:48:43 +00:00
|
|
|
|
|
|
|
private void onActionExecute() {
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putLong("id", rule.id);
|
|
|
|
|
|
|
|
new SimpleTask<Integer>() {
|
|
|
|
@Override
|
|
|
|
protected Integer onExecute(Context context, Bundle args) throws JSONException, MessagingException, IOException {
|
|
|
|
long id = args.getLong("id");
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
EntityRule rule = db.rule().getRule(id);
|
|
|
|
if (rule == null)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
JSONObject jcondition = new JSONObject(rule.condition);
|
|
|
|
JSONObject jheader = jcondition.optJSONObject("header");
|
|
|
|
if (jheader != null)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int applied = 0;
|
2019-10-02 19:13:10 +00:00
|
|
|
List<Long> ids =
|
2019-10-23 18:01:43 +00:00
|
|
|
db.message().getMessageIdsByFolder(rule.folder);
|
2019-06-09 12:48:43 +00:00
|
|
|
for (long mid : ids)
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
EntityMessage message = db.message().getMessage(mid);
|
2019-10-16 14:57:43 +00:00
|
|
|
if (message == null)
|
|
|
|
continue;
|
2019-06-09 12:48:43 +00:00
|
|
|
|
2019-07-16 06:25:30 +00:00
|
|
|
if (rule.matches(context, message, null))
|
|
|
|
if (rule.execute(context, message))
|
|
|
|
applied++;
|
2019-06-09 12:48:43 +00:00
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
return applied;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onExecuted(Bundle args, Integer applied) {
|
2019-06-24 07:49:45 +00:00
|
|
|
Snackbar.make(
|
2019-07-01 07:30:07 +00:00
|
|
|
parentFragment.getView(),
|
2019-06-24 07:49:45 +00:00
|
|
|
context.getString(R.string.title_rule_applied, applied),
|
|
|
|
Snackbar.LENGTH_LONG).show();
|
2019-06-09 12:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2019-12-06 07:50:46 +00:00
|
|
|
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
|
2019-06-09 12:48:43 +00:00
|
|
|
}
|
|
|
|
}.execute(context, owner, args, "rule:execute");
|
|
|
|
}
|
2019-08-20 20:08:38 +00:00
|
|
|
|
2019-10-19 14:02:28 +00:00
|
|
|
private void onActionReset() {
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putLong("id", rule.id);
|
|
|
|
|
|
|
|
new SimpleTask<Void>() {
|
|
|
|
@Override
|
|
|
|
protected Void onExecute(Context context, Bundle args) {
|
|
|
|
long id = args.getLong("id");
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
db.rule().resetRule(id);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2019-12-06 07:50:46 +00:00
|
|
|
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
|
2019-10-19 14:02:28 +00:00
|
|
|
}
|
|
|
|
}.execute(context, owner, args, "rule:execute");
|
|
|
|
}
|
|
|
|
|
2019-09-08 07:54:03 +00:00
|
|
|
private void onActionMove() {
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putString("title", context.getString(R.string.title_move_to_folder));
|
|
|
|
args.putLong("account", rule.account);
|
|
|
|
args.putLongArray("disabled", new long[]{rule.folder});
|
|
|
|
args.putLong("rule", rule.id);
|
|
|
|
|
|
|
|
FragmentDialogFolder fragment = new FragmentDialogFolder();
|
|
|
|
fragment.setArguments(args);
|
|
|
|
fragment.setTargetFragment(parentFragment, FragmentRules.REQUEST_MOVE);
|
2019-10-12 15:16:53 +00:00
|
|
|
fragment.show(parentFragment.getParentFragmentManager(), "rule:move");
|
2019-09-08 07:54:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 20:08:38 +00:00
|
|
|
private void onActionCopy() {
|
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
|
|
|
|
lbm.sendBroadcast(
|
|
|
|
new Intent(ActivityView.ACTION_EDIT_RULE)
|
|
|
|
.putExtra("id", rule.id)
|
|
|
|
.putExtra("account", rule.account)
|
|
|
|
.putExtra("folder", rule.folder)
|
|
|
|
.putExtra("copy", true));
|
|
|
|
}
|
2019-05-03 19:30:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
popupMenu.show();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 07:30:07 +00:00
|
|
|
AdapterRule(Fragment parentFragment) {
|
|
|
|
this.parentFragment = parentFragment;
|
|
|
|
this.context = parentFragment.getContext();
|
|
|
|
this.owner = parentFragment.getViewLifecycleOwner();
|
2019-01-17 13:29:35 +00:00
|
|
|
this.inflater = LayoutInflater.from(context);
|
2019-07-01 07:30:07 +00:00
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
setHasStableIds(true);
|
2019-07-01 07:30:07 +00:00
|
|
|
|
|
|
|
owner.getLifecycle().addObserver(new LifecycleObserver() {
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
|
|
|
public void onDestroyed() {
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d(AdapterRule.this + " parent destroyed");
|
2019-07-01 07:30:07 +00:00
|
|
|
AdapterRule.this.parentFragment = null;
|
|
|
|
}
|
|
|
|
});
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 21:41:00 +00:00
|
|
|
public void set(@NonNull List<TupleRuleEx> rules) {
|
2019-01-17 13:29:35 +00:00
|
|
|
Log.i("Set rules=" + rules.size());
|
|
|
|
|
2019-03-15 11:53:22 +00:00
|
|
|
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(items, rules), false);
|
2019-01-17 13:29:35 +00:00
|
|
|
|
2019-03-15 11:53:22 +00:00
|
|
|
items = rules;
|
2019-01-17 13:29:35 +00:00
|
|
|
|
|
|
|
diff.dispatchUpdatesTo(new ListUpdateCallback() {
|
|
|
|
@Override
|
|
|
|
public void onInserted(int position, int count) {
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d("Inserted @" + position + " #" + count);
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRemoved(int position, int count) {
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d("Removed @" + position + " #" + count);
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMoved(int fromPosition, int toPosition) {
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d("Moved " + fromPosition + ">" + toPosition);
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onChanged(int position, int count, Object payload) {
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d("Changed @" + position + " #" + count);
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
diff.dispatchUpdatesTo(this);
|
|
|
|
}
|
|
|
|
|
2019-02-10 18:02:55 +00:00
|
|
|
private class DiffCallback extends DiffUtil.Callback {
|
2019-03-15 11:53:22 +00:00
|
|
|
private List<TupleRuleEx> prev = new ArrayList<>();
|
|
|
|
private List<TupleRuleEx> next = new ArrayList<>();
|
2019-01-17 13:29:35 +00:00
|
|
|
|
2019-02-10 18:02:55 +00:00
|
|
|
DiffCallback(List<TupleRuleEx> prev, List<TupleRuleEx> next) {
|
2019-03-15 11:53:22 +00:00
|
|
|
this.prev.addAll(prev);
|
|
|
|
this.next.addAll(next);
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getOldListSize() {
|
|
|
|
return prev.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getNewListSize() {
|
|
|
|
return next.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
|
2019-01-17 21:41:00 +00:00
|
|
|
TupleRuleEx r1 = prev.get(oldItemPosition);
|
|
|
|
TupleRuleEx r2 = next.get(newItemPosition);
|
2019-01-17 13:29:35 +00:00
|
|
|
return r1.id.equals(r2.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
|
2019-01-17 21:41:00 +00:00
|
|
|
TupleRuleEx r1 = prev.get(oldItemPosition);
|
|
|
|
TupleRuleEx r2 = next.get(newItemPosition);
|
2019-01-17 13:29:35 +00:00
|
|
|
return r1.equals(r2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getItemId(int position) {
|
2019-03-15 11:53:22 +00:00
|
|
|
return items.get(position).id;
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
2019-03-15 11:53:22 +00:00
|
|
|
return items.size();
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@NonNull
|
|
|
|
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
return new ViewHolder(inflater.inflate(R.layout.item_rule, parent, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
|
|
|
holder.unwire();
|
2019-03-15 11:53:22 +00:00
|
|
|
TupleRuleEx rule = items.get(position);
|
2019-01-17 13:29:35 +00:00
|
|
|
holder.bindTo(rule);
|
|
|
|
holder.wire();
|
|
|
|
}
|
2019-05-03 19:30:11 +00:00
|
|
|
|
|
|
|
@Override
|
2019-09-21 18:28:03 +00:00
|
|
|
public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
|
2019-05-03 19:30:11 +00:00
|
|
|
holder.powner.recreate();
|
|
|
|
}
|
2019-01-17 13:29:35 +00:00
|
|
|
}
|