-
Notifications
You must be signed in to change notification settings - Fork 499
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
Hilt and ViewModel initialization with Navigation Component arguments #692
Comments
I'm looking into https://airbnb.io/mavericks/#/fragment-arguments?id=using-fragment-args-in-the-initial-value-for-mavericksstate The following might work: LoginFragmentDirections.actionLoginFragmentToHome(userFullName = it.userFullName) data class HomeState(
val userFullName: String,
val paymentList: Async<List<Payment>> = Uninitialized,
val coarseLocationPermissionGranted: Boolean = false,
val deviceLastLocationCoordinates: Pair<Double, Double> = Pair(0.0, 0.0),
val count: Int = 0
) : MavericksState {
constructor(homeFragmentArgs: HomeFragmentArgs) : this(userFullName = homeFragmentArgs.userFullName.orEmpty())
|
Nah, that alternate |
Well things ARE working when I follow the steps above However, this process seems opposite to using the arguments declared in the Navigation Component nav graph XML file. I still have to create custom Parcelizable argument data classes 😕 I'm unable to use generated findNavController().navigate(LoginFragmentDirections.actionLoginFragmentToHome(userFullName = it.userFullName)) I know that custom However, this doesn't really help me from avoiding having to create these custom args classes in the first place 😕 |
Doesn't really address this ticket's topic/question, but another option I just thought of was to save the full name to repo-level persistence in |
Closing the loop here. This is what I've ultimately ended up with and I'm posting it here mainly so that others might benefit from it. I created a import android.os.Parcelable
import kotlinx.parcelize.Parcelize
@Parcelize
class ParcelableClassWithArgs(
var stringValue: String? = "",
var booleanValue: Boolean? = false,
): Parcelable
fun Parcelable.setAsMavericksArgs(fragment: Fragment): Bundle {
this.asMavericksArgs().let {
fragment.arguments = it
return it
}
}
fun Fragment.navigate(actionInt: Int, optionalArgs: Bundle? = null) =
findNavController().navigate(actionInt, optionalArgs) Navigating in the navigate(
NavGraphDirections.actionToNextFragment().actionId,
ParcelableClassWithArgs(
...various values
).setAsMavericksArgs(this)
) Setup in the data class ReceivingViewModelState(
val args: ParcelableClassWithArgs? = null,
) : MavericksState {
constructor(parcelableClassWithArgs: ParcelableClassWithArgs) : this(args = parcelableClassWithArgs)
} |
tldr; How do I initialize a Mavericks State with Navigation Component arguments when the fragment's ViewModel is using
hiltMavericksViewModelFactory
?I'm trying to figure out how to blend the use of this Mavericks library (version 3.0.6), Hilt, and Navigation Component arguments. I'm not using Compose. I'm using Views. My app is single activity, multiple fragments.
I've also seen the README and various example files within https://github.com/airbnb/mavericks/tree/main/sample-hilt.
My Gradle setup:
HomeState
in theHomeViewModel
is:I had Mavericks working just fine before I decided to try to install Hilt.
My setup is the following after I dove into setting up Hilt. It works but I'm not initializing the
ViewModel
with any Navigation Component arguments:As expected, I get the crash below if I experiment by commenting out the
hiltMavericksViewModelFactory()
companion object
setupIgnoring the crash for a moment, my overall question again is how I would initialize the
HomeState
with that initialuserFullName
value from the Navigation Component argument bundle?I see mention of
viewModelContext
in theHiltMavericksViewModelFactory
class, so do I need to somehow adjust thatHiltMavericksViewModelFactory
class?I haven't tried it, but would a hack be to pass the arguments (bundle) from the fragment to the
ViewModel
in the fragment'sonCreate()
oronCreateView()
?The text was updated successfully, but these errors were encountered: