Git Product home page Git Product logo

Comments (12)

hrach avatar hrach commented on June 10, 2024 2

fun SavedStateHandle.decodeArguments<T>() released in 0.9

from navigation-compose-typed.

StylianosGakis avatar StylianosGakis commented on June 10, 2024 2

Might wanna edit that last message to say decodeArguments so that people don't become confused when they can't find decoredArguments :D

from navigation-compose-typed.

StylianosGakis avatar StylianosGakis commented on June 10, 2024

For the bonus question. We do this https://github.com/HedvigInsurance/android/blob/082e966c5023ee9efaede2f5b61cc4df7b860d32/app/navigation/navigation-compose-typed/src/main/kotlin/com/hedvig/android/navigation/compose/typed/DestinationScopedViewModel.kt#L16-L25
It should just work, give it a shot. We use koin here of course, but edit it accordingly to use hilt instead. The core logic of this is to get the NavBackStackEntry through

val parentEntry: NavBackStackEntry = remember(navController, backStackEntry) {
  navController.getBackStackEntry(createRoutePattern<Dest>())
}

then I would guess that hiltViewModel also accepts it as a parameter to get the right scope

from navigation-compose-typed.

WildOrangutan avatar WildOrangutan commented on June 10, 2024

@kroegerama since library is only extension of Jetpack navigation for compose, you can access arguments from SavedStateHandle. The only issue is, how to access them in a type safe way.

If you for example look at quick start. Article destination has argument id. But to access it, I guess you would probably need to do savedStateHandle["id"]. Not ideal, since library has the key name "under control".

It would probably be nice to have additional extension, that parses arguments, based on destination arguments, if possible.

from navigation-compose-typed.

kroegerama avatar kroegerama commented on June 10, 2024

Oh yes, I am currently using the "raw" method to access the attributes:

    val args = MyNavArgs(
        handle["myId"]!!,
        handle.get<String>("myBoolean")!!.toBoolean()
    )

Especially, the myBoolean attribute is quite "ugly", so I was hoping for a typesafe alternative.

from navigation-compose-typed.

jmhend avatar jmhend commented on June 10, 2024

+1 on this. I'm considering adopting this library, and giving SavedStateHandles the same type-safety afforded to Bundles in decoding would be super helpful.

Either that, or opening up a simple API for the underlying getSerializersModule() so we can write our own SavedStateHandler decoder.

Appreciate your work on this library! :D

from navigation-compose-typed.

hrach avatar hrach commented on June 10, 2024

I'm sorry for the late answer, I somehow missed this issue.

We explicitly pass the wanted navigation arguments to the viewModel through the parameters. In koin using parametersOf(), in Hilt using the assisted parameters. Usually, we pass the whole destination object.

import org.koin.androidx.compose.getViewModel

val viewModel: ProfileViewModel = getViewModel { parametersOf(destination) }

from navigation-compose-typed.

hrach avatar hrach commented on June 10, 2024

Why not read it from the state? My POV:

  • not well documented that this state contains the nav params
  • being explicit about VM "deps" is better than pass them through simple Map

from navigation-compose-typed.

hrach avatar hrach commented on June 10, 2024

Closing as answered. If anything, don't hesitate to comment.

from navigation-compose-typed.

kroegerama avatar kroegerama commented on June 10, 2024

In my opinion, this library definitely needs a way to extract the destination classes from a SavedStateHandle. That's the only correct way to properly deal with process death, if I am not wrong.

Also, creating ViewModels with custom/assisted parameters seems a little bit like a hack for me. That way the parameters would be sent to the ViewModel twice, right? Once via the parameter, and once via the SavedStateHandle.

from navigation-compose-typed.

hrach avatar hrach commented on June 10, 2024

In my opinion, this library definitely needs a way to extract the destination classes from a SavedStateHandle. That's the only correct way to properly deal with process death, if I am not wrong.

The NavHost state is properly restored, the backstack entry is properly decoded and the VM is properly created using those nav params. So my answer is: No, this is not the only correct way.

creating ViewModels with custom/assisted parameters seems a little bit like a hack for me

I disagree, I believe the VM inputs should be clear and not hidden in a map. SavedStateHandle is an implementation detail of state retention, not an ideal way to provide necessary inputs.


But ok, let's be open-minded here - is this feature documented anywhere - I mean - is there a contract that promises SavedStateHandle to contain navigation inputs? If so, I'm very ok to add it. If not, I am open to giving you the possibility.

from navigation-compose-typed.

kroegerama avatar kroegerama commented on June 10, 2024

Thanks for reopening the issue. I really appreciate it.

I'm not sure, if you could count that as documented feature, but google is using this in their official docs:

  • They access the SavedStateHandle here
  • Also, in their typesafe version, they use the SavedStateHandle as a constructor for the navigation arguments here

However, this is your library after all. And if this is against your typical use cases, I can completely see, why you do not want (or have the time) to implement it. It's just that I really like this library and that's the only nice-to-have feature it's missing for me.
(As a side note: I also was able to create a custom navigator that uses the new BottomSheet from Material 3, without the accompanist library - your library made this very easy for me)

from navigation-compose-typed.

Related Issues (18)

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.