From 66c6eb8158ef250e3e60a4d2120da2b1909463d2 Mon Sep 17 00:00:00 2001 From: Brayan Oliveira <69634269+brayandso@users.noreply.github.com> Date: Fri, 15 Dec 2023 05:31:36 -0300 Subject: [PATCH] feat: add "Flag" option to the new previewer --- .../ichi2/anki/previewer/PreviewerFragment.kt | 19 +++++++++ .../anki/previewer/PreviewerViewModel.kt | 16 ++++++++ AnkiDroid/src/main/res/menu/previewer2.xml | 39 +++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerFragment.kt b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerFragment.kt index 877aa0f1d4bf..cd6eb6ed2f4a 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerFragment.kt @@ -39,6 +39,7 @@ import com.google.android.material.button.MaterialButton import com.google.android.material.slider.Slider import com.google.android.material.textview.MaterialTextView import com.ichi2.anki.CollectionHelper +import com.ichi2.anki.Flag import com.ichi2.anki.NoteEditor import com.ichi2.anki.R import com.ichi2.anki.SingleFragmentActivity @@ -62,6 +63,9 @@ class PreviewerFragment : Fragment(), Toolbar.OnMenuItemClickListener { private val markOption: MenuItem get() = menu.findItem(R.id.action_mark) + private val flagOption: MenuItem + get() = menu.findItem(R.id.action_flag) + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -159,6 +163,13 @@ class PreviewerFragment : Fragment(), Toolbar.OnMenuItemClickListener { } } } + lifecycleScope.launch { + viewModel.flagCode + .flowWithLifecycle(lifecycle) + .collectLatest { flagCode -> + flagOption.setIcon(Flag.fromCode(flagCode).drawableRes) + } + } if (cardsCount == 1) { slider.visibility = View.GONE @@ -203,6 +214,14 @@ class PreviewerFragment : Fragment(), Toolbar.OnMenuItemClickListener { R.id.action_edit -> editCard() R.id.action_mark -> viewModel.toggleMark() R.id.action_back_side_only -> viewModel.toggleBacksideOnly() + R.id.action_flag_zero -> viewModel.setFlag(Flag.NONE) + R.id.action_flag_one -> viewModel.setFlag(Flag.RED) + R.id.action_flag_two -> viewModel.setFlag(Flag.ORANGE) + R.id.action_flag_three -> viewModel.setFlag(Flag.GREEN) + R.id.action_flag_four -> viewModel.setFlag(Flag.BLUE) + R.id.action_flag_five -> viewModel.setFlag(Flag.PINK) + R.id.action_flag_six -> viewModel.setFlag(Flag.TURQUOISE) + R.id.action_flag_seven -> viewModel.setFlag(Flag.PURPLE) } return true } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerViewModel.kt b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerViewModel.kt index 0b9e59a9911f..13119e7c64ce 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerViewModel.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerViewModel.kt @@ -24,6 +24,7 @@ import androidx.lifecycle.viewmodel.viewModelFactory import com.google.android.material.color.MaterialColors.getColor import com.ichi2.anki.AnkiDroidApp import com.ichi2.anki.CollectionManager.withCol +import com.ichi2.anki.Flag import com.ichi2.anki.LanguageUtils import com.ichi2.anki.servicelayer.MARKED_TAG import com.ichi2.libanki.Card @@ -48,6 +49,7 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra val currentIndex = MutableStateFlow(firstIndex) val backsideOnly = MutableStateFlow(false) val isMarked = MutableStateFlow(false) + val flagCode: MutableStateFlow = MutableStateFlow(Flag.NONE.code) private var showingAnswer = false @@ -81,6 +83,15 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra } } + fun setFlag(flag: Flag) { + launchCatching { + withCol { + setUserFlagForCards(listOf(currentCard.id), flag.code) + } + flagCode.emit(flag.code) + } + } + fun serverBaseUrl() = server.baseUrl() fun cardId() = currentCard.id @@ -104,6 +115,10 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra } } + private suspend fun updateFlagIcon() { + flagCode.emit(currentCard.userFlag()) + } + private suspend fun updateMarkIcon() { isMarked.emit(currentCard.note().hasTag(MARKED_TAG)) } @@ -118,6 +133,7 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra eval.emit("_showQuestion(${Json.encodeToString(question)}, ${Json.encodeToString(answer)}, '$bodyClass');") + updateFlagIcon() updateMarkIcon() } diff --git a/AnkiDroid/src/main/res/menu/previewer2.xml b/AnkiDroid/src/main/res/menu/previewer2.xml index 318330eb5d06..7643e7472d9b 100644 --- a/AnkiDroid/src/main/res/menu/previewer2.xml +++ b/AnkiDroid/src/main/res/menu/previewer2.xml @@ -21,6 +21,45 @@ android:icon="@drawable/ic_card_question" android:title="@string/card_side_both" app:showAsAction="always"/> + + + + + + + + + + + +