You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@chrisbanes The reason we moved away from that is because initialState is pretty critical to MvRx. This would prevent a ViewModel from being able to use Fragment args or for MvRx to do proper ViewModel restoration in a new process.
Renaming this to be clearer what the goal is (for me anyway).
I got AutoFactory working here. The only downside I see is that MvRxViewModelFactory only passes along the Activity, whereas I would much rather inject the related fragment instead.
Can we get a version of MvRxViewModelFactory which provides the Fragment?
It would be nice to have MvRxViewModelFactory with provided fragment, as @chrisbanes mentioned. Otherwise, activity should hold all the viewmodel's factories.
@chrisbanes Thanks for wrapping your head around all of this, using activities to store the factories works well!
Do you have any idea how this might work in a single activity project with multi gradle modules where each feature module only has fragments and therefore no access/ knowledge of the activity? -> ((activity as ShowDetailsActivity).episodeDetailsViewModelFactory.create(state) in the view models companion object does not work anymore.
I have been using a fork I made that adds a fragment factory #148.
With the fork, you can use the AssistedInject to inject the fragment and then call the factory in the Fragment from the factory like how @chrisbanes was doing with an ActivityViewModel.
However, the tivi app from @chrisbanes was able to use AssistedInject and in our app, we use a MvRxViewModelFactory and manually get the dependencies from the component to do constructor injection. I'm going to close this issue for now since I'm not sure if there is anything actionable at this point.
Activity
chrisbanes commentedon Aug 30, 2018
I settled on the following for now commit
I inject the ViewModel.Factory into the Activity, then that to create the VM. Works nicely but we do lose the initial state nice-ness.
gpeal commentedon Aug 30, 2018
@chrisbanes The reason we moved away from that is because initialState is pretty critical to MvRx. This would prevent a ViewModel from being able to use Fragment args or for MvRx to do proper ViewModel restoration in a new process.
Thoughts?
chrisbanes commentedon Aug 31, 2018
Yeah, we don't have assisted DI with Dagger so it's hard to get the initialState + fragment args working nicely.
I managed to get it sort of working, but I can't rely on
init {}
and have to manually poke the VM to use the fragment args: here.I might have a play with AutoFactory and see if I can get it to work.
[-]Integrate with existing ViewModelProviders and ViewModelProvider.Factory[/-][+]Work with Dagger[/+]chrisbanes commentedon Aug 31, 2018
Renaming this to be clearer what the goal is (for me anyway).
I got AutoFactory working here. The only downside I see is that
MvRxViewModelFactory
only passes along the Activity, whereas I would much rather inject the related fragment instead.Can we get a version of
MvRxViewModelFactory
which provides the Fragment?mzgreen commentedon Aug 31, 2018
@chrisbanes have you seen this: https://github.com/square/AssistedInject ? Maybe it'll be helpful in this scenario?
chrisbanes commentedon Aug 31, 2018
@mzgreen Ah yes! I've been wanting a reason to try it but totally forgot about it. I'll try on Monday.
chrisbanes commentedon Sep 3, 2018
Moving over to https://github.com/square/AssistedInject in chrisbanes/tivi#214
gpeal commentedon Oct 4, 2018
@chrisbanes I implemented AssistedInject at Tonal and it's working great! Thank you for putting in that work. I'll try and make a wiki page for it.
mtilbrook-dev commentedon Nov 15, 2018
@chrisbanes I was not able to find a way to create a Dagger
ViewModelProvider.Factory
, But, I did find a nice-ish way to inject aViewModelFactory
Provider
map via the activity. Here is all the boilerplatehttps://gist.github.com/marukami/3024dd7ae399a4a33df3041a9af84401
Here is the ViewModel example; I can't get my head around how I would remove the downcast and keep dagger happy.
chrisbanes commentedon Nov 15, 2018
@marukami You need to create an
AssistedInject.Factory
for each ViewModel.Example: https://github.com/chrisbanes/tivi/blob/master/app/src/main/java/app/tivi/showdetails/episodedetails/EpisodeDetailsViewModel.kt#L54
TheLester commentedon Nov 16, 2018
It would be nice to have
MvRxViewModelFactory
with provided fragment, as @chrisbanes mentioned. Otherwise, activity should hold all the viewmodel's factories.pwillmann commentedon Dec 6, 2018
@chrisbanes Thanks for wrapping your head around all of this, using activities to store the factories works well!
Do you have any idea how this might work in a single activity project with multi gradle modules where each feature module only has fragments and therefore no access/ knowledge of the activity? -> (
(activity as ShowDetailsActivity).episodeDetailsViewModelFactory.create(state)
in the view models companion object does not work anymore.mtilbrook-dev commentedon Dec 7, 2018
I have been using a fork I made that adds a fragment factory #148.
With the fork, you can use the
AssistedInject
to inject the fragment and then call the factory in theFragment
from the factory like how @chrisbanes was doing with anActivity
ViewModel
.Any feedback on doing it this way would be awesome.
Allow for Fragment ViewModel factory (airbnb#69) @marukami
pwillmann commentedon Dec 13, 2018
Looks good to me, this would really help using MvRx in dagger projects with multiple gradle modules, where not every module has its own activity.
Can we merge this @gpeal ?
Allow for Fragment ViewModel factory (#69) (#148)
gpeal commentedon Apr 17, 2019
@pwillmann We weren't able to merge that change, unfortunately. You can follow that discussion here.
gpeal commentedon Apr 17, 2019
However, the tivi app from @chrisbanes was able to use AssistedInject and in our app, we use a MvRxViewModelFactory and manually get the dependencies from the component to do constructor injection. I'm going to close this issue for now since I'm not sure if there is anything actionable at this point.