Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add next card due message #16566

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions AnkiDroid/src/main/java/com/ichi2/anki/pages/CongratsPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@
*/
package com.ichi2.anki.pages

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.webkit.JavascriptInterface
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import anki.collection.OpChanges
import com.google.android.material.appbar.MaterialToolbar
import com.ichi2.anki.CollectionManager
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.CollectionManager.withCol
import com.ichi2.anki.DeckPicker
import com.ichi2.anki.FilteredDeckOptions
Expand All @@ -40,16 +41,21 @@ import com.ichi2.anki.SingleFragmentActivity
import com.ichi2.anki.StudyOptionsActivity
import com.ichi2.anki.dialogs.customstudy.CustomStudyDialog
import com.ichi2.anki.launchCatchingIO
import com.ichi2.anki.launchCatchingTask
import com.ichi2.anki.preferences.sharedPrefs
import com.ichi2.anki.showThemedToast
import com.ichi2.anki.snackbar.showSnackbar
import com.ichi2.anki.utils.SECONDS_PER_DAY
import com.ichi2.anki.utils.TIME_HOUR
import com.ichi2.anki.utils.TIME_MINUTE
import com.ichi2.libanki.ChangeManager
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.undoableOp
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import timber.log.Timber
import kotlin.math.round

class CongratsPage :
PageFragment(),
Expand Down Expand Up @@ -183,28 +189,54 @@ class CongratsPage :
private fun displayNewCongratsScreen(context: Context): Boolean =
context.sharedPrefs().getBoolean("new_congrats_screen", false)

fun display(activity: Activity) {
fun display(activity: FragmentActivity) {
if (displayNewCongratsScreen(activity)) {
activity.startActivity(getIntent(activity))
} else {
showThemedToast(activity, R.string.studyoptions_congrats_finished, false)
activity.launchCatchingTask {
val message = getDeckFinishedMessage(activity)
showThemedToast(activity, message, false)
}
}
}

fun onReviewsCompleted(activity: Activity, cardsInDeck: Boolean) {
fun onReviewsCompleted(activity: FragmentActivity, cardsInDeck: Boolean) {
if (displayNewCongratsScreen(activity)) {
activity.startActivity(getIntent(activity))
return
}

// Show a message when reviewing has finished
if (cardsInDeck) {
activity.showSnackbar(R.string.studyoptions_congrats_finished)
activity.launchCatchingTask {
val message = getDeckFinishedMessage(activity)
activity.showSnackbar(message)
}
} else {
activity.showSnackbar(R.string.studyoptions_no_cards_due)
}
}

// based in https://github.com/ankitects/anki/blob/9b4dd54312de8798a3f2bee07892bb3a488d1f9b/ts/routes/congrats/lib.ts#L8C17-L8C34
private suspend fun getDeckFinishedMessage(activity: FragmentActivity): String {
val info = withCol { sched.congratulationsInfo() }
val secsUntilNextLearn = info.secsUntilNextLearn
if (secsUntilNextLearn >= SECONDS_PER_DAY) {
return activity.getString(R.string.studyoptions_congrats_finished)
}
// https://github.com/ankitects/anki/blob/9b4dd54312de8798a3f2bee07892bb3a488d1f9b/ts/lib/tslib/time.ts#L22
val (unit, amount) = if (secsUntilNextLearn < TIME_MINUTE) {
"seconds" to secsUntilNextLearn.toDouble()
} else if (secsUntilNextLearn < TIME_HOUR) {
"minutes" to secsUntilNextLearn / TIME_MINUTE
} else {
"hours" to secsUntilNextLearn / TIME_HOUR
}

val nextLearnDue = TR.schedulingNextLearnDue(unit, round(amount).toInt())
return activity.getString(R.string.studyoptions_congrats_next_due_in, nextLearnDue)
}

fun DeckPicker.onDeckCompleted() {
startActivity(getIntent(this))
}
Expand Down
6 changes: 3 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/utils/Time.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ private const val TIME_DAY_LONG = 24 * TIME_HOUR_LONG
// These are doubles on purpose because we want a rounded, not integer result later.
// Use values from Anki Desktop:
// https://github.com/ankitects/anki/blob/05cc47a5d3d48851267cda47f62af79f468eb028/rslib/src/sched/timespan.rs#L83
private const val TIME_MINUTE = 60.0 // seconds
private const val TIME_HOUR = 60.0 * TIME_MINUTE
private const val TIME_DAY = 24.0 * TIME_HOUR
const val TIME_MINUTE = 60.0 // seconds
const val TIME_HOUR = 60.0 * TIME_MINUTE
const val TIME_DAY = 24.0 * TIME_HOUR
private const val TIME_MONTH = 30.0 * TIME_DAY
private const val TIME_YEAR = 12.0 * TIME_MONTH

Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/01-core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
<string name="search_for_download_deck" comment="Deck search value for downloading deck">Deck Search</string>
<string name="invalid_deck_name">Invalid deck name</string>
<string name="studyoptions_congrats_finished">Congratulations! You have finished for now.</string>
<string name="studyoptions_congrats_next_due_in" comment="The param will be replaced with 'The next card will be ready in X time'">Deck finished for now! %s</string>
<string name="studyoptions_no_cards_due">No cards are due yet</string>

<string name="sd_card_not_mounted">Device storage not mounted</string>
Expand Down