I’m using navigation3 and I have the following lay...
# compose-android
m
I’m using navigation3 and I have the following layouts: 1. List + detail on portrait and landscape 2. Single screen mode with List + Detail panes split vertically, on portrait On portrait mode, the user can toggle between options 1 and 2, on landscape they only have option 1. I am wondering how to go about implementing this.
ListDetailSceneStrategy
works great but doesn’t support vertically splitting the screen, thus doesn't fulfil scenario #2. I assume the right way would be to create a new strategy that handles both scenarios, but I would rather not re-create
ListDetailSceneStrategy
logic there. Any thoughts on this?
i
Just have two strategies and swap between them?
m
That was my first thought, however, I had my doubts about what's the "right" way to go about it in terms of Nav3. Effectively, this is one screen with multiple panes that can be arranged in various ways, and as such it feels like the whole layout should be managed by one SceneStrategy. But that means then not being able to reuse
ListDetailSceneStrategy
, and I'd prefer not to rewrite/copy that logic.
i
You could write it either way, but the whole idea of strategies is that you have many, single focused ones and pick the one most appropriate for the situation, be that the configuration of the device or a user setting, whatever
m
What was holding me back from switching between Strategies in one screen was the thought that maybe it was an antipattern.. so doing something like:
Copy code
if (shouldSplitVerticallyOnPortrait) VerticalSplitSceneStrategy else ListDetailSceneStrategy
But on another look at the docs I can see:
SceneStrategy
also provides a convenient
then
infix function, allowing you to chain multiple strategies together. This creates a flexible decision-making pipeline where each strategy can attempt to calculate a
Scene
, and if it can't, it delegates to the next one in the chain.
So, no need for an
if
, but instead handle the logic as mentioned in the above quote
i
Yeah, your
calculateScene
could
return if (inPortrait && isUserSplitScreenEnabled) VerticalSplitScene() else null
1
m
Can confirm that it works, kinda amazing to have two different panes rendered in three different ways in one screen. If someone would've told me I had to implement something like this 4-5 years ago, I'd have walked out of the door 😄
🎉 4