Skip to content

Conversation

@adriangl
Copy link
Contributor

@adriangl adriangl commented Feb 3, 2020

PR's key points

The PR adds support for injecting ViewModel factories and for retrieving ViewModels via previously declared factories. This will ease parameterized ViewModels delivery and remove some boilerplate that we use when configuring the ViewModels in production code.

The added code relies in a new TypedViewModel that enforces passing a single params value to ensure type safety in the injection functions.
I couldn't think of a better idea to do this, so if you have any other ideas (the ideal thing would be to ensure type safety while also not forcing to extend another class) feel free to propose them 😉

The final idea would be:

  • Create your ViewModel with parameters.
  • Create a ViewModelProvider.Factory that creates said ViewModel. Since the code wouldn't be especially complex, we can use something like https://github.com/radutopor/ViewModelFactory to auto-generate said factories and save some boilerplate.
  • Inject the ViewModelProvider.Factory with bindViewModelFactory
  • Retrieve a ViewModel instance with parameters using by viewModel(parameters)

@adriangl adriangl added the enhancement New feature or request label Feb 3, 2020
@adriangl adriangl self-assigned this Feb 3, 2020
@adriangl adriangl merged commit c9e90bb into master Feb 7, 2020
@adriangl adriangl deleted the feature/view-model-factories branch February 7, 2020 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants