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>() {
if (isChecked) { @Override
new Async.RootTask<Void, Void, Void>() { protected Void doInBackground(Void... voids) {
@Override if (isChecked) {
protected Void doInBackground(Void... voids) { module.removeDisableFile();
module.removeDisableFile(); } else {
return null; module.createDisableFile();
} }
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<Void, Void, Void>() {
@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.delete.setOnClickListener(v -> { @Override
if (module.willBeRemoved()) { protected void onPostExecute(Void v) {
new Async.RootTask<Void, Void, Void>() { int title = isChecked ? R.string.disable_file_removed : R.string.disable_file_created;
@Override Snackbar.make(holder.title, title, Snackbar.LENGTH_SHORT).show();
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<Void, Void, Void>() {
@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();
} }
}); }.exec());
holder.delete.setOnClickListener(v -> new Async.RootTask<Void, Void, Void>() {
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()) { if (module.isUpdated()) {
holder.notice.setVisibility(View.VISIBLE); holder.notice.setVisibility(View.VISIBLE);