@@ -23,7 +23,6 @@ import androidx.lifecycle.ViewModel
2323import androidx.lifecycle.viewModelScope
2424import com.ichi2.anki.CollectionManager
2525import com.ichi2.anki.libanki.DeckId
26- import com.ichi2.anki.pages.ImageOcclusion
2726import kotlinx.coroutines.launch
2827import kotlinx.parcelize.Parcelize
2928import org.json.JSONObject
@@ -43,39 +42,33 @@ data class ImageOcclusionArgs(
4342class ImageOcclusionViewModel (
4443 savedStateHandle : SavedStateHandle ,
4544) : ViewModel() {
46- var selectedDeckId: Long
45+ private val args: ImageOcclusionArgs =
46+ checkNotNull(savedStateHandle[IO_ARGS_KEY ]) { " $IO_ARGS_KEY required" }
47+
48+ var selectedDeckId: Long = args.editorDeckId
4749
4850 /* *
4951 * The ID of the deck that was originally selected when the editor was opened.
5052 * This is used to restore the deck after saving a note to prevent unexpected deck changes.
5153 */
52- val oldDeckId: Long
54+ val oldDeckId: Long = args.editorDeckId
5355
5456 /* *
5557 * A [JSONObject] containing options for loading the [image occlusion page][ImageOcclusion].
5658 * This includes the type of operation ("add" or "edit"), and relevant IDs and paths.
5759 *
5860 * Defined in https://github.com/ankitects/anki/blob/main/ts/routes/image-occlusion/lib.ts
5961 */
60- val webViewOptions: JSONObject
61-
62- init {
63- val args: ImageOcclusionArgs = checkNotNull(savedStateHandle[ImageOcclusion .IO_ARGS_KEY ])
64-
65- selectedDeckId = args.editorDeckId
66- oldDeckId = args.editorDeckId
67-
68- webViewOptions =
69- JSONObject ().apply {
70- put(" kind" , args.kind)
71- if (args.kind == " add" ) {
72- put(" imagePath" , args.imagePath)
73- put(" notetypeId" , args.id)
74- } else {
75- put(" noteId" , args.id)
76- }
62+ val webViewOptions: JSONObject =
63+ JSONObject ().apply {
64+ put(" kind" , args.kind)
65+ if (args.kind == " add" ) {
66+ put(" imagePath" , args.imagePath)
67+ put(" notetypeId" , args.id)
68+ } else {
69+ put(" noteId" , args.id)
7770 }
78- }
71+ }
7972
8073 /* *
8174 * Handles the selection of a new deck.
@@ -102,4 +95,8 @@ class ImageOcclusionViewModel(
10295 CollectionManager .withCol { backend.setCurrentDeck(oldDeckId) }
10396 }
10497 }
98+
99+ companion object {
100+ const val IO_ARGS_KEY = " IMAGE_OCCLUSION_ARGS"
101+ }
105102}
0 commit comments