Skip to content

Commit

Permalink
feat(new reviewer): next times
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Jun 23, 2024
1 parent b308900 commit 89cdbab
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2024 Brayan Oliveira <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ichi2.anki.ui.windows.reviewer

import com.ichi2.anki.CollectionManager.withCol
import com.ichi2.libanki.sched.CurrentQueueState

data class AnswerButtonsNextTime(
val again: String,
val hard: String,
val good: String,
val easy: String
) {
companion object {
suspend fun from(state: CurrentQueueState): AnswerButtonsNextTime {
val (again, hard, good, easy) = withCol { sched.describeNextStates(state.states) }
return AnswerButtonsNextTime(again = again, hard = hard, good = good, easy = easy)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import android.view.MenuItem
import android.view.View
import android.webkit.WebView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.StringRes
import androidx.appcompat.view.menu.MenuBuilder
import androidx.appcompat.widget.ThemeUtils
import androidx.appcompat.widget.Toolbar
Expand Down Expand Up @@ -146,19 +147,32 @@ class ReviewerFragment :
}

private fun setupAnswerButtons(view: View) {
view.findViewById<MaterialButton>(R.id.again_button).setOnClickListener {
viewModel.answerAgain()
fun MaterialButton.setAnswerButtonNextTime(@StringRes title: Int, nextTime: String?) {
val titleString = context.getString(title)
text = ReviewerViewModel.buildAnswerButtonText(titleString, nextTime)
}
view.findViewById<MaterialButton>(R.id.hard_button).setOnClickListener {
viewModel.answerHard()

val againButton = view.findViewById<MaterialButton>(R.id.again_button).apply {
setOnClickListener { viewModel.answerAgain() }
}
val hardButton = view.findViewById<MaterialButton>(R.id.hard_button).apply {
setOnClickListener { viewModel.answerHard() }
}
view.findViewById<MaterialButton>(R.id.good_button).setOnClickListener {
viewModel.answerGood()
val goodButton = view.findViewById<MaterialButton>(R.id.good_button).apply {
setOnClickListener { viewModel.answerGood() }
}
view.findViewById<MaterialButton>(R.id.easy_button).setOnClickListener {
viewModel.answerEasy()
val easyButton = view.findViewById<MaterialButton>(R.id.easy_button).apply {
setOnClickListener { viewModel.answerEasy() }
}

viewModel.answerButtonsNextTimeFlow.flowWithLifecycle(lifecycle)
.collectIn(lifecycleScope) { times ->
againButton.setAnswerButtonNextTime(R.string.ease_button_again, times?.again)
hardButton.setAnswerButtonNextTime(R.string.ease_button_hard, times?.hard)
goodButton.setAnswerButtonNextTime(R.string.ease_button_good, times?.good)
easyButton.setAnswerButtonNextTime(R.string.ease_button_easy, times?.easy)
}

val showAnswerButton = view.findViewById<MaterialButton>(R.id.show_answer).apply {
setOnClickListener {
viewModel.showAnswer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.ichi2.anki.ui.windows.reviewer

import android.text.style.RelativeSizeSpan
import androidx.core.text.buildSpannedString
import androidx.core.text.inSpans
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
Expand Down Expand Up @@ -91,6 +94,11 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
*/
private var statesMutated = true

val answerButtonsNextTimeFlow: MutableStateFlow<AnswerButtonsNextTime?> = MutableStateFlow(null)
private val shouldShowNextTimes: Deferred<Boolean> = asyncIO {
withCol { config.get("estTimes") ?: true }
}

init {
ChangeManager.subscribe(this)
launchCatchingIO {
Expand Down Expand Up @@ -120,6 +128,7 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
while (!statesMutated) {
delay(50)
}
updateNextTimes()
showAnswerInternal()
loadAndPlaySounds(CardSide.ANSWER)
}
Expand Down Expand Up @@ -372,6 +381,14 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
redoLabelFlow.emit(withCol { redoLabel() })
}

private suspend fun updateNextTimes() {
if (!shouldShowNextTimes.await()) return
val state = queueState.await() ?: return

val nextTimes = AnswerButtonsNextTime.from(state)
answerButtonsNextTimeFlow.emit(nextTimes)
}

override fun opExecuted(changes: OpChanges, handler: Any?) {
launchCatchingIO { updateUndoAndRedoLabels() }
}
Expand All @@ -384,5 +401,19 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
}
}
}

fun buildAnswerButtonText(title: String, nextTime: String?): CharSequence {
return if (nextTime != null) {
buildSpannedString {
inSpans(RelativeSizeSpan(0.8F)) {
append(nextTime)
}
append("\n")
append(title)
}
} else {
title
}
}
}
}

0 comments on commit 89cdbab

Please sign in to comment.