diff --git a/app/src/main/java/eu/faircode/email/FragmentRule.java b/app/src/main/java/eu/faircode/email/FragmentRule.java index b9f947db38..83365a5be5 100644 --- a/app/src/main/java/eu/faircode/email/FragmentRule.java +++ b/app/src/main/java/eu/faircode/email/FragmentRule.java @@ -272,96 +272,7 @@ public class FragmentRule extends FragmentBase { getString(R.string.title_rule_action_remark, data.folder.getDisplayName(getContext()))); tvActionRemark.setVisibility(View.VISIBLE); - Bundle rargs = new Bundle(); - rargs.putLong("id", id); - - new SimpleTask() { - @Override - protected TupleRuleEx onExecute(Context context, Bundle args) { - long id = args.getLong("id"); - return DB.getInstance(context).rule().getRule(id); - } - - @Override - protected void onExecuted(Bundle args, TupleRuleEx rule) { - try { - JSONObject jcondition = (rule == null ? new JSONObject() : new JSONObject(rule.condition)); - JSONObject jaction = (rule == null ? new JSONObject() : new JSONObject(rule.action)); - - JSONObject jsender = jcondition.optJSONObject("sender"); - JSONObject jsubject = jcondition.optJSONObject("subject"); - JSONObject jheader = jcondition.optJSONObject("header"); - - etName.setText(rule == null ? null : rule.name); - etOrder.setText(rule == null ? null : Integer.toString(rule.order)); - cbEnabled.setChecked(rule == null || rule.enabled); - cbStop.setChecked(rule != null && rule.stop); - etSender.setText(jsender == null ? null : jsender.getString("value")); - cbSender.setChecked(jsender != null && jsender.getBoolean("regex")); - etSubject.setText(jsubject == null ? null : jsubject.getString("value")); - cbSubject.setChecked(jsubject != null && jsubject.getBoolean("regex")); - etHeader.setText(jheader == null ? null : jheader.getString("value")); - cbHeader.setChecked(jheader != null && jheader.getBoolean("regex")); - - if (rule == null) { - for (int pos = 0; pos < adapterIdentity.getCount(); pos++) - if (adapterIdentity.getItem(pos).primary) { - spIdent.setSelection(pos); - break; - } - } else { - int type = jaction.getInt("type"); - switch (type) { - case EntityRule.TYPE_MOVE: - long target = jaction.getLong("target"); - for (int pos = 0; pos < adapterTarget.getCount(); pos++) - if (adapterTarget.getItem(pos).id.equals(target)) { - spTarget.setSelection(pos); - break; - } - break; - - case EntityRule.TYPE_ANSWER: - long identity = jaction.getLong("identity"); - for (int pos = 0; pos < adapterIdentity.getCount(); pos++) - if (adapterIdentity.getItem(pos).id.equals(identity)) { - spIdent.setSelection(pos); - break; - } - - long answer = jaction.getLong("answer"); - for (int pos = 0; pos < adapterAnswer.getCount(); pos++) - if (adapterAnswer.getItem(pos).id.equals(answer)) { - spAnswer.setSelection(pos); - break; - } - break; - } - - for (int pos = 0; pos < adapterAction.getCount(); pos++) - if (adapterAction.getItem(pos).type == type) { - spAction.setTag(pos); - spAction.setSelection(pos); - break; - } - - showActionParameters(type); - } - - grpReady.setVisibility(View.VISIBLE); - bottom_navigation.findViewById(R.id.action_delete).setVisibility(rule == null ? View.GONE : View.VISIBLE); - bottom_navigation.setVisibility(View.VISIBLE); - pbWait.setVisibility(View.GONE); - } catch (JSONException ex) { - Log.e(ex); - } - } - - @Override - protected void onException(Bundle args, Throwable ex) { - Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex); - } - }.execute(FragmentRule.this, rargs, "rule:get"); + loadRule(); } @Override @@ -403,6 +314,99 @@ public class FragmentRule extends FragmentBase { } } + private void loadRule() { + Bundle rargs = new Bundle(); + rargs.putLong("id", id); + + new SimpleTask() { + @Override + protected TupleRuleEx onExecute(Context context, Bundle args) { + long id = args.getLong("id"); + return DB.getInstance(context).rule().getRule(id); + } + + @Override + protected void onExecuted(Bundle args, TupleRuleEx rule) { + try { + JSONObject jcondition = (rule == null ? new JSONObject() : new JSONObject(rule.condition)); + JSONObject jaction = (rule == null ? new JSONObject() : new JSONObject(rule.action)); + + JSONObject jsender = jcondition.optJSONObject("sender"); + JSONObject jsubject = jcondition.optJSONObject("subject"); + JSONObject jheader = jcondition.optJSONObject("header"); + + etName.setText(rule == null ? null : rule.name); + etOrder.setText(rule == null ? null : Integer.toString(rule.order)); + cbEnabled.setChecked(rule == null || rule.enabled); + cbStop.setChecked(rule != null && rule.stop); + etSender.setText(jsender == null ? null : jsender.getString("value")); + cbSender.setChecked(jsender != null && jsender.getBoolean("regex")); + etSubject.setText(jsubject == null ? null : jsubject.getString("value")); + cbSubject.setChecked(jsubject != null && jsubject.getBoolean("regex")); + etHeader.setText(jheader == null ? null : jheader.getString("value")); + cbHeader.setChecked(jheader != null && jheader.getBoolean("regex")); + + if (rule == null) { + for (int pos = 0; pos < adapterIdentity.getCount(); pos++) + if (adapterIdentity.getItem(pos).primary) { + spIdent.setSelection(pos); + break; + } + } else { + int type = jaction.getInt("type"); + switch (type) { + case EntityRule.TYPE_MOVE: + long target = jaction.getLong("target"); + for (int pos = 0; pos < adapterTarget.getCount(); pos++) + if (adapterTarget.getItem(pos).id.equals(target)) { + spTarget.setSelection(pos); + break; + } + break; + + case EntityRule.TYPE_ANSWER: + long identity = jaction.getLong("identity"); + for (int pos = 0; pos < adapterIdentity.getCount(); pos++) + if (adapterIdentity.getItem(pos).id.equals(identity)) { + spIdent.setSelection(pos); + break; + } + + long answer = jaction.getLong("answer"); + for (int pos = 0; pos < adapterAnswer.getCount(); pos++) + if (adapterAnswer.getItem(pos).id.equals(answer)) { + spAnswer.setSelection(pos); + break; + } + break; + } + + for (int pos = 0; pos < adapterAction.getCount(); pos++) + if (adapterAction.getItem(pos).type == type) { + spAction.setTag(pos); + spAction.setSelection(pos); + break; + } + + showActionParameters(type); + } + + grpReady.setVisibility(View.VISIBLE); + bottom_navigation.findViewById(R.id.action_delete).setVisibility(rule == null ? View.GONE : View.VISIBLE); + bottom_navigation.setVisibility(View.VISIBLE); + pbWait.setVisibility(View.GONE); + } catch (JSONException ex) { + Log.e(ex); + } + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex); + } + }.execute(FragmentRule.this, rargs, "rule:get"); + } + private void onActionTrash() { new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) .setMessage(R.string.title_ask_delete_rule)