Skip to content

Commit

Permalink
feat(export): support non-legacy exports [apkg/colpkg]
Browse files Browse the repository at this point in the history
The default is changed to 'non-legacy'

A checkbox is added for colpkg and apkg exports
to continue to support legacy exports

Matches Anki Desktop:
"Support older Anki versions (slower/ larger files)"

Fixes 17536
  • Loading branch information
Haz3-jolt authored and david-allison committed Dec 19, 2024
1 parent cabcf4f commit 70936db
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
9 changes: 6 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/BackendExporting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fun AnkiActivity.exportApkgPackage(
withDeckConfigs: Boolean,
withMedia: Boolean,
limit: ExportLimit,
legacy: Boolean,
) {
launchCatchingTask {
val onProgress: ProgressContext.() -> Unit = {
Expand All @@ -35,7 +36,7 @@ fun AnkiActivity.exportApkgPackage(
}
}
withProgress(extractProgress = onProgress) {
withCol { exportAnkiPackage(exportPath, withScheduling, withDeckConfigs, withMedia, limit) }
withCol { exportAnkiPackage(exportPath, withScheduling, withDeckConfigs, withMedia, limit, legacy) }
}
val factory =
(this@exportApkgPackage as ExportDialogsFactoryProvider).exportDialogsFactory()
Expand All @@ -47,23 +48,25 @@ fun AnkiActivity.exportApkgPackage(
suspend fun AnkiActivity.exportColpkg(
colpkgPath: String,
withMedia: Boolean,
legacy: Boolean,
) {
val onProgress: ProgressContext.() -> Unit = {
if (progress.hasExporting()) {
text = getString(R.string.export_preparation_in_progress)
}
}
withProgress(extractProgress = onProgress) {
withCol { exportCollectionPackage(colpkgPath, withMedia, true) }
withCol { exportCollectionPackage(colpkgPath, withMedia, legacy) }
}
}

fun AnkiActivity.exportCollectionPackage(
exportPath: String,
withMedia: Boolean,
legacy: Boolean,
) {
launchCatchingTask {
exportColpkg(exportPath, withMedia)
exportColpkg(exportPath, withMedia, legacy)
val factory =
(this@exportCollectionPackage as ExportDialogsFactoryProvider).exportDialogsFactory()
val dialog = factory.newExportReadyDialog().withArguments(exportPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import com.ichi2.anki.exportSelectedCards
import com.ichi2.anki.exportSelectedNotes
import com.ichi2.anki.ui.BasicItemSelectedListener
import com.ichi2.anki.utils.getTimestamp
import com.ichi2.annotations.NeedsTest
import com.ichi2.compat.CompatHelper.Companion.getSerializableCompat
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.DeckNameId
Expand Down Expand Up @@ -77,6 +78,8 @@ class ExportDialogFragment : DialogFragment() {
private lateinit var notesIncludeNotetypeName: CheckBox
private lateinit var notesIncludeUniqueIdentifier: CheckBox
private lateinit var cardsIncludeHtml: CheckBox
private lateinit var apkgExportLegacyCheckbox: CheckBox
private lateinit var collectionExportLegacyCheckbox: CheckBox

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialogView =
Expand Down Expand Up @@ -203,17 +206,27 @@ class ExportDialogFragment : DialogFragment() {
/**
* Initializes the views representing the extra options available when exporting a collection.
*/
@NeedsTest("Checkbox is only available on two selections ")
@NeedsTest("Checkbox defaults to false")
@NeedsTest("Checkbox value is provided to the correct export functions (true/false)")
private fun View.initializeCollectionExportUi() =
with(CollectionManager.TR) {
collectionIncludeMedia =
findViewById<CheckBox>(R.id.export_extras_collection_media).apply {
text = exportingIncludeMedia()
}
collectionExportLegacyCheckbox =
findViewById<CheckBox>(R.id.export_legacy_checkbox_collection).apply {
text = exportingSupportOlderAnkiVersions()
}
}

/**
* Initializes the views representing the extra options available when exporting an Anki package.
*/
@NeedsTest("Checkbox is only available on two selections ")
@NeedsTest("Checkbox defaults to false")
@NeedsTest("Checkbox value is provided to the correct export functions (true/false)")
private fun View.initializeApkgExportUi() =
with(CollectionManager.TR) {
apkgIncludeMedia =
Expand All @@ -228,6 +241,10 @@ class ExportDialogFragment : DialogFragment() {
findViewById<CheckBox>(R.id.export_apkg_schedule).apply {
text = exportingIncludeSchedulingInformation()
}
apkgExportLegacyCheckbox =
findViewById<CheckBox>(R.id.export_legacy_checkbox_apkg).apply {
text = exportingSupportOlderAnkiVersions()
}
}

/**
Expand Down Expand Up @@ -295,18 +312,20 @@ class ExportDialogFragment : DialogFragment() {

private fun handleCollectionExport() {
val includeMedia = collectionIncludeMedia.isChecked
val legacy = collectionExportLegacyCheckbox.isChecked
val exportPath =
File(
getExportRootFile(),
"${CollectionManager.TR.exportingCollection()}-${getTimestamp(TimeManager.time)}.colpkg",
).path
(requireActivity() as AnkiActivity).exportCollectionPackage(exportPath, includeMedia)
(requireActivity() as AnkiActivity).exportCollectionPackage(exportPath, includeMedia, legacy)
}

private fun handleAnkiPackageExport() {
val includeSchedule = apkgIncludeSchedule.isChecked
val includeDeckConfigs = apkgIncludeDeckConfigs.isChecked
val includeMedia = apkgIncludeMedia.isChecked
val legacy = apkgExportLegacyCheckbox.isChecked
val limits = buildExportLimit()
var packagePrefix = getNonCollectionNamePrefix()
// files can't have `/` in their names
Expand All @@ -322,6 +341,7 @@ class ExportDialogFragment : DialogFragment() {
withDeckConfigs = includeDeckConfigs,
withMedia = includeMedia,
limit = limits,
legacy = legacy,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fun importCollectionPackage(
fun Collection.exportCollectionPackage(
outPath: String,
includeMedia: Boolean,
legacy: Boolean = true,
legacy: Boolean,
) {
close(forFullSync = true)
backend.exportCollectionPackage(
Expand Down Expand Up @@ -113,7 +113,7 @@ fun Collection.exportAnkiPackage(
withDeckConfigs: Boolean,
withMedia: Boolean,
limit: ExportLimit,
legacy: Boolean = true,
legacy: Boolean,
) {
val options =
exportAnkiPackageOptions {
Expand Down
17 changes: 16 additions & 1 deletion AnkiDroid/src/main/res/layout/dialog_export_options.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,22 @@
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/decks_selected_barrier">
app:layout_constraintTop_toBottomOf="@id/decks_selected_barrier"
android:orientation="vertical">

<CheckBox
android:id="@+id/export_extras_collection_media"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
tools:text="Include media" />

<CheckBox
android:id="@+id/export_legacy_checkbox_collection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
tools:text="Support older Anki versions (slower/ larger files)" />
</LinearLayout>


Expand Down Expand Up @@ -63,6 +71,13 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true" />

<CheckBox
android:id="@+id/export_legacy_checkbox_apkg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
tools:text="Support older Anki versions (slower/ larger files)" />
</LinearLayout>


Expand Down

0 comments on commit 70936db

Please sign in to comment.