@@ -18,6 +18,7 @@ package com.ichi2.anki.dialogs
1818
1919import androidx.lifecycle.SavedStateHandle
2020import androidx.lifecycle.ViewModel
21+ import androidx.lifecycle.viewModelScope
2122import anki.collection.OpChanges
2223import com.ichi2.anki.CollectionManager.withCol
2324import com.ichi2.anki.utils.InitStatus
@@ -26,10 +27,12 @@ import com.ichi2.anki.utils.ext.require
2627import com.ichi2.annotations.NeedsTest
2728import com.ichi2.libanki.CardTemplates
2829import com.ichi2.libanki.NoteId
30+ import com.ichi2.libanki.NoteTypeId
2931import com.ichi2.libanki.NotetypeJson
3032import com.ichi2.libanki.Notetypes
3133import com.ichi2.libanki.undoableOp
3234import com.ichi2.utils.HashUtil
35+ import com.ichi2.utils.NamedJSONComparator
3336import kotlinx.coroutines.CoroutineScope
3437import kotlinx.coroutines.async
3538import kotlinx.coroutines.flow.MutableSharedFlow
@@ -67,12 +70,15 @@ class ChangeNoteTypeViewModel(
6770 ViewModelDelayedInitializer {
6871 /* * IDs of notes to be modified */
6972 val noteIds: List <NoteId >
70- get() = stateHandle.require(ARG_NOTE_IDS )
73+ get() = stateHandle.require< LongArray > (ARG_NOTE_IDS ).toList( )
7174
7275 // unchanged after init { }
7376 /* * The note type of the selected notes */
7477 // This should be lateinit
75- private var sourceNoteType: NotetypeJson = NotetypeJson (" {}" )
78+ var sourceNoteType: NotetypeJson = NotetypeJson (" {}" )
79+ private set
80+
81+ var availableNoteTypes: List <NotetypeJson > = listOf ()
7682
7783 // UI variables
7884 /* * @see Tab */
@@ -82,17 +88,17 @@ class ChangeNoteTypeViewModel(
8288 private var noteTypeChangeFieldMap: MutableMap <FieldIndex , FieldIndex ?>? = mutableMapOf ()
8389 private var noteTypeChangeCardMap: MutableMap <CardTemplateIndex , CardTemplateIndex ?>? = mutableMapOf ()
8490
85- override val viewModelScope : CoroutineScope
86- get() = this @ChangeNoteTypeViewModel. viewModelScope
91+ override val scope : CoroutineScope
92+ get() = viewModelScope
8793
8894 // Flows
8995 /* * Flow to track initialization status */
9096 override val initStatus: MutableStateFlow <InitStatus > = MutableStateFlow (InitStatus .PENDING )
9197
9298 /* * The note type selected by the user for conversion */
93- var destinationNoteType = MutableStateFlow <NotetypeJson >(sourceNoteType)
99+ var selectedDestinationNoteType = MutableStateFlow <NotetypeJson >(sourceNoteType)
94100 val conversionType =
95- destinationNoteType
101+ selectedDestinationNoteType
96102 .map { newNoteType ->
97103 ConversionType .fromNoteTypes(current = sourceNoteType, new = newNoteType)
98104 }
@@ -107,17 +113,18 @@ class ChangeNoteTypeViewModel(
107113 init {
108114 delayedInit {
109115 sourceNoteType = withCol { getNote(noteIds.first()) }.notetype
110-
111- destinationNoteType.emit(sourceNoteType)
116+ availableNoteTypes = withCol { notetypes.all().sortedWith(NamedJSONComparator .INSTANCE ) }
117+ selectedDestinationNoteType.value = sourceNoteType
118+ Timber .d(" set selectedDestinationNoteType to ${selectedDestinationNoteType.value.name} " )
112119 initializeMapsInternal(sourceNoteType, sourceNoteType)
113120 }
114121 }
115122
116123 @NeedsTest(" test changing note type" )
117124 fun changeNoteTypeAsync () =
118- viewModelScope .async {
125+ scope .async {
119126 val oldNotetype = sourceNoteType
120- val newNotetype = destinationNoteType .value
127+ val newNotetype = selectedDestinationNoteType .value
121128
122129 Timber .d(" Changing note type from %s to %s" , oldNotetype.name, newNotetype.name)
123130 Timber .d(" Field map: %s" , noteTypeChangeFieldMap)
@@ -172,7 +179,7 @@ class ChangeNoteTypeViewModel(
172179
173180 suspend fun initializeMaps () {
174181 val oldNotetype = sourceNoteType
175- val newNotetype = destinationNoteType .value
182+ val newNotetype = selectedDestinationNoteType .value
176183
177184 val oldFieldMap = Notetypes .fieldMap(oldNotetype)
178185 val newFieldMap = Notetypes .fieldMap(newNotetype)
@@ -308,7 +315,7 @@ class ChangeNoteTypeViewModel(
308315 Timber .w(" Attempted to update field mapping before initialization" )
309316 }
310317 // Increment the counter to notify observers that field mapping has changed
311- viewModelScope .launch {
318+ scope .launch {
312319 fieldMappingChanges.emit(Unit )
313320 }
314321 }
@@ -321,7 +328,7 @@ class ChangeNoteTypeViewModel(
321328 Timber .w(" Attempted to update card mapping before initialization" )
322329 }
323330 // Increment the counter to notify observers that card mapping has changed
324- viewModelScope .launch {
331+ scope .launch {
325332 cardMappingChanges.emit(Unit )
326333 }
327334 }
@@ -382,12 +389,21 @@ class ChangeNoteTypeViewModel(
382389 */
383390 fun updateSelectedNoteType (noteType : NotetypeJson ) {
384391 viewModelScope.launch {
385- destinationNoteType.emit(noteType)
392+ Timber .i(" updating selected note type to ${noteType.id} " )
393+ selectedDestinationNoteType.emit(noteType)
386394 // Initialize maps immediately after note type selection
387395 initializeMaps()
388396 }
389397 }
390398
399+ /* *
400+ * @throws IllegalStateException if [id] is not found
401+ */
402+ fun selectNoteTypeAtPosition (id : NoteTypeId ) {
403+ val notetype = availableNoteTypes.find { it.id == id } ? : error(" $id not found" )
404+ updateSelectedNoteType(notetype)
405+ }
406+
391407 enum class Tab (
392408 val position : Int ,
393409 ) {
0 commit comments