Giter Site home page Giter Site logo

Comments (6)

langsmith avatar langsmith commented on June 28, 2024

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())

from mavericks.

langsmith avatar langsmith commented on June 28, 2024

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 👇🏽

Screenshot 2023-09-05 at 2 03 42 PM

from mavericks.

langsmith avatar langsmith commented on June 28, 2024

Well things ARE working when I follow the steps above

  1. Screenshot 2023-09-05 at 2 16 40 PM
  2. Screenshot 2023-09-05 at 2 16 43 PM
  3. Screenshot 2023-09-05 at 2 16 52 PM

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

Screenshot 2023-09-05 at 2 58 31 PM

However, this doesn't really help me from avoiding having to create these custom args classes in the first place 😕

from mavericks.

langsmith avatar langsmith commented on June 28, 2024

Any thoughts on this @gpeal @elihart ?

from mavericks.

langsmith avatar langsmith commented on June 28, 2024

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.

from mavericks.

langsmith avatar langsmith commented on June 28, 2024

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)
}

from mavericks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.