toothpick icon indicating copy to clipboard operation
toothpick copied to clipboard

ViewModel constructor injection

Open volo-droid opened this issue 9 months ago • 0 comments

This library seems to be long time abandoned, but if one day you like I find yourself on a project that uses Toothpick and question how to perform a constructor injection of ViewModel without too much hassle have a look at the following Gist:

https://gist.github.com/volo-droid/54d4443c28ec92ecb0a0741d16d4c47c

It allows to inject ViewModel in a simpler way.

class MainFragment : Fragment() {
    
    private val viewModel by viewModelOf(::MyViewModel)
    
    private val sharedViewModel by activityViewModelOf(::MySharedViewModel)
    
    // …
}

volo-droid avatar Mar 24 '25 00:03 volo-droid