refactor(notetypeFieldEditor): NotetypeFieldEditor ViewModel#20480
refactor(notetypeFieldEditor): NotetypeFieldEditor ViewModel#20480S-H-Y-A wants to merge 6 commits intoankidroid:mainfrom
Conversation
david-allison
left a comment
There was a problem hiding this comment.
Please remove the merge commit from the history and fix the lint errors
f902c97 to
df487ef
Compare
There was a problem hiding this comment.
Is there any way you can rebase this to make it easier to review?
I'm looking through 8d21a89 and most of the changes feel unnecessary/incorrect
for example:
- AnkiActivity(R.layout.note_type_field_editor) {
+ com.ichi2.anki.AnkiActivity(_root_ide_package_.com.ichi2.anki.R.layout.note_type_field_editor) {4ea4517 to
c68f093
Compare
|
Thank you for quick look. |
| noteFields = notetype.fields | ||
| fieldsLabels = notetype.fieldsNames | ||
| binding.fields.adapter = NoteFieldAdapter(this, fieldNamesWithKind()) | ||
| lifecycleScope.launch { |
There was a problem hiding this comment.
launchCollectionInLifecycleScope
| @@ -0,0 +1,148 @@ | |||
| /* | |||
| * Copyright (c) 2024 Neel Doshi <neeldoshi147@gmail.com> | |||
| viewModel.state.value.noteFields[viewModel.state.value.currentPos] | ||
| .name |
There was a problem hiding this comment.
This is verbose.
The use of currentPos is questionable, but why not define this as viewModel.currentField.name
| val noteFields: Fields = notetype.fields, | ||
| val fieldsLabels: List<String> = notetype.fieldsNames, | ||
| val isLoading: Boolean = false, | ||
| ) |
There was a problem hiding this comment.
Most of these properties are derived properties which are unchanged, and therefore shouldn't be in the constructor.
| @@ -0,0 +1,29 @@ | |||
| /* | |||
| * Copyright (c) 2024 Neel Doshi <neeldoshi147@gmail.com> | |||
| ) : ViewModel() { | ||
| private val _state by lazy { | ||
| val noteTypeID = savedStateHandle.get<Long>(EXTRA_NOTETYPE_ID) ?: 0 | ||
| val notetype = getColUnsafe().notetypes.get(noteTypeID)!! |
There was a problem hiding this comment.
Don't use getColUnsafe() in ViewModels
| class NoteTypeFieldEditorViewModel( | ||
| private val savedStateHandle: SavedStateHandle, | ||
| ) : ViewModel() { | ||
| private val _state by lazy { |
There was a problem hiding this comment.
I find it's OK to expose MutableStateFlow publicly
Now we're on Kotlin 2.3, we can specify a different type for the public view of the type
There was a problem hiding this comment.
According to the lint, "-Xexplicit-backing-fields" need to use Explicit Backing Fields.
The lint failed due to it.
(I oppressed it on my local machine when coding.)
| import kotlinx.coroutines.launch | ||
| import timber.log.Timber | ||
|
|
||
| class NoteTypeFieldEditorViewModel( |
Brought in viewModel to NoteTypeFieldEditor
Removed unnecessary values from the state Stopped offering unnecessary data to NoteTypeFieldEditor.kt Removed an unused method: undo()
1ed1160 to
a2fefbf
Compare
|
I have been working on #20453 this week. |
|
That's not a problem. We'll get it in. Thanks so much for your time! Do get in touch before a large change next time, and we can speed things along |
Purpose / Description
This commit brings ViewModel to NotetypeFieldEditor, including no UI change.
This is the first one of the split PRs of #20453.
Approach
How Has This Been Tested?
Physical device
Pixel 7a
Android 16
Learning (optional, can help others)
Checklist
Please, go through these checks before submitting the PR.