Skip to content

Refactor ViewModelFactory to use DSL#1064

Open
marcellogalhardo wants to merge 1 commit intoandroid:viewsfrom
marcellogalhardo:views
Open

Refactor ViewModelFactory to use DSL#1064
marcellogalhardo wants to merge 1 commit intoandroid:viewsfrom
marcellogalhardo:views

Conversation

@marcellogalhardo
Copy link
Copy Markdown

Change TodoViewModelFactory to use the viewModelFactory DSL. This replaces manual class checks and unchecked casts.

Change TodoViewModelFactory to use the viewModelFactory DSL.
This replaces manual class checks and unchecked casts.
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +54 to +56
private val CreationExtras.application: TodoApplication
get() = this[APPLICATION_KEY] as? TodoApplication
?: error("'CreationExtras' must contain 'APPLICATION_KEY' to access 'TodoApplication'")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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") }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant