Skip to content

Add parameterized ViewModel injection via factories #9

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

Merged
merged 1 commit into from
Feb 7, 2020

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.

4 participants