From e49cadf98b5eef8d29f120087d85c9c6a0e4ef49 Mon Sep 17 00:00:00 2001 From: Brayan Oliveira <69634269+brayandso@users.noreply.github.com> Date: Wed, 13 Dec 2023 20:08:31 -0300 Subject: [PATCH] feat: add mark option to new previewer --- .../ichi2/anki/previewer/PreviewerFragment.kt | 24 +++++++++++++++++-- .../anki/previewer/PreviewerViewModel.kt | 22 ++++++++++++++++- .../ichi2/anki/servicelayer/NoteService.kt | 2 ++ AnkiDroid/src/main/res/drawable/ic_star.xml | 5 ++++ .../res/drawable/ic_star_border_white.xml | 2 +- AnkiDroid/src/main/res/menu/previewer2.xml | 5 ++++ 6 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 AnkiDroid/src/main/res/drawable/ic_star.xml 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 16d28fd01328..877aa0f1d4bf 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerFragment.kt @@ -19,6 +19,7 @@ import android.content.Context import android.content.Intent import android.os.Bundle import android.view.LayoutInflater +import android.view.Menu import android.view.MenuItem import android.view.View import android.view.ViewGroup @@ -52,9 +53,14 @@ import java.io.File class PreviewerFragment : Fragment(), Toolbar.OnMenuItemClickListener { private lateinit var viewModel: PreviewerViewModel + private val menu: Menu + get() = requireView().findViewById(R.id.toolbar).menu + private val backsideOnlyOption: MenuItem - get() = requireView().findViewById(R.id.toolbar) - .menu.findItem(R.id.action_back_side_only) + get() = menu.findItem(R.id.action_back_side_only) + + private val markOption: MenuItem + get() = menu.findItem(R.id.action_mark) override fun onCreateView( inflater: LayoutInflater, @@ -140,6 +146,19 @@ class PreviewerFragment : Fragment(), Toolbar.OnMenuItemClickListener { getString(R.string.preview_progress_bar_text, displayIndex, cardsCount) } } + lifecycleScope.launch { + viewModel.isMarked + .flowWithLifecycle(lifecycle) + .collectLatest { isMarked -> + if (isMarked) { + markOption.setIcon(R.drawable.ic_star) + markOption.setTitle(R.string.menu_unmark_note) + } else { + markOption.setIcon(R.drawable.ic_star_border_white) + markOption.setTitle(R.string.menu_mark_note) + } + } + } if (cardsCount == 1) { slider.visibility = View.GONE @@ -182,6 +201,7 @@ class PreviewerFragment : Fragment(), Toolbar.OnMenuItemClickListener { override fun onMenuItemClick(item: MenuItem): Boolean { when (item.itemId) { R.id.action_edit -> editCard() + R.id.action_mark -> viewModel.toggleMark() R.id.action_back_side_only -> viewModel.toggleBacksideOnly() } 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 b3b3243691f4..8a69d3dc97bd 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerViewModel.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerViewModel.kt @@ -25,8 +25,11 @@ import com.google.android.material.color.MaterialColors.getColor import com.ichi2.anki.AnkiDroidApp import com.ichi2.anki.CollectionManager.withCol 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 +import com.ichi2.libanki.undoableOp import com.ichi2.themes.Themes import com.ichi2.utils.toRGBHex import kotlinx.coroutines.CancellationException @@ -45,6 +48,7 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra val onError = MutableSharedFlow() val currentIndex = MutableStateFlow(firstIndex) val backsideOnly = MutableStateFlow(false) + val isMarked = MutableStateFlow(false) private var showingAnswer = false @@ -62,6 +66,22 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra } } + fun toggleMark() { + launchCatching { + val note = currentCard.note() + val hasMarkTag = note.hasTag(MARKED_TAG) + if (hasMarkTag) { + note.delTag(MARKED_TAG) + } else { + note.addTag(MARKED_TAG) + } + undoableOp { + updateNote(note) + } + isMarked.emit(!hasMarkTag) + } + } + fun serverBaseUrl() = server.baseUrl() fun cardId() = currentCard.id @@ -90,7 +110,7 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra } private suspend fun updateMarkIcon() { - eval.emit("_drawMark(${currentCard.note().hasTag("marked")})") + isMarked.emit(currentCard.note().hasTag(MARKED_TAG)) } private suspend fun showQuestion() { diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/servicelayer/NoteService.kt b/AnkiDroid/src/main/java/com/ichi2/anki/servicelayer/NoteService.kt index 723a1dd6bdfb..c5eaac980004 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/servicelayer/NoteService.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/servicelayer/NoteService.kt @@ -231,6 +231,8 @@ object NoteService { } } +const val MARKED_TAG = "marked" + fun Card.totalLapsesOfNote() = NoteService.totalLapses(note()) fun Card.totalReviewsForNote() = NoteService.totalReviews(note()) diff --git a/AnkiDroid/src/main/res/drawable/ic_star.xml b/AnkiDroid/src/main/res/drawable/ic_star.xml new file mode 100644 index 000000000000..8ecb6a11546a --- /dev/null +++ b/AnkiDroid/src/main/res/drawable/ic_star.xml @@ -0,0 +1,5 @@ + + + diff --git a/AnkiDroid/src/main/res/drawable/ic_star_border_white.xml b/AnkiDroid/src/main/res/drawable/ic_star_border_white.xml index b5396baf5d65..7fa5188f1e0a 100644 --- a/AnkiDroid/src/main/res/drawable/ic_star_border_white.xml +++ b/AnkiDroid/src/main/res/drawable/ic_star_border_white.xml @@ -1,5 +1,5 @@ - + diff --git a/AnkiDroid/src/main/res/menu/previewer2.xml b/AnkiDroid/src/main/res/menu/previewer2.xml index d93d522915c0..318330eb5d06 100644 --- a/AnkiDroid/src/main/res/menu/previewer2.xml +++ b/AnkiDroid/src/main/res/menu/previewer2.xml @@ -21,6 +21,11 @@ android:icon="@drawable/ic_card_question" android:title="@string/card_side_both" app:showAsAction="always"/> +