Skip to content

Commit

Permalink
Anki 23.12.1
Browse files Browse the repository at this point in the history
  • Loading branch information
dae committed Jan 3, 2024
1 parent f034ae3 commit cb5a565
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/BackendImporting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import com.ichi2.libanki.undoableOp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

suspend fun importJsonFileRaw(input: ByteArray): ByteArray {
suspend fun importAnkiPackageUndoable(input: ByteArray): ByteArray {
return withContext(Dispatchers.Main) {
val output = withCol { this.importAnkiPackageRaw(input) }
val changes = OpChangesOnly.parseFrom(output)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import androidx.core.os.bundleOf
import com.ichi2.anki.CollectionManager
import com.ichi2.anki.R
import com.ichi2.anki.hideShowButtonCss
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

class AnkiPackageImporterFragment : PageFragment() {
override val title: String
get() = resources.getString(R.string.menu_import)
override val pageName: String
get() = "import-page"
get() = "import-anki-package"
override lateinit var webViewClient: PageWebViewClient
override var webChromeClient: PageChromeClient = PageChromeClient()
private lateinit var backCallback: OnBackPressedCallback
Expand Down Expand Up @@ -60,9 +62,9 @@ class AnkiPackageImporterFragment : PageFragment() {
private var isDone = false

override fun onPageFinished(view: WebView?, url: String?) {
val params = """{ type: "json_file", path: "$path"}"""
val params = Json.encodeToString(path)
// https://github.com/ankitects/anki/blob/main/ts/import-page/index.ts
view!!.evaluateJavascript("anki.setupImportPage($params);$hideShowButtonCss;") {
view!!.evaluateJavascript("anki.setupImportAnkiPackagePage($params);$hideShowButtonCss;") {
super.onPageFinished(view, url)
}
}
Expand Down
15 changes: 11 additions & 4 deletions AnkiDroid/src/main/java/com/ichi2/anki/pages/AnkiServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ package com.ichi2.anki.pages
import android.app.Activity
import androidx.fragment.app.FragmentActivity
import anki.collection.OpChanges
import com.ichi2.anki.AnkiDroidApp
import com.ichi2.anki.CollectionManager
import com.ichi2.anki.CollectionManager.withCol
import com.ichi2.anki.NoteEditor
import com.ichi2.anki.importAnkiPackageUndoable
import com.ichi2.anki.importCsvRaw
import com.ichi2.anki.importJsonFileRaw
import com.ichi2.anki.launchCatchingTask
import com.ichi2.anki.searchInBrowser
import com.ichi2.libanki.*
Expand All @@ -38,6 +39,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import timber.log.Timber
import java.io.ByteArrayInputStream
import java.io.IOException

const val PORT = 0
// const val PORT = 40001
Expand All @@ -62,8 +64,12 @@ open class AnkiServer(
val mime = getMimeFromUri(uri)

if (session.method == Method.GET) {
val resourcePath = "web$uri"
val stream = this.javaClass.classLoader!!.getResourceAsStream(resourcePath)
val resourcePath = "backend/web$uri"
val stream = try {
AnkiDroidApp.instance.assets.open(resourcePath)
} catch (e: IOException) {
null
}
Timber.d("GET: Requested %s (%s), found? %b", uri, resourcePath, stream != null)
return newChunkedResponse(Response.Status.OK, mime, stream)
}
Expand All @@ -88,8 +94,9 @@ open class AnkiServer(
"getDeckNames" -> withCol { getDeckNamesRaw(bytes) }
"getCsvMetadata" -> withCol { getCsvMetadataRaw(bytes) }
"importCsv" -> activity.importCsvRaw(bytes)
"importJsonFile" -> importJsonFileRaw(bytes)
"importAnkiPackage" -> importAnkiPackageUndoable(bytes)
"importDone" -> bytes
"getImportAnkiPackagePresets" -> withCol { getImportAnkiPackagePresetsRaw(bytes) }
"searchInBrowser" -> activity.searchInBrowser(bytes)
"completeTag" -> withCol { completeTagRaw(bytes) }
"getFieldNames" -> withCol { getFieldNamesRaw(bytes) }
Expand Down
13 changes: 12 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/libanki/BackendImportExport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.ichi2.libanki

import anki.import_export.ExportLimit
import anki.import_export.exportAnkiPackageOptions
import anki.search.SearchNode
import net.ankiweb.rsdroid.Backend

Expand Down Expand Up @@ -95,6 +96,10 @@ fun Collection.importAnkiPackageRaw(input: ByteArray): ByteArray {
return backend.importAnkiPackageRaw(input)
}

fun Collection.getImportAnkiPackagePresetsRaw(input: ByteArray): ByteArray {
return backend.getImportAnkiPackagePresetsRaw(input)
}

/**
* Export the specified deck to an .apkg file.
* * If legacy is false, an apkg will be created that can only
Expand All @@ -107,7 +112,13 @@ fun Collection.exportAnkiPackage(
limit: ExportLimit,
legacy: Boolean = true
) {
backend.exportAnkiPackage(outPath, withScheduling, withMedia, legacy, limit)
val options = exportAnkiPackageOptions {
this.withScheduling = withScheduling
this.withMedia = withMedia
this.legacy = legacy
this.withDeckConfigs = withScheduling
}
backend.exportAnkiPackage(outPath, options, limit)
}

fun Collection.getCsvMetadataRaw(input: ByteArray): ByteArray {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
ext.kotlin_version = '1.9.21'
ext.lint_version = '31.1.1'
ext.acra_version = '5.11.3'
ext.ankidroid_backend_version = '0.1.32-anki23.10.1'
ext.ankidroid_backend_version = '0.1.34-anki23.12.1'
ext.hamcrest_version = '2.2'
ext.junit_version = '5.10.1'
ext.coroutines_version = '1.7.3'
Expand Down

0 comments on commit cb5a565

Please sign in to comment.