From fdb266b78c7bf7d5732c813c4df7bf0be1d0e5d8 Mon Sep 17 00:00:00 2001 From: Brayan Oliveira <69634269+brayandso@users.noreply.github.com> Date: Wed, 13 Dec 2023 20:33:51 -0300 Subject: [PATCH] feat: add flag option to the new previewer --- .../ichi2/anki/previewer/PreviewerFragment.kt | 27 +++++++++++++ .../anki/previewer/PreviewerViewModel.kt | 15 ++++++- AnkiDroid/src/main/res/menu/previewer2.xml | 39 +++++++++++++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) 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..e481e9d41179 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerFragment.kt @@ -39,9 +39,18 @@ 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_BLUE +import com.ichi2.anki.FLAG_GREEN +import com.ichi2.anki.FLAG_NONE +import com.ichi2.anki.FLAG_ORANGE +import com.ichi2.anki.FLAG_PINK +import com.ichi2.anki.FLAG_PURPLE +import com.ichi2.anki.FLAG_RED +import com.ichi2.anki.FLAG_TURQUOISE import com.ichi2.anki.NoteEditor import com.ichi2.anki.R import com.ichi2.anki.SingleFragmentActivity +import com.ichi2.anki.getFlagDrawable import com.ichi2.anki.previewer.PreviewerViewModel.Companion.stdHtml import com.ichi2.themes.Themes import kotlinx.coroutines.flow.collectLatest @@ -62,6 +71,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 +171,13 @@ class PreviewerFragment : Fragment(), Toolbar.OnMenuItemClickListener { } } } + lifecycleScope.launch { + viewModel.cardFlag + .flowWithLifecycle(lifecycle) + .collectLatest { flag -> + flagOption.setIcon(getFlagDrawable(flag)) + } + } if (cardsCount == 1) { slider.visibility = View.GONE @@ -203,6 +222,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 8a69d3dc97bd..0e120943be03 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerViewModel.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerViewModel.kt @@ -24,8 +24,9 @@ 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_NONE +import com.ichi2.anki.Flag import com.ichi2.anki.LanguageUtils -import com.ichi2.anki.R import com.ichi2.anki.servicelayer.MARKED_TAG import com.ichi2.libanki.Card import com.ichi2.libanki.addPlayButtons @@ -49,6 +50,7 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra val currentIndex = MutableStateFlow(firstIndex) val backsideOnly = MutableStateFlow(false) val isMarked = MutableStateFlow(false) + val cardFlag = MutableStateFlow(FLAG_NONE) private var showingAnswer = false @@ -82,6 +84,15 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra } } + fun setFlag(@Flag flag: Int) { + launchCatching { + withCol { + setUserFlagForCards(listOf(currentCard.id), flag) + } + cardFlag.emit(flag) + } + } + fun serverBaseUrl() = server.baseUrl() fun cardId() = currentCard.id @@ -106,7 +117,7 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra } private suspend fun updateFlagIcon() { - eval.emit("_drawFlag(${currentCard.userFlag()});") + cardFlag.emit(currentCard.userFlag()) } private suspend fun 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"/> + + + + + + + + + + + +