Refactor ViewModelFactory to use DSL#1064
Refactor ViewModelFactory to use DSL#1064marcellogalhardo wants to merge 1 commit intoandroid:viewsfrom
Conversation
Change TodoViewModelFactory to use the viewModelFactory DSL. This replaces manual class checks and unchecked casts.
There was a problem hiding this comment.
Code Review
This pull request refactors the TodoViewModelFactory to use the viewModelFactory DSL and initializer blocks, simplifying the ViewModel creation logic. A review comment suggests refining the error handling in the CreationExtras.application extension property to provide more specific error messages when the application key is missing versus when there is a type mismatch.
| private val CreationExtras.application: TodoApplication | ||
| get() = this[APPLICATION_KEY] as? TodoApplication | ||
| ?: error("'CreationExtras' must contain 'APPLICATION_KEY' to access 'TodoApplication'") |
There was a problem hiding this comment.
The error message in the application extension property is slightly misleading. If APPLICATION_KEY is present but the value is not a TodoApplication (e.g., during testing with a mock application), the current message still claims the key must be present. Consider refining the error handling to distinguish between a missing key and a type mismatch for better debugging.
| private val CreationExtras.application: TodoApplication | |
| get() = this[APPLICATION_KEY] as? TodoApplication | |
| ?: error("'CreationExtras' must contain 'APPLICATION_KEY' to access 'TodoApplication'") | |
| private val CreationExtras.application: TodoApplication | |
| get() = (this[APPLICATION_KEY] ?: error("CreationExtras must contain APPLICATION_KEY")) | |
| .let { it as? TodoApplication ?: error("Application associated with APPLICATION_KEY must be of type TodoApplication") } |
Change TodoViewModelFactory to use the viewModelFactory DSL. This replaces manual class checks and unchecked casts.