Skip to content

Commit 4c0ef76

Browse files
david-allisonmikehardy
authored andcommitted
refactor(image-occlusion): inline ViewModel init block
1 parent 7ce66da commit 4c0ef76

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/pages/ImageOcclusion.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import com.ichi2.anki.libanki.DeckId
3838
import com.ichi2.anki.model.SelectableDeck
3939
import com.ichi2.anki.pages.viewmodel.ImageOcclusionArgs
4040
import com.ichi2.anki.pages.viewmodel.ImageOcclusionViewModel
41+
import com.ichi2.anki.pages.viewmodel.ImageOcclusionViewModel.Companion.IO_ARGS_KEY
4142
import com.ichi2.anki.requireAnkiActivity
4243
import com.ichi2.anki.selectedDeckIfNotFiltered
4344
import com.ichi2.anki.startDeckSelection
@@ -135,8 +136,6 @@ class ImageOcclusion :
135136
}
136137

137138
companion object {
138-
const val IO_ARGS_KEY = "IMAGE_OCCLUSION_ARGS"
139-
140139
/**
141140
* @param editorWorkingDeckId the current deck id that [com.ichi2.anki.NoteEditorFragment] is using
142141
*/

AnkiDroid/src/main/java/com/ichi2/anki/pages/viewmodel/ImageOcclusionViewModel.kt

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import androidx.lifecycle.ViewModel
2323
import androidx.lifecycle.viewModelScope
2424
import com.ichi2.anki.CollectionManager
2525
import com.ichi2.anki.libanki.DeckId
26-
import com.ichi2.anki.pages.ImageOcclusion
2726
import kotlinx.coroutines.launch
2827
import kotlinx.parcelize.Parcelize
2928
import org.json.JSONObject
@@ -43,39 +42,33 @@ data class ImageOcclusionArgs(
4342
class 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

Comments
 (0)