forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeckOptions.kt
More file actions
71 lines (64 loc) · 2.72 KB
/
DeckOptions.kt
File metadata and controls
71 lines (64 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Copyright (c) 2022 Brayan Oliveira <brayandso.dev@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ichi2.anki.pages
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.webkit.WebView
import androidx.fragment.app.FragmentActivity
import anki.collection.OpChanges
import com.ichi2.anki.CollectionManager
import com.ichi2.anki.R
import com.ichi2.libanki.undoableOp
import com.ichi2.libanki.updateDeckConfigsRaw
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class DeckOptions : PageFragment() {
override val title: String
get() = resources.getString(R.string.menu__deck_options)
override val pageName = "deck-options"
override lateinit var webViewClient: PageWebViewClient
override var webChromeClient = PageChromeClient()
override fun onCreate(savedInstanceState: Bundle?) {
val deckId = arguments?.getLong(ARG_DECK_ID)
?: throw Exception("missing deck ID")
webViewClient = DeckOptionsWebClient(deckId)
super.onCreate(savedInstanceState)
}
class DeckOptionsWebClient(val deckId: Long) : PageWebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
// from upstream: https://github.com/ankitects/anki/blob/678c354fed4d98c0a8ef84fb7981ee085bd744a7/qt/aqt/deckoptions.py#L55
view!!.evaluateJavascript("const \$deckOptions = anki.setupDeckOptions($deckId);") {
super.onPageFinished(view, url)
}
}
}
companion object {
const val ARG_DECK_ID = "deckId"
fun getIntent(context: Context, deckId: Long): Intent {
val arguments = Bundle().apply {
putLong(ARG_DECK_ID, deckId)
}
return PagesActivity.getIntent(context, DeckOptions::class, arguments)
}
}
}
suspend fun FragmentActivity.updateDeckConfigsRaw(input: ByteArray): ByteArray {
val output = CollectionManager.withCol { updateDeckConfigsRaw(input) }
undoableOp { OpChanges.parseFrom(output) }
withContext(Dispatchers.Main) { finish() }
return output
}