darkmoon_uk12/06/2020, 10:58 AM
with a lambda that constructs ViewModels with parameters, there is no way to assign this as the default factory. This entails that you either have to pass the factory around your composable functions to use it e.g.
or actually override the
method. This is convenient enough in an
but becomes more troublesome if you try to do it for a
. In other words the API design is coercing us toward using Dependency Injection frameworks in our ViewModels, rather than just plain construction. Whether you favour DI frameworks or not; construction parameters are objectively the primary mechanism in the Kotlin language for providing dependencies to an object: this at least implies that construction parameters should remain the first and best supported mechanism, followed by others. Am I missing something here or is there a case for Google to make these API's more flexible?
Ian Lake12/06/2020, 4:09 PM
darkmoon_uk12/06/2020, 9:14 PM
based on a lambda, that performs custom construction. What's unclear is how to then use that custom factory in the current framework - besides just invoking it directly. Ideally I would have this custom factory become part of the default provider, in scope of the current Activity or NavHost. But there's no way to reassign the default
, and looking into the implementation of the default
, it doesn't appear to offer any way to have its
reassigned/deferred e.g. one possible way might have the default provider use an ambient factory, using the ambience mechanism?
which appears to wrap only either of the default
instances, but looks like it could be extended to allow for wrapping a custom
Ian Lake12/06/2020, 11:47 PM
if you want to extend it to allow your own custom
call. Whether you have a single
lambda you pass into your composable and construct a
() -> YourViewModel
out of it via a helper method, whether you pass in an actual
as a parameter to your composable, whether your composable uses an ambient as a bare bones service locator to access your custom factory, or whether you use a full blown DI system to provide either the whole factory or that wraps the construction/creation of the VIewModel up entirely for you (ala what Koin does) is all up to you
darkmoon_uk12/07/2020, 3:33 AM
with the factory does still go via the ambient store for caching purposes 👍 Thank you for the guidance.