Skip to content

Commit 8a72662

Browse files
committed
disable save button when there are no pending note type changes
1 parent 9d3a70f commit 8a72662

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,15 @@ class ChangeNoteTypeDialog : AnalyticsDialogFragment(R.layout.dialog_change_note
152152
}
153153
}
154154

155+
private fun updateSaveButton(binding: DialogChangeNoteTypeBinding) {
156+
binding.btnSave.isEnabled = viewModel.hasChanges()
157+
}
158+
155159
private fun setupChangeNoteTypeDialog(binding: DialogChangeNoteTypeBinding) {
156160
Timber.d("setting up dialog")
157161
setupNoteTypeSpinner(binding)
158162
setupViewPagerAndTabs(binding)
163+
updateSaveButton(binding)
159164
}
160165

161166
private fun setupNoteTypeSpinner(binding: DialogChangeNoteTypeBinding) {
@@ -168,6 +173,7 @@ class ChangeNoteTypeDialog : AnalyticsDialogFragment(R.layout.dialog_change_note
168173
onItemSelectedListener =
169174
BasicItemSelectedListener { position, id: NoteTypeId ->
170175
viewModel.setOutputNoteTypeId(id)
176+
updateSaveButton(binding)
171177
}
172178
}
173179
}
@@ -410,6 +416,10 @@ class ChangeNoteTypeDialog : AnalyticsDialogFragment(R.layout.dialog_change_note
410416
SelectedIndex.from(position)
411417
}
412418
viewModel.updateFieldMapping(oldIndex, newMapping)
419+
(requireParentFragment() as ChangeNoteTypeDialog)
420+
.updateSaveButton(
421+
DialogChangeNoteTypeBinding.bind(requireParentFragment().requireView()),
422+
)
413423
}
414424
}
415425

@@ -601,6 +611,10 @@ class ChangeNoteTypeDialog : AnalyticsDialogFragment(R.layout.dialog_change_note
601611
SelectedIndex.from(position)
602612
}
603613
viewModel.updateTemplateMapping(outputTemplateIndex = spinnerIndex, newMapping)
614+
(requireParentFragment() as ChangeNoteTypeDialog)
615+
.updateSaveButton(
616+
DialogChangeNoteTypeBinding.bind(requireParentFragment().requireView()),
617+
)
604618
}
605619
}
606620

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,22 @@ class ChangeNoteTypeViewModel(
300300
}
301301
}
302302

303+
/**
304+
* Returns whether there are any pending changes in Change Note Type.
305+
*
306+
* Save button should only be enabled when:
307+
* - note type is changed
308+
* - field mapping is changed
309+
* - template mapping is changed
310+
*
311+
* If everything is same as the initial/default state,
312+
* then there is nothing to save.
313+
*/
314+
fun hasChanges(): Boolean =
315+
inputNoteType.id != outputNoteType.id ||
316+
fieldChangeMap != rebuildFieldMap(outputNoteType) ||
317+
templateChangeMap != rebuildTemplateMap(outputNoteType)
318+
303319
/**
304320
* Performs the [changeNoteTypeOfNotes] operation
305321
*

0 commit comments

Comments
 (0)