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.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) {
new Async.RootTask<Void, Void, Void>() {
@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<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();
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
protected Void doInBackground(Void... voids) {
if (removed) {
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();
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();
}
});
}.exec());
if (module.isUpdated()) {
holder.notice.setVisibility(View.VISIBLE);