Simplify listeners containing async tasks

This commit is contained in:
tonymanou 2017-01-09 22:36:06 +01:00 committed by topjohnwu
parent 7be5937aa0
commit f0e1a8823e

View File

@ -49,67 +49,44 @@ public class ModulesAdapter extends RecyclerView.Adapter<ModulesAdapter.ViewHold
holder.checkBox.setOnCheckedChangeListener(null); holder.checkBox.setOnCheckedChangeListener(null);
holder.checkBox.setChecked(module.isEnabled()); holder.checkBox.setChecked(module.isEnabled());
holder.checkBox.setOnCheckedChangeListener((v, isChecked) -> { holder.checkBox.setOnCheckedChangeListener((v, isChecked) -> new Async.RootTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
if (isChecked) { if (isChecked) {
new Async.RootTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
module.removeDisableFile(); 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 { } else {
new Async.RootTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
module.createDisableFile(); module.createDisableFile();
}
return null; return null;
} }
@Override @Override
protected void onPostExecute(Void v) { protected void onPostExecute(Void v) {
Snackbar.make(holder.title, R.string.disable_file_created, Snackbar.LENGTH_SHORT).show(); int title = isChecked ? R.string.disable_file_removed : R.string.disable_file_created;
Snackbar.make(holder.title, title, Snackbar.LENGTH_SHORT).show();
} }
}.exec(); }.exec());
}
}); holder.delete.setOnClickListener(v -> new Async.RootTask<Void, Void, Void>() {
private final boolean removed = module.willBeRemoved();
holder.delete.setOnClickListener(v -> {
if (module.willBeRemoved()) {
new Async.RootTask<Void, Void, Void>() {
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
if (removed) {
module.deleteRemoveFile(); 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 { } else {
new Async.RootTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
module.createRemoveFile(); module.createRemoveFile();
}
return null; return null;
} }
@Override @Override
protected void onPostExecute(Void v) { protected void onPostExecute(Void v) {
Snackbar.make(holder.title, R.string.remove_file_created, Snackbar.LENGTH_SHORT).show(); 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); updateDeleteButton(holder, module);
} }
}.exec(); }.exec());
}
});
if (module.isUpdated()) { if (module.isUpdated()) {
holder.notice.setVisibility(View.VISIBLE); holder.notice.setVisibility(View.VISIBLE);