Skip to content
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

Honor project source version for generated annotation type #9

Merged
merged 1 commit into from Dec 17, 2020
Merged

Honor project source version for generated annotation type #9

merged 1 commit into from Dec 17, 2020

Conversation

gm-vm
Copy link
Contributor

@gm-vm gm-vm commented Dec 17, 2020

No description provided.

In some cases, both types would be available despite the user desiring to target 8.

Code adapted from AssisteInject.
@gm-vm
Copy link
Contributor Author

gm-vm commented Dec 17, 2020

I've been using a local build for my dev builds, but with Android Studio 4.2 getting closer to a stable release and with Hilt not being ready for my usecase [1], I thought I might just push this. I don't know if you are still interested in keeping this alive.

I was actually waiting for the next release of AssistedInject, since it comes with a breaking change (that'll prevent this from ever happening again. Tip: let gradle generate the build cache with Android Studio's JDK. The system JDK may cause parameters to lose their name).

[1] I want a clean and easy way to initialize the SavedStateHandle: google/dagger#1938 (comment)
I could take advantage of the fact the the SavedStateHandle is initialized with the Fragment's arguments and use some helpers to stuff the arguments in there, but I decided to wait and see how that usecase will be handled.
Then there's also this issue, which would break quite a few things: google/dagger#2152.

I also like the fact that I can use this library with dagger-reflect, not sure about Hilt.

@hansenji hansenji merged commit 3ec124b into hansenji:master Dec 17, 2020
@gm-vm
Copy link
Contributor Author

gm-vm commented Jan 2, 2021

Hey, just a heads-up: AssistedInject is now part of dagger and it's already available in HEAD-SNAPSHOT.

This library could benefit from it, but I've played a bit with Hilt and I think I can now get what I want with no third party libraries:

fun interface AssistedViewModelFactory<T : ViewModel> {
    fun create(savedStateHandle: SavedStateHandle): T
}

class MyViewModel @AssistedInject constructor(
        private val someDependency: Any,
        @Assisted private val savedStateHandle: SavedStateHandle,
) : ViewModel() {

    @AssistedFactory
    interface Factory : AssistedViewModelFactory<MyViewModel>
}

@AndroidEntryPoint
class MyFragment : Fragment() {

    @Inject lateinit var viewModelFactory: MyViewModel.Factory

    // `appViewModels` would be an extension function that defines
    // a local `AbstractSavedStateViewModelFactory` which uses the above
    // Factory to instantiate the ViewModel.
    private val viewModel by appViewModels { viewModelFactory.create(it) }
}

The real game changer here is @AndroidEntryPoint, because I probably could have done this earlier. Currently I inject SavedStateViewModelFactory.Factory once in the Application, just to keep things as simple as possible, but Hilt allows me to do proper DI with almost no effort.

If you are wondering why I would not use Hilt's @ViewModelInject (for the moment at least): it's not as flexible IMO, it still has some issues with SavedStateHandle and its API is likely going to change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants