Skip to content

Commit 0c4d443

Browse files
committed
!!!
1 parent 9b0786d commit 0c4d443

File tree

4 files changed

+211
-135
lines changed

4 files changed

+211
-135
lines changed

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

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import androidx.core.view.isVisible
3030
import androidx.fragment.app.DialogFragment
3131
import androidx.fragment.app.Fragment
3232
import androidx.fragment.app.activityViewModels
33+
import androidx.fragment.app.viewModels
3334
import androidx.lifecycle.lifecycleScope
3435
import androidx.viewpager2.adapter.FragmentStateAdapter
3536
import androidx.viewpager2.widget.ViewPager2
@@ -60,24 +61,14 @@ import kotlinx.coroutines.launch
6061
import timber.log.Timber
6162

6263
class ChangeNoteTypeDialog : DialogFragment() {
63-
val noteIds: List<NoteId>
64-
get() =
65-
requireNotNull(requireArguments().getLongArray(ARG_NOTE_IDS)?.toList()) { ARG_NOTE_IDS }
6664
private var initialRotation: Int = 0
6765
private var noteTypeSpinner: Spinner? = null
6866
private var allNoteTypeIds: List<Long>? = null
6967

70-
private val viewModel: ChangeNoteTypeViewModel by activityViewModels {
71-
ChangeNoteTypeViewModel.factory(noteIds)
72-
}
68+
private val viewModel: ChangeNoteTypeViewModel by viewModels { defaultViewModelProviderFactory }
7369

7470
override fun onCreate(savedInstanceState: Bundle?) {
7571
super.onCreate(savedInstanceState)
76-
// Initialize the SavedStateHandle with noteIds
77-
if (!viewModel.stateHandle.contains(ChangeNoteTypeViewModel.ARG_NOTE_IDS)) {
78-
viewModel.stateHandle[ChangeNoteTypeViewModel.ARG_NOTE_IDS] = noteIds
79-
}
80-
Timber.d("ChangeNoteTypeDialog onCreate")
8172
this.initialRotation = getScreenRotation()
8273
}
8374

@@ -226,7 +217,7 @@ class ChangeNoteTypeDialog : DialogFragment() {
226217

227218
lifecycleScope.launch {
228219
createFieldSpinner()
229-
viewModel.selectedNoteType.collect {
220+
viewModel.destinationNoteType.collect {
230221
if (it != null) {
231222
createFieldSpinner()
232223
}
@@ -263,10 +254,13 @@ class ChangeNoteTypeDialog : DialogFragment() {
263254
val fieldsContainer = requireView().findViewById<LinearLayout>(R.id.fields_container)
264255
fieldsContainer.removeAllViews()
265256

266-
val selectedNotetypeJson = viewModel.getNoteTypeOfSelectedNotes()
257+
val selectedNotetypeJson =
258+
// Return cached value if available, otherwise load from database
259+
viewModel.noteTypeOfSelectedNotes
267260
val fieldNames =
268-
viewModel.selectedNoteType.value?.fieldsNames
269-
?: viewModel.getNoteTypeOfSelectedNotes().fieldsNames
261+
viewModel.destinationNoteType.value?.fieldsNames
262+
?: // Return cached value if available, otherwise load from database
263+
viewModel.noteTypeOfSelectedNotes.fieldsNames
270264

271265
for (i in fieldNames.indices) {
272266
val fieldLayout =
@@ -281,12 +275,14 @@ class ChangeNoteTypeDialog : DialogFragment() {
281275

282276
val fieldSpinner =
283277
Spinner(requireContext()).apply {
284-
layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
278+
layoutParams =
279+
LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
285280
}
286281

287282
val fieldText =
288283
MaterialTextView(requireContext()).apply {
289-
layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
284+
layoutParams =
285+
LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
290286
text = fieldNames[i]
291287
textAlignment = View.TEXT_ALIGNMENT_CENTER
292288
}
@@ -295,7 +291,8 @@ class ChangeNoteTypeDialog : DialogFragment() {
295291
fieldLayout.addView(fieldText)
296292
fieldsContainer.addView(fieldLayout)
297293

298-
allNoteTypeIds = setupFieldSpinner(requireContext(), fieldSpinner, selectedNotetypeJson)
294+
allNoteTypeIds =
295+
setupFieldSpinner(requireContext(), fieldSpinner, selectedNotetypeJson)
299296

300297
val selectionIndex = if (i < allNoteTypeIds!!.size) i else allNoteTypeIds!!.size
301298
fieldSpinner.setSelection(selectionIndex)
@@ -312,9 +309,14 @@ class ChangeNoteTypeDialog : DialogFragment() {
312309
) {
313310
// Update the field mapping in the ViewModel
314311
// If the last position is selected (Nothing option), map to null
315-
val newIndex = if (position < selectedNotetypeJson.fieldsNames.size) position else null
312+
val newIndex =
313+
if (position < selectedNotetypeJson.fieldsNames.size) position else null
316314

317-
Timber.d("Updating field mapping: old field %d -> new field %s", oldIndex, newIndex)
315+
Timber.d(
316+
"Updating field mapping: old field %d -> new field %s",
317+
oldIndex,
318+
newIndex,
319+
)
318320
viewModel.updateFieldMapping(oldIndex, newIndex)
319321
}
320322

@@ -345,13 +347,13 @@ class ChangeNoteTypeDialog : DialogFragment() {
345347
// show/hide cloze info layout based on note type
346348
lifecycleScope.launch {
347349
createTemplateSpinner()
348-
viewModel.selectedNoteType.collect {
350+
viewModel.destinationNoteType.collect {
349351
createTemplateSpinner()
350352
}
351353
}
352354

353355
lifecycleScope.launch {
354-
viewModel.selectedConversionType.collect { type ->
356+
viewModel.conversionType.collect { type ->
355357
if (type != null) {
356358
clozeMessage(type, view)
357359
}
@@ -415,13 +417,17 @@ class ChangeNoteTypeDialog : DialogFragment() {
415417
}
416418

417419
private suspend fun createTemplateSpinner() {
418-
val templatesContainer = requireView().findViewById<LinearLayout>(R.id.templates_container)
420+
val templatesContainer =
421+
requireView().findViewById<LinearLayout>(R.id.templates_container)
419422
templatesContainer.removeAllViews()
420423

421-
val selectedNotetypeJson = viewModel.getNoteTypeOfSelectedNotes()
424+
val selectedNotetypeJson =
425+
// Return cached value if available, otherwise load from database
426+
viewModel.noteTypeOfSelectedNotes
422427
val templateNames =
423-
viewModel.selectedNoteType.value?.templatesNames
424-
?: viewModel.getNoteTypeOfSelectedNotes().templatesNames
428+
viewModel.destinationNoteType.value?.templatesNames
429+
?: // Return cached value if available, otherwise load from database
430+
viewModel.noteTypeOfSelectedNotes.templatesNames
425431

426432
for (i in templateNames.indices) {
427433
val templateLayout =
@@ -436,12 +442,14 @@ class ChangeNoteTypeDialog : DialogFragment() {
436442

437443
val templateSpinner =
438444
Spinner(requireContext()).apply {
439-
layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
445+
layoutParams =
446+
LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
440447
}
441448

442449
val templateText =
443450
MaterialTextView(requireContext()).apply {
444-
layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
451+
layoutParams =
452+
LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
445453
text = templateNames[i]
446454
textAlignment = View.TEXT_ALIGNMENT_CENTER
447455
}
@@ -450,7 +458,8 @@ class ChangeNoteTypeDialog : DialogFragment() {
450458
templateLayout.addView(templateText)
451459
templatesContainer.addView(templateLayout)
452460

453-
allNoteTypeIds = setupTemplateSpinner(requireContext(), templateSpinner, selectedNotetypeJson)
461+
allNoteTypeIds =
462+
setupTemplateSpinner(requireContext(), templateSpinner, selectedNotetypeJson)
454463

455464
val selectionIndex = if (i < allNoteTypeIds!!.size) i else allNoteTypeIds!!.size
456465
templateSpinner.setSelection(selectionIndex)
@@ -467,9 +476,14 @@ class ChangeNoteTypeDialog : DialogFragment() {
467476
) {
468477
// Update the card mapping in the ViewModel
469478
// If the last position is selected (Nothing option), map to null
470-
val newIndex = if (position < selectedNotetypeJson.templatesNames.size) position else null
479+
val newIndex =
480+
if (position < selectedNotetypeJson.templatesNames.size) position else null
471481

472-
Timber.d("Updating card mapping: old template %d -> new template %s", oldIndex, newIndex)
482+
Timber.d(
483+
"Updating card mapping: old template %d -> new template %s",
484+
oldIndex,
485+
newIndex,
486+
)
473487
viewModel.updateCardMapping(oldIndex, newIndex)
474488
}
475489

0 commit comments

Comments
 (0)