From 8d626d1593b4364f1fb29b21925447d305b83bf2 Mon Sep 17 00:00:00 2001 From: lukstbit <52494258+lukstbit@users.noreply.github.com> Date: Fri, 18 Oct 2024 12:28:21 +0300 Subject: [PATCH] Fix learn ahead limit not being applied after setting change The preference was changed in the backend but the scheduler didn't update itself so the learning cards that should have become available weren't present in the review queue. The preference change is now done through get/setPreferences(). Fixes #16645 --- .../preferences/ReviewingSettingsFragment.kt | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/preferences/ReviewingSettingsFragment.kt b/AnkiDroid/src/main/java/com/ichi2/anki/preferences/ReviewingSettingsFragment.kt index 8c2421aa4c49..95a89e98528b 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/preferences/ReviewingSettingsFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/preferences/ReviewingSettingsFragment.kt @@ -15,13 +15,20 @@ */ package com.ichi2.anki.preferences +import anki.config.copy import com.ichi2.anki.CollectionManager.withCol import com.ichi2.anki.R import com.ichi2.anki.launchCatchingTask import com.ichi2.anki.preferences.Preferences.Companion.getDayOffset import com.ichi2.anki.preferences.Preferences.Companion.setDayOffset +import com.ichi2.annotations.NeedsTest +import com.ichi2.libanki.undoableOp import com.ichi2.preferences.NumberRangePreferenceCompat import com.ichi2.preferences.SliderPreference +import timber.log.Timber +import kotlin.time.Duration +import kotlin.time.DurationUnit +import kotlin.time.toDuration class ReviewingSettingsFragment : SettingsFragment() { override val preferenceResource: Int @@ -34,10 +41,11 @@ class ReviewingSettingsFragment : SettingsFragment() { // Represents the collections pref "collapseTime": i.e. // if there are no card to review now, but there are learning cards remaining for today, we show those learning cards if they are due before LEARN_CUTOFF minutes // Note that "collapseTime" is in second while LEARN_CUTOFF is in minute. + @NeedsTest("#16645, changing the learn ahead limit shows expected cards in review queue") requirePreference(R.string.learn_cutoff_preference).apply { - launchCatchingTask { setValue(withCol { sched.learnAheadSeconds() / 60 }) } + launchCatchingTask { setValue(getLearnAheadLimit().toInt(DurationUnit.MINUTES)) } setOnPreferenceChangeListener { newValue -> - launchCatchingTask { withCol { config.set("collapseTime", (newValue as Int * 60)) } } + launchCatchingTask { setLearnAheadLimit((newValue as Int).toDuration(DurationUnit.MINUTES)) } } } // Timebox time limit @@ -60,4 +68,17 @@ class ReviewingSettingsFragment : SettingsFragment() { } } } + + private suspend fun getLearnAheadLimit(): Duration = + withCol { getPreferences().scheduling.learnAheadSecs }.toDuration(DurationUnit.SECONDS) + + private suspend fun setLearnAheadLimit(limit: Duration) { + val prefs = withCol { getPreferences() } + val newPrefs = prefs.copy { + scheduling = prefs.scheduling.copy { learnAheadSecs = limit.toInt(DurationUnit.SECONDS) } + } + + undoableOp { setPreferences(newPrefs) } + Timber.i("set learn ahead limit: '%d'", limit.toInt(DurationUnit.SECONDS)) + } }