Skip to content

Commit

Permalink
Tweak withProgress() to work without collection; handle colpkg progress
Browse files Browse the repository at this point in the history
  • Loading branch information
dae committed Jun 22, 2022
1 parent 32b05ca commit f8839f8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 41 deletions.
13 changes: 11 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/BackendBackups.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.ichi2.libanki.CollectionV16
import com.ichi2.libanki.awaitBackupCompletion
import com.ichi2.libanki.createBackup
import kotlinx.coroutines.*
import timber.log.Timber

fun DeckPicker.performBackupInBackground() {
val col = CollectionHelper.getInstance().getCol(baseContext).newBackend
Expand All @@ -33,12 +34,20 @@ fun DeckPicker.performBackupInBackground() {
}
}

// TODO: show progress indicator
fun DeckPicker.importColpkg(colpkgPath: String) {
val deckPicker = this
catchingLifecycleScope(this) {
runInBackground {
CollectionHelper.getInstance().importColpkg(baseContext, colpkgPath)
val helper = CollectionHelper.getInstance()
val backend = helper.getOrCreateBackend(baseContext)
backend.withProgress({
if (it.hasImporting()) {
// TODO: show progress in GUI

This comment has been minimized.

Copy link
@mikehardy

mikehardy Jun 22, 2022

Member

could be captured as part of GSoC notification project - "there should be a generic facility for posting sticky foreground notifications that accept notification content updates (for progress indication)"

This comment has been minimized.

Copy link
@dae

dae Jun 23, 2022

Author Contributor

Agreed, though I'd prefer to leave the GSoC assignments up to you guys. My ideal interface would by similar to the computer version, eg:

  • prevent UI interaction while an operation is active (eg ignore taps)
  • show a progress pop-up after 600ms elapses (interaction blocking should happen even before it pops up)
  • provide a way to update the currently-displayed text, and ideally an optional progress bar
Timber.i("%s", it.importing)
}
}) {
helper.importColpkg(baseContext, colpkgPath)
}
deckPicker.updateDeckList()
}
}
Expand Down
37 changes: 21 additions & 16 deletions AnkiDroid/src/main/java/com/ichi2/anki/CoroutineHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import androidx.lifecycle.coroutineScope
import anki.collection.Progress
import com.ichi2.anki.UIUtils.showSimpleSnackbar
import com.ichi2.libanki.CollectionV16
import com.ichi2.libanki.getProgress
import kotlinx.coroutines.*
import net.ankiweb.rsdroid.Backend
import net.ankiweb.rsdroid.BackendException
import timber.log.Timber

/**
* Launch a job that catches any uncaught errors and reports them to the user.
Expand All @@ -52,30 +51,36 @@ suspend fun <T> runInBackground(block: suspend CoroutineScope.() -> T): T {
}
}

suspend fun <T> Backend.withProgress(onProgress: (Progress) -> Unit, block: suspend CoroutineScope.() -> T): T {
val backend = this
return coroutineScope {
val monitor = launch {
monitorProgress(backend, onProgress)
}
try {
block()
} finally {
monitor.cancel()
}
}
}

suspend fun <T> runInBackgroundWithProgress(
col: CollectionV16,
onProgress: (Progress) -> Unit,
op: suspend (CollectionV16) -> T
): T = coroutineScope {
val monitor = launch { monitorProgress(col, onProgress) }
try {
col.backend.withProgress(onProgress) {
runInBackground { op(col) }
} finally {
monitor.cancel()
}
}

suspend fun monitorProgress(col: CollectionV16, op: (Progress) -> Unit) {
suspend fun monitorProgress(backend: Backend, op: (Progress) -> Unit) {
while (true) {
try {
val progress = col.getProgress()
// on main thread, so op can update UI
withContext(Dispatchers.Main) {
op(progress)
}
} catch (exc: Exception) {
Timber.e("exception in monitorProgress: $exc")
return
val progress = backend.latestProgress()
// on main thread, so op can update UI
withContext(Dispatchers.Main) {
op(progress)
}
delay(100)
}
Expand Down
23 changes: 0 additions & 23 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Progress.kt

This file was deleted.

0 comments on commit f8839f8

Please sign in to comment.