Skip to content

Commit

Permalink
Hook apkg/colpkg exports up to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
dae committed Jul 22, 2022
1 parent a07794a commit e10448b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
38 changes: 27 additions & 11 deletions AnkiDroid/src/main/java/com/ichi2/anki/BackendImporting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package com.ichi2.anki
import anki.import_export.ImportResponse
import com.ichi2.anki.CollectionManager.withColNew
import com.ichi2.libanki.exportAnkiPackage
import com.ichi2.libanki.exportCollectionPackage
import com.ichi2.libanki.importAnkiPackage
import com.ichi2.libanki.undoableOp
import net.ankiweb.rsdroid.Translations
Expand Down Expand Up @@ -63,23 +64,38 @@ private fun summarizeReport(tr: Translations, output: ImportResponse): String {
return msgs.joinToString("\n")
}

fun DeckPicker.exportApkg(
suspend fun AnkiActivity.exportApkg(
apkgPath: String,
withScheduling: Boolean,
withMedia: Boolean,
deckId: Long?
) {
launchCatchingTask {
withProgress(
extractProgress = {
if (progress.hasExporting()) {
text = progress.exporting
}
},
) {
withColNew {
exportAnkiPackage(apkgPath, withScheduling, withMedia, deckId)
withProgress(
extractProgress = {
if (progress.hasExporting()) {
text = progress.exporting
}
},
) {
withColNew {
exportAnkiPackage(apkgPath, withScheduling, withMedia, deckId)
}
}
}

suspend fun AnkiActivity.exportColpkg(
colpkgPath: String,
withMedia: Boolean,
) {
withProgress(
extractProgress = {
if (progress.hasExporting()) {
text = progress.exporting
}
},
) {
withColNew {
exportCollectionPackage(colpkgPath, withMedia, true)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.StringRes
import androidx.core.app.ShareCompat.IntentBuilder
import androidx.core.content.FileProvider
import com.ichi2.anki.AnkiActivity
import com.ichi2.anki.DeckPicker
import com.ichi2.anki.R
import com.ichi2.anki.*
import com.ichi2.anki.UIUtils.showSimpleSnackbar
import com.ichi2.anki.UIUtils.showThemedToast
import com.ichi2.anki.dialogs.ExportCompleteDialog.ExportCompleteDialogListener
import com.ichi2.anki.dialogs.ExportDialog.ExportDialogListener
import com.ichi2.anki.exportApkg
import com.ichi2.async.CollectionTask.ExportApkg
import com.ichi2.async.TaskManager
import com.ichi2.compat.CompatHelper
Expand Down Expand Up @@ -102,11 +99,15 @@ class ActivityExportingDelegate(private val activity: AnkiActivity, private val
exportListener
)
} else {
// TODO: this code needs reworking so that the post-export dialogs can be
// shown correctly
(activity as DeckPicker).exportApkg(exportPath.path, includeSched, includeMedia, did)
// exportListener.actualOnPreExecute(activity)
// exportListener.actualOnPostExecute(activity, android.util.Pair(false, exportPath.path))
activity.launchCatchingTask {
if (did == null && includeSched) {
activity.exportColpkg(exportPath.path, includeMedia)
} else {
activity.exportApkg(exportPath.path, includeSched, includeMedia, did)
}
val dialog = mDialogsFactory.newExportCompleteDialog().withArguments(exportPath.path)
activity.showAsyncDialogFragment(dialog)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,22 @@ fun importCollectionPackage(
}

/**
* Export the collection into a .colpkg file. This closes the collection (since it may need to
* downgrade the schema for a legacy export), and col.reopen() must be called afterwards.
* * If legacy=false, a file targeting Anki 2.1.50+ is created. It compresses better and is faster to
* Export the collection into a .colpkg file.
* If legacy=false, a file targeting Anki 2.1.50+ is created. It compresses better and is faster to
* create, but older clients can not read it.
*/
fun CollectionV16.exportCollectionPackage(
outPath: String,
includeMedia: Boolean,
legacy: Boolean = true
) {
close(save = true, downgrade = false, forFullSync = true)
backend.exportCollectionPackage(
outPath = outPath,
includeMedia = includeMedia,
legacy = legacy
)
reopen(afterFullSync = false)
}

/**
Expand Down

0 comments on commit e10448b

Please sign in to comment.