Comments (12)
fun SavedStateHandle.decodeArguments<T>()
released in 0.9
from navigation-compose-typed.
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.
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.
@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.
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.
+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.
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.
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.
Closing as answered. If anything, don't hesitate to comment.
from navigation-compose-typed.
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 ViewModel
s 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.
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.
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)
- Dependency Dashboard
- Method to get current destination HOT 1
- Add proguard rules HOT 4
- DialogResultEffect is called after navigateUp is called HOT 4
- Default arguments for startDestination HOT 5
- Add an option to `popUpTo` a specific destination HOT 5
- Consider supporting the accompanist navigation library which has animation support HOT 2
- Consider enabling registering serializers for types coming from third party libraries inside destination classes HOT 3
- Consider using the stable compose BOM instead of the alpha one from Chris Banes HOT 2
- Posibility to add more serializable types to the `SerializersModule` that this library is using, which aren't necessary of the `Destination` type. HOT 4
- Nested NavHost leads to SerializerAlreadyRegisteredException HOT 1
- Include bottomSheet() in library? HOT 1
- Question around deep link parameters involving enums HOT 5
- Add size transform argument
- ResultDestination with sealed interface as result acting unpredictably HOT 5
- Future because the native impl in Navigation 2.8 HOT 12
- Consider passing `AnimatedContentScope` to the composables HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from navigation-compose-typed.