Skip to content

Commit

Permalink
feat: add mark option to new previewer
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Dec 14, 2023
1 parent 627251d commit e49cadf
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Toolbar>(R.id.toolbar).menu

private val backsideOnlyOption: MenuItem
get() = requireView().findViewById<Toolbar>(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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,6 +48,7 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra
val onError = MutableSharedFlow<String>()
val currentIndex = MutableStateFlow(firstIndex)
val backsideOnly = MutableStateFlow(false)
val isMarked = MutableStateFlow(false)

private var showingAnswer = false

Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ object NoteService {
}
}

const val MARKED_TAG = "marked"

fun Card.totalLapsesOfNote() = NoteService.totalLapses(note())

fun Card.totalReviewsForNote() = NoteService.totalReviews(note())
Expand Down
5 changes: 5 additions & 0 deletions AnkiDroid/src/main/res/drawable/ic_star.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="?attr/colorControlNormal"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FFFFFF" android:pathData="M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z"/>
</vector>
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/res/drawable/ic_star_border_white.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<vector android:height="24dp" android:tint="?attr/colorControlNormal"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M22,9.24l-7.19,-0.62L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21 12,17.27 18.18,21l-1.63,-7.03L22,9.24zM12,15.4l-3.76,2.27 1,-4.28 -3.32,-2.88 4.38,-0.38L12,6.1l1.71,4.04 4.38,0.38 -3.32,2.88 1,4.28L12,15.4z"/>
<path android:fillColor="#FFFFFF" android:pathData="M22,9.24l-7.19,-0.62L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21 12,17.27 18.18,21l-1.63,-7.03L22,9.24zM12,15.4l-3.76,2.27 1,-4.28 -3.32,-2.88 4.38,-0.38L12,6.1l1.71,4.04 4.38,0.38 -3.32,2.88 1,4.28L12,15.4z"/>
</vector>
5 changes: 5 additions & 0 deletions AnkiDroid/src/main/res/menu/previewer2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
android:icon="@drawable/ic_card_question"
android:title="@string/card_side_both"
app:showAsAction="always"/>
<item
android:id="@+id/action_mark"
android:icon="@drawable/ic_star_border_white"
android:title="@string/menu_mark_note"
app:showAsAction="always"/>
<item
android:id="@+id/action_edit"
android:icon="@drawable/ic_mode_edit_white"
Expand Down

0 comments on commit e49cadf

Please sign in to comment.