Skip to content

Commit 7402d62

Browse files
committed
!!!
1 parent 0c4d443 commit 7402d62

File tree

8 files changed

+337
-405
lines changed

8 files changed

+337
-405
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/dialogs/ChangeNoteTypeDialog.kt

Lines changed: 301 additions & 242 deletions
Large diffs are not rendered by default.

AnkiDroid/src/main/java/com/ichi2/anki/dialogs/ChangeNoteTypeViewModel.kt

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.ichi2.anki.dialogs
1818

1919
import androidx.lifecycle.SavedStateHandle
2020
import androidx.lifecycle.ViewModel
21+
import androidx.lifecycle.viewModelScope
2122
import anki.collection.OpChanges
2223
import com.ichi2.anki.CollectionManager.withCol
2324
import com.ichi2.anki.utils.InitStatus
@@ -26,10 +27,12 @@ import com.ichi2.anki.utils.ext.require
2627
import com.ichi2.annotations.NeedsTest
2728
import com.ichi2.libanki.CardTemplates
2829
import com.ichi2.libanki.NoteId
30+
import com.ichi2.libanki.NoteTypeId
2931
import com.ichi2.libanki.NotetypeJson
3032
import com.ichi2.libanki.Notetypes
3133
import com.ichi2.libanki.undoableOp
3234
import com.ichi2.utils.HashUtil
35+
import com.ichi2.utils.NamedJSONComparator
3336
import kotlinx.coroutines.CoroutineScope
3437
import kotlinx.coroutines.async
3538
import 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
) {

AnkiDroid/src/main/java/com/ichi2/anki/ui/ChangeNoteTypeSpinnerUtils.kt

Lines changed: 0 additions & 45 deletions
This file was deleted.

AnkiDroid/src/main/java/com/ichi2/anki/ui/FieldSpinnerUtils.kt

Lines changed: 0 additions & 50 deletions
This file was deleted.

AnkiDroid/src/main/java/com/ichi2/anki/ui/TemplateSpinnerUtils.kt

Lines changed: 0 additions & 50 deletions
This file was deleted.

AnkiDroid/src/main/java/com/ichi2/anki/utils/ViewModelUtils.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ interface ViewModelDelayedInitializer {
3232
/** Flow to track initialization status */
3333
val initStatus: MutableStateFlow<InitStatus>
3434

35-
val viewModelScope: CoroutineScope
35+
val scope: CoroutineScope
3636

3737
// TODO: Docs
3838
fun delayedInit(block: suspend () -> Unit) {
39-
viewModelScope.launch {
39+
scope.launch {
4040
initStatus.value = InitStatus.IN_PROGRESS
4141
try {
4242
block()

AnkiDroid/src/main/res/layout/change_note_type_dialog.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<ScrollView
3+
android:id="@+id/change_note_type_layout"
34
xmlns:android="http://schemas.android.com/apk/res/android"
45
xmlns:app="http://schemas.android.com/apk/res-auto"
56
android:layout_width="match_parent"
67
android:layout_height="wrap_content"
8+
android:visibility="gone"
79
android:fadeScrollbars="false">
810
<LinearLayout
911
android:layout_width="match_parent"

AnkiDroid/src/main/res/values/03-dialogs.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ also changes the interval of the card"
270270
<string name="tts_error_dialog_supported_voices_button_text">Voice options</string>
271271

272272
<!--Change Note Type Dialog Fragment-->
273-
<string name="regular_to_cloze">Card numbers remain unchanged</string>
274-
<string name="cloze_to_regular">Extra cloze deletions will be removed</string>
273+
<string name="card_numbers_unchanged">Card numbers remain unchanged</string>
274+
<string name="extra_cloze_deletions_removed">Extra cloze deletions will be removed</string>
275275
<string name="different_note_types_selected">Please select notes from only one notetype.</string>
276276
<string name="change_note_type_complete">Note type changed for: %1$d notes</string>
277277
</resources>

0 commit comments

Comments
 (0)