Skip to content

Commit

Permalink
feat(deck-options): progress dialog for Optimize
Browse files Browse the repository at this point in the history
`computeFsrsParams` now uses the same output as `updateDeckConfigsRaw`

* move from a collection to a UI method

Fixes 16642
  • Loading branch information
david-allison committed Jan 14, 2025
1 parent 1969ed9 commit b1cc7cb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
61 changes: 43 additions & 18 deletions AnkiDroid/src/main/java/com/ichi2/anki/pages/DeckOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ import android.webkit.WebView
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.FragmentActivity
import anki.collection.OpChanges
import anki.collection.Progress
import com.ichi2.anki.CollectionManager
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.CollectionManager.withCol
import com.ichi2.anki.CrashReportService
import com.ichi2.anki.ProgressContext
import com.ichi2.anki.R
import com.ichi2.anki.dialogs.DiscardChangesDialog
import com.ichi2.anki.launchCatchingTask
import com.ichi2.anki.utils.openUrl
import com.ichi2.anki.withProgress
import com.ichi2.annotations.NeedsTest
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.sched.computeFsrsParamsRaw
import com.ichi2.libanki.undoableOp
import com.ichi2.libanki.updateDeckConfigsRaw
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -236,24 +240,7 @@ suspend fun FragmentActivity.updateDeckConfigsRaw(input: ByteArray): ByteArray {
withContext(Dispatchers.Main) {
withProgress(
extractProgress = {
text =
if (progress.hasComputeParams()) {
val value = progress.computeParams
val label =
TR.deckConfigOptimizingPreset(
currentCount = value.currentPreset,
totalCount = value.totalPresets,
)
val pct = if (value.total > 0) (value.current.toDouble() / value.total.toDouble() * 100.0) else 0.0
val reviewsLabel =
TR.deckConfigPercentOfReviews(
pct = "%.1f".format(pct),
reviews = value.reviews,
)
label + "\n" + reviewsLabel
} else {
getString(R.string.dialog_processing)
}
text = this.toOptimizingPresetString() ?: getString(R.string.dialog_processing)
},
) {
withContext(Dispatchers.IO) {
Expand All @@ -265,3 +252,41 @@ suspend fun FragmentActivity.updateDeckConfigsRaw(input: ByteArray): ByteArray {
withContext(Dispatchers.Main) { finish() }
return output
}

suspend fun FragmentActivity.computeFsrsParams(input: ByteArray): ByteArray =
withContext(Dispatchers.Main) {
withProgress(extractProgress = {
text = this.toOptimizingPresetString() ?: getString(R.string.dialog_processing)
}) {
withContext(Dispatchers.IO) {
withCol { computeFsrsParamsRaw(input) }
}
}
}

/**
* ```
* Optimizing preset 1/20
* 5.2% of 1000 reviews
* ```
*
* @return the above string, or `null` if [ProgressContext] has no
* [compute parameters][Progress.hasComputeParams]
*/
private fun ProgressContext.toOptimizingPresetString(): String? {
if (!progress.hasComputeParams()) return null

val value = progress.computeParams
val label =
TR.deckConfigOptimizingPreset(
currentCount = value.currentPreset,
totalCount = value.totalPresets,
)
val pct = if (value.total > 0) (value.current.toDouble() / value.total.toDouble() * 100.0) else 0.0
val reviewsLabel =
TR.deckConfigPercentOfReviews(
pct = "%.1f".format(pct),
reviews = value.reviews,
)
return label + "\n" + reviewsLabel
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import com.ichi2.libanki.getDeckNamesRaw
import com.ichi2.libanki.getFieldNamesRaw
import com.ichi2.libanki.getImportAnkiPackagePresetsRaw
import com.ichi2.libanki.getNotetypeNamesRaw
import com.ichi2.libanki.sched.computeFsrsParamsRaw
import com.ichi2.libanki.sched.computeOptimalRetentionRaw
import com.ichi2.libanki.sched.evaluateParamsRaw
import com.ichi2.libanki.sched.simulateFsrsReviewRaw
Expand Down Expand Up @@ -77,7 +76,6 @@ val collectionMethods =
"getFieldNames" to { bytes -> getFieldNamesRaw(bytes) },
"cardStats" to { bytes -> cardStatsRaw(bytes) },
"getDeckConfigsForUpdate" to { bytes -> getDeckConfigsForUpdateRaw(bytes) },
"computeFsrsParams" to { bytes -> computeFsrsParamsRaw(bytes) },
"computeOptimalRetention" to { bytes -> computeOptimalRetentionRaw(bytes) },
"evaluateParams" to { bytes -> evaluateParamsRaw(bytes) },
"simulateFsrsReview" to { bytes -> simulateFsrsReviewRaw(bytes) },
Expand Down Expand Up @@ -124,6 +122,7 @@ val uiMethods =
withCol { updateImageOcclusionNoteRaw(bytes) }
}
},
"computeFsrsParams" to { bytes -> lifecycleScope.async { computeFsrsParams(bytes) } },
)

suspend fun FragmentActivity?.handleUiPostRequest(
Expand Down

0 comments on commit b1cc7cb

Please sign in to comment.