Skip to content

Commit

Permalink
feat(new reviewer): bury
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed May 27, 2024
1 parent 9a31b5a commit 166fde4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
10 changes: 10 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/servicelayer/NoteService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,13 @@ fun Card.totalLapsesOfNote(col: Collection) = NoteService.totalLapses(col, note(
fun Card.totalReviewsForNote(col: Collection) = NoteService.totalReviews(col, note(col))

fun Card.avgIntervalOfNote(col: Collection) = NoteService.avgInterval(col, note(col))

suspend fun isBuryNoteAvailable(card: Card): Boolean {
return withCol {
db.queryScalar(
"select 1 from cards where nid = ? and id != ? and queue >= " + Consts.QUEUE_TYPE_NEW + " limit 1",
card.nid,
card.id
) == 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class ReviewerFragment :
override fun onMenuItemClick(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_add_note -> launchAddNote()
R.id.action_bury_card -> viewModel.buryCard()
R.id.action_bury_note -> viewModel.buryNote()
R.id.action_card_info -> launchCardInfo()
R.id.action_delete -> viewModel.deleteNote()
R.id.action_edit -> launchEditNote()
Expand Down Expand Up @@ -167,6 +169,19 @@ class ReviewerFragment :
markItem.setTitle(R.string.menu_mark_note)
}
}

val buryItem = menu.findItem(R.id.action_bury)
val buryCardItem = menu.findItem(R.id.action_bury_card)
viewModel.canBuryNoteFlow.flowWithLifecycle(lifecycle)
.collectLatestIn(lifecycleScope) { canBuryNote ->
if (canBuryNote) {
buryItem.isVisible = true
buryCardItem.isVisible = false
} else {
buryItem.isVisible = false
buryCardItem.isVisible = true
}
}
}

private val noteEditorLauncher =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import com.ichi2.anki.previewer.NoteEditorDestination
import com.ichi2.anki.reviewer.CardSide
import com.ichi2.anki.servicelayer.MARKED_TAG
import com.ichi2.anki.servicelayer.NoteService
import com.ichi2.anki.servicelayer.isBuryNoteAvailable
import com.ichi2.anki.servicelayer.isSuspendNoteAvailable
import com.ichi2.libanki.hasTag
import com.ichi2.libanki.note
import com.ichi2.libanki.sched.CurrentQueueState
Expand All @@ -61,6 +63,8 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
var isQueueFinishedFlow = MutableSharedFlow<Boolean>()
val isMarkedFlow = MutableStateFlow(false)
val actionFeedbackFlow = MutableSharedFlow<String>()
val canBuryNoteFlow = MutableStateFlow(true)
val canSuspendNoteFlow = MutableStateFlow(true)

private val server = AnkiServer(this).also { it.start() }
private val stateMutationKey = TimeManager.time.intTimeMS().toString()
Expand Down Expand Up @@ -169,6 +173,28 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
}
}

fun buryCard() {
launchCatchingIO {
val cardId = currentCard.await().id
val noteCount = undoableOp {
sched.buryCards(cids = listOf(cardId))
}.count
actionFeedbackFlow.emit(CollectionManager.TR.studyingCardsBuried(noteCount))
updateCurrentCard()
}
}

fun buryNote() {
launchCatchingIO {
val noteId = currentCard.await().nid
val noteCount = undoableOp {
sched.buryNotes(nids = listOf(noteId))
}.count
actionFeedbackFlow.emit(CollectionManager.TR.studyingCardsBuried(noteCount))
updateCurrentCard()
}
}

/* *********************************************************************************************
*************************************** Internal methods ***************************************
********************************************************************************************* */
Expand Down Expand Up @@ -257,10 +283,13 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
return
}

currentCard = CompletableDeferred(state.topCard)
val card = state.topCard
currentCard = CompletableDeferred(card)
showQuestion()
loadAndPlaySounds(CardSide.QUESTION)
updateMarkedStatus()
canBuryNoteFlow.emit(isBuryNoteAvailable(card))
canSuspendNoteFlow.emit(isSuspendNoteAvailable(card))
}

// TODO
Expand Down
19 changes: 19 additions & 0 deletions AnkiDroid/src/main/res/menu/reviewer2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,23 @@
android:title="@string/menu_delete_note"
android:icon="@drawable/ic_delete_white"
app:showAsAction="never"/>
<item
android:id="@+id/action_bury_card"
android:title="@string/menu_bury_card"
android:icon="@drawable/ic_flip_to_back_white"
android:visible="false"
/>
<item
android:id="@+id/action_bury"
android:title="@string/menu_bury"
android:icon="@drawable/ic_flip_to_back_white">
<menu>
<item
android:id="@+id/action_bury_card"
android:title="@string/menu_bury_card" />
<item
android:id="@+id/action_bury_note"
android:title="@string/menu_bury_note" />
</menu>
</item>
</menu>

0 comments on commit 166fde4

Please sign in to comment.