Long press answer to make favorite

This commit is contained in:
M66B 2021-05-06 21:07:24 +02:00
parent 8bc5364828
commit 700110d194
2 changed files with 35 additions and 3 deletions

View File

@ -135,9 +135,11 @@ public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder
if (composable)
popupMenu.getMenu().add(Menu.NONE, R.string.title_compose, 1, R.string.title_compose);
popupMenu.getMenu().add(Menu.NONE, R.string.title_answer_hide, 2, R.string.title_answer_hide)
popupMenu.getMenu().add(Menu.NONE, R.string.title_answer_favorite, 2, R.string.title_answer_favorite)
.setCheckable(true).setChecked(answer.favorite);
popupMenu.getMenu().add(Menu.NONE, R.string.title_answer_hide, 3, R.string.title_answer_hide)
.setCheckable(true).setChecked(answer.hide);
popupMenu.getMenu().add(Menu.NONE, R.string.title_copy, 3, R.string.title_copy);
popupMenu.getMenu().add(Menu.NONE, R.string.title_copy, 4, R.string.title_copy);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
@ -146,6 +148,9 @@ public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder
if (itemId == R.string.title_compose) {
onActionCompose();
return true;
} else if (itemId == R.string.title_answer_favorite) {
onActionFavorite(!item.isChecked());
return true;
} else if (itemId == R.string.title_answer_hide) {
onActionHide(!item.isChecked());
return true;
@ -162,6 +167,30 @@ public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder
.putExtra("answer", answer.id));
}
private void onActionFavorite(boolean favorite) {
Bundle args = new Bundle();
args.putLong("id", answer.id);
args.putBoolean("favorite", favorite);
new SimpleTask<Boolean>() {
@Override
protected Boolean onExecute(Context context, Bundle args) {
long id = args.getLong("id");
boolean favorite = args.getBoolean("favorite");
DB db = DB.getInstance(context);
db.answer().setAnswerFavorite(id, favorite);
return favorite;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "answer:favorite");
}
private void onActionHide(boolean hide) {
Bundle args = new Bundle();
args.putLong("id", answer.id);
@ -183,7 +212,7 @@ public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "rule:enable");
}.execute(context, owner, args, "answer:hide");
}
private void onActionCopy() {

View File

@ -66,6 +66,9 @@ public interface DaoAnswer {
@Update
int updateAnswer(EntityAnswer answer);
@Query("UPDATE answer SET favorite = :favorite WHERE id = :id AND NOT (favorite IS :favorite)")
int setAnswerFavorite(long id, boolean favorite);
@Query("UPDATE answer SET hide = :hide WHERE id = :id AND NOT (hide IS :hide)")
int setAnswerHidden(long id, boolean hide);