Keep record of number of times a rule was applied

This commit is contained in:
M66B 2019-10-19 16:02:28 +02:00
parent e392447a5f
commit 8d760b2acd
6 changed files with 1979 additions and 4 deletions

File diff suppressed because it is too large Load Diff

View File

@ -49,6 +49,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
@ -69,7 +70,9 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
private ImageView ivStop;
private TextView tvCondition;
private TextView tvAction;
private TextView tvApplied;
private NumberFormat NF = NumberFormat.getNumberInstance();
private TwoStateOwner powner = new TwoStateOwner(owner, "RulePopup");
ViewHolder(View itemView) {
@ -81,6 +84,7 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
ivStop = itemView.findViewById(R.id.ivStop);
tvCondition = itemView.findViewById(R.id.tvCondition);
tvAction = itemView.findViewById(R.id.tvAction);
tvApplied = itemView.findViewById(R.id.tvApplied);
}
private void wire() {
@ -163,6 +167,8 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
} catch (Throwable ex) {
tvAction.setText(ex.getMessage());
}
tvApplied.setText(NF.format(rule.applied));
}
@Override
@ -197,8 +203,9 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
.setCheckable(true).setChecked(rule.enabled);
popupMenu.getMenu().add(Menu.NONE, R.string.title_rule_execute, 2, R.string.title_rule_execute)
.setEnabled(ActivityBilling.isPro(context));
popupMenu.getMenu().add(Menu.NONE, R.string.title_move_to_folder, 3, R.string.title_move_to_folder);
popupMenu.getMenu().add(Menu.NONE, R.string.title_copy, 4, R.string.title_copy);
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);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
@ -212,6 +219,10 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
onActionExecute();
return true;
case R.string.title_reset:
onActionReset();
return true;
case R.string.title_move_to_folder:
onActionMove();
return true;
@ -306,6 +317,28 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
}.execute(context, owner, args, "rule:execute");
}
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) {
Helper.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "rule:execute");
}
private void onActionMove() {
Bundle args = new Bundle();
args.putString("title", context.getString(R.string.title_move_to_folder));

View File

@ -57,7 +57,7 @@ import io.requery.android.database.sqlite.RequerySQLiteOpenHelperFactory;
// https://developer.android.com/topic/libraries/architecture/room.html
@Database(
version = 110,
version = 111,
entities = {
EntityIdentity.class,
EntityAccount.class,
@ -1077,6 +1077,13 @@ public abstract class DB extends RoomDatabase {
db.execSQL("ALTER TABLE `message` ADD COLUMN `ui_busy` INTEGER");
}
})
.addMigrations(new Migration(110, 111) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase db) {
Log.i("DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `rule` ADD COLUMN `applied` INTEGER NOT NULL DEFAULT 0");
}
})
.build();
}

View File

@ -65,6 +65,12 @@ public interface DaoRule {
@Query("UPDATE rule SET enabled = :enabled WHERE id = :id")
int setRuleEnabled(long id, boolean enabled);
@Query("UPDATE rule SET applied = applied + 1 WHERE id = :id")
int applyRule(long id);
@Query("UPDATE rule SET applied = 0 WHERE id = :id")
int resetRule(long id);
@Query("DELETE FROM rule WHERE id = :id")
void deleteRule(long id);
}

View File

@ -85,6 +85,8 @@ public class EntityRule {
public String condition;
@NonNull
public String action;
@NonNull
public Integer applied = 0;
static final int TYPE_SEEN = 1;
static final int TYPE_UNSEEN = 2;
@ -276,6 +278,15 @@ public class EntityRule {
}
boolean execute(Context context, EntityMessage message) throws JSONException, IOException {
boolean executed = _execute(context, message);
if (executed) {
DB db = DB.getInstance(context);
db.rule().applyRule(id);
}
return executed;
}
private boolean _execute(Context context, EntityMessage message) throws JSONException, IOException {
JSONObject jaction = new JSONObject(action);
int type = jaction.getInt("type");
Log.i("Executing rule=" + type + ":" + name + " message=" + message.id);
@ -524,7 +535,8 @@ public class EntityRule {
this.enabled == other.enabled &&
this.stop == other.stop &&
this.condition.equals(other.condition) &&
this.action.equals(other.action);
this.action.equals(other.action) &&
this.applied.equals(other.applied);
} else
return false;
}

View File

@ -68,6 +68,15 @@
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvCondition" />
<TextView
android:id="@+id/tvApplied"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvCondition" />
</androidx.constraintlayout.widget.ConstraintLayout>
</eu.faircode.email.ViewCardOptional>
</FrameLayout>