-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
refactor(CardTemplateEditor): load notetype into ViewModel #20109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| // During ViewPager tab transitions, TextWatcher may fire with a stale ordinal | ||
| // (e.g. when switching from a note type with 3 templates to one with 2). | ||
| // The legacy code path handles the actual update, so we can safely skip here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While testing, I encountered a crash when switching between note types with different template counts. The TextWatcher fired during ViewPager tab transitions with a stale cardIndex, causing an IndexOutOfBoundsException i.e. Fixed by adding a bounds check that safely skips the ViewMode update when the ordinal is out of range.
Process: com.ichi2.anki.debug, PID: 21692
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:603)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:932)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:593)
... 1 more
Caused by: org.json.JSONException: Index 2 out of range [0..2)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should fix this in the view layer, it should be a bug in the ViewModel
| // During ViewPager tab transitions, TextWatcher may fire with a stale ordinal | ||
| // (e.g. when switching from a note type with 3 templates to one with 2). | ||
| // The legacy code path handles the actual update, so we can safely skip here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should fix this in the view layer, it should be a bug in the ViewModel
AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditorViewModel.kt
Outdated
Show resolved
Hide resolved
Introduce CardTemplateEditorViewModel and CardTemplateEditorState as the foundation for migrating CardTemplateEditor to use the Rust backend's atomic updateNotetype() API. Part 1 of migration, infrastructure only, no behavioral changes yet.
Connect CardTemplateEditorViewModel to CardTemplateEditor Activity: - Add viewModel declaration using viewModels delegate - Add state collection with lifecycleScope + repeatOnLifecycle - Wire tab selection to viewModel.setCurrentTemplateOrd() - Handle sealed class states (Loading, Loaded, Error, Finished) Part 2 of migration, infrastructure wiring, no behavioral changes.
…emplate updates - Add tempNotetype property to Loaded state (sealed class) - Add loadNotetype() to load and deep clone notetype into ViewModel - Add updateTemplateContent() using EditorViewType enum - Add addTemplate() and removeTemplate() placeholder methods - Wire Fragment's TextWatcher to call ViewModel in parallel with legacy path - Share ViewModel between Activity and Fragment via activityViewModels() Part 3 of migration, infrastructure wiring, no behavioral changes.
1c4fc50 to
4e798f0
Compare
| showSnackbar( | ||
| state.exception.source.localizedMessage ?: getString(R.string.something_wrong), | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like it needs a better abstraction.
- If it's a loading error, a snackbar is insufficient, as it'll be closed when the screen is closed
- If it's a loading error, this doesn't finish the screen, and it probably should
If it's a transient error (save failed), it may be displayed alongside the valid state
We probably want the user to copy/paste the error, so an error dialog is more friendly
| val tempNotetype = loadedState.tempNotetype | ||
| if (tempNotetype.templateCount < 2) { | ||
| Timber.w("Cannot delete last template") | ||
| updateLoadedState { it.copy(message = CardTemplateEditorState.UserMessage.CantDeleteLastTemplate) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be great to test some of this logic
Purpose / Description
Third PR in the CardTemplateEditor migration series.
tempNotetypeproperty toCardTemplateEditorStateloadNotetype()in ViewModelupdateTemplateContent()addTemplate()andremoveTemplate()methodsViewModelwith Activity viaactivityViewModels()TextWatchercalls ViewModel in parallel with legacy code path during this transitional phaseLinks without closing (Part of)
Based on
How Has This Been Tested?
No regressions obtained, and all
TemplateEditorViewModelTestpasses.Checklist
Please, go through these checks before submitting the PR.