From f0e1a8823e2c1def08252b1a359a664be71e54fd Mon Sep 17 00:00:00 2001 From: tonymanou Date: Mon, 9 Jan 2017 22:36:06 +0100 Subject: [PATCH] Simplify listeners containing async tasks --- .../magisk/adapters/ModulesAdapter.java | 93 +++++++------------ 1 file changed, 35 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/adapters/ModulesAdapter.java b/app/src/main/java/com/topjohnwu/magisk/adapters/ModulesAdapter.java index 781658c50..2cd7c6f07 100644 --- a/app/src/main/java/com/topjohnwu/magisk/adapters/ModulesAdapter.java +++ b/app/src/main/java/com/topjohnwu/magisk/adapters/ModulesAdapter.java @@ -49,67 +49,44 @@ public class ModulesAdapter extends RecyclerView.Adapter { - if (isChecked) { - new Async.RootTask() { - @Override - protected Void doInBackground(Void... voids) { - module.removeDisableFile(); - return null; - } - - @Override - protected void onPostExecute(Void v) { - Snackbar.make(holder.title, R.string.disable_file_removed, Snackbar.LENGTH_SHORT).show(); - } - }.exec(); - } else { - new Async.RootTask() { - @Override - protected Void doInBackground(Void... voids) { - module.createDisableFile(); - return null; - } - - @Override - protected void onPostExecute(Void v) { - Snackbar.make(holder.title, R.string.disable_file_created, Snackbar.LENGTH_SHORT).show(); - } - }.exec(); + holder.checkBox.setOnCheckedChangeListener((v, isChecked) -> new Async.RootTask() { + @Override + protected Void doInBackground(Void... voids) { + if (isChecked) { + module.removeDisableFile(); + } else { + module.createDisableFile(); + } + return null; } - }); - holder.delete.setOnClickListener(v -> { - if (module.willBeRemoved()) { - new Async.RootTask() { - @Override - protected Void doInBackground(Void... voids) { - module.deleteRemoveFile(); - return null; - } - - @Override - protected void onPostExecute(Void v) { - Snackbar.make(holder.title, R.string.remove_file_deleted, Snackbar.LENGTH_SHORT).show(); - updateDeleteButton(holder, module); - } - }.exec(); - } else { - new Async.RootTask() { - @Override - protected Void doInBackground(Void... voids) { - module.createRemoveFile(); - return null; - } - - @Override - protected void onPostExecute(Void v) { - Snackbar.make(holder.title, R.string.remove_file_created, Snackbar.LENGTH_SHORT).show(); - updateDeleteButton(holder, module); - } - }.exec(); + @Override + protected void onPostExecute(Void v) { + int title = isChecked ? R.string.disable_file_removed : R.string.disable_file_created; + Snackbar.make(holder.title, title, Snackbar.LENGTH_SHORT).show(); } - }); + }.exec()); + + holder.delete.setOnClickListener(v -> new Async.RootTask() { + private final boolean removed = module.willBeRemoved(); + + @Override + protected Void doInBackground(Void... voids) { + if (removed) { + module.deleteRemoveFile(); + } else { + module.createRemoveFile(); + } + return null; + } + + @Override + protected void onPostExecute(Void v) { + int title = removed ? R.string.remove_file_deleted : R.string.remove_file_created; + Snackbar.make(holder.title, title, Snackbar.LENGTH_SHORT).show(); + updateDeleteButton(holder, module); + } + }.exec()); if (module.isUpdated()) { holder.notice.setVisibility(View.VISIBLE);