Open langsmith opened 10 months ago
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())
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 constructor
setup didn't work. Not sure whether or not it's because I'm not doing the steps 👇🏽
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 _____Directions
and do something like
findNavController().navigate(LoginFragmentDirections.actionLoginFragmentToHome(userFullName = it.userFullName))
I know that custom Parcelable
objects can be passed through as arguments
However, this doesn't really help me from avoiding having to create these custom args classes in the first place 😕
Any thoughts on this @gpeal @elihart ?
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 LoginViewModel
and just fetch it in HomeViewModel
instead of passing it at Navigation Component arguments from one fragment/VM to another.
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 setAsMavericksArgs
extension function, which has made things pretty easy and minimal when going from one fragment/VM to another.
import android.os.Parcelable
import kotlinx.parcelize.Parcelize
@Parcelize
class ParcelableClassWithArgs(
var stringValue: String? = "",
var booleanValue: Boolean? = false,
): Parcelable
Parcelable
extension function 👇🏽
fun Parcelable.setAsMavericksArgs(fragment: Fragment): Bundle {
this.asMavericksArgs().let {
fragment.arguments = it
return it
}
}
Fragment
extension function 👇🏽
fun Fragment.navigate(actionInt: Int, optionalArgs: Bundle? = null) =
findNavController().navigate(actionInt, optionalArgs)
Navigating in the Fragment
👇🏽
navigate(
NavGraphDirections.actionToNextFragment().actionId,
ParcelableClassWithArgs(
...various values
).setAsMavericksArgs(this)
)
Setup in the ViewModel
of the Fragment
that has been navigated to
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()
?