-
Notifications
You must be signed in to change notification settings - Fork 63
List detail no placeholder #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tiwiz
wants to merge
22
commits into
main
Choose a base branch
from
list-detail-no-placeholder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+524
−6
Open
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
d1e7fd0
First commit for List-Detail with variable number of columns
tiwiz d019a52
Started strategy for Three Panes List-List-Detail
tiwiz a420ca1
Initial behaviour - TODO: select multiple items
tiwiz 3d174d9
Initial behaviour - TODO: select multiple items
tiwiz 356b779
Select multiple items, but only show one in the backstack
tiwiz a18d4ea
Add possibility to show third panel with the second. Crashing when go…
tiwiz 462e181
Merge branch 'main' into list-detail-no-placeholder
tiwiz d8af65f
Make List-Detail without placeholders
tiwiz 8e7e3a6
Add README entry
tiwiz 6ac3c4b
Fix PR comments
tiwiz 15b79ec
Introduce support for 3 panes
tiwiz 6fc115f
Introduce support for variable weights
tiwiz 288048d
Introduce support for variable weights
tiwiz 5fa0c86
Code cleanup
tiwiz 0412b5d
Remove single pane scene to leverage default single pane
tiwiz c07d62e
Move helper function closer to its usage
tiwiz ebc76bc
Add some more comments
tiwiz c7785d2
Add possibility to also show SupportingPane
tiwiz 9893103
Fix build issues
tiwiz 29d23c3
Merge branch 'main' into list-detail-no-placeholder
tiwiz 9906549
Fix build issues
tiwiz 3c0967b
Add more customization for BottomSheet
tiwiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
217 changes: 217 additions & 0 deletions
217
.../com/example/nav3recipes/scenes/listdeailnoplaceholder/ListDetailNoPlaceholderActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
/* | ||
* Copyright 2025 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.nav3recipes.scenes.listdeailnoplaceholder | ||
tiwiz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
import android.os.Bundle | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.compose.animation.ExperimentalSharedTransitionApi | ||
import androidx.compose.animation.SharedTransitionLayout | ||
import androidx.compose.animation.SharedTransitionScope | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.BoxWithConstraints | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.grid.GridCells | ||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.ProvidableCompositionLocal | ||
import androidx.compose.runtime.compositionLocalOf | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableIntStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import androidx.navigation3.runtime.NavBackStack | ||
import androidx.navigation3.runtime.NavKey | ||
import androidx.navigation3.runtime.entry | ||
import androidx.navigation3.runtime.entryProvider | ||
import androidx.navigation3.runtime.navEntryDecorator | ||
import androidx.navigation3.runtime.rememberNavBackStack | ||
import androidx.navigation3.runtime.rememberSavedStateNavEntryDecorator | ||
import androidx.navigation3.ui.LocalNavAnimatedContentScope | ||
import androidx.navigation3.ui.NavDisplay | ||
import androidx.navigation3.ui.rememberSceneSetupNavEntryDecorator | ||
import com.example.nav3recipes.content.ContentBase | ||
import com.example.nav3recipes.content.ContentGreen | ||
import com.example.nav3recipes.content.ContentRed | ||
import com.example.nav3recipes.ui.setEdgeToEdgeConfig | ||
import com.example.nav3recipes.ui.theme.colors | ||
import kotlinx.serialization.Serializable | ||
|
||
/** | ||
* This example shows how to create custom layouts using the Scenes API. | ||
* | ||
* A custom Scene, `TwoPaneScene`, will render content in two panes if: | ||
* | ||
* - the window width is over 600dp | ||
* - the last two nav entries on the back stack have indicated that they support being displayed in | ||
* a `TwoPaneScene` in their metadata. | ||
* | ||
* | ||
* @see `TwoPaneScene` | ||
*/ | ||
tiwiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Serializable | ||
private object Home : NavKey | ||
|
||
@Serializable | ||
private data class Product(val id: Int) : NavKey | ||
|
||
@Serializable | ||
private data object Profile : NavKey | ||
|
||
|
||
class ListDetailNoPlaceholderActivity : ComponentActivity() { | ||
|
||
private val mockProducts = List(10) { Product(it) } | ||
|
||
@OptIn(ExperimentalSharedTransitionApi::class) | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
setEdgeToEdgeConfig() | ||
super.onCreate(savedInstanceState) | ||
|
||
setContent { | ||
|
||
val localNavSharedTransitionScope: ProvidableCompositionLocal<SharedTransitionScope> = | ||
compositionLocalOf { | ||
throw IllegalStateException( | ||
"Unexpected access to LocalNavSharedTransitionScope. You must provide a " + | ||
"SharedTransitionScope from a call to SharedTransitionLayout() or " + | ||
"SharedTransitionScope()" | ||
) | ||
} | ||
|
||
|
||
var numberOfColumns by remember { mutableIntStateOf(1) } | ||
|
||
/** | ||
* A [NavEntryDecorator] that wraps each entry in a shared element that is controlled by the | ||
* [Scene]. | ||
*/ | ||
val sharedEntryInSceneNavEntryDecorator = navEntryDecorator<NavKey> { entry -> | ||
with(localNavSharedTransitionScope.current) { | ||
BoxWithConstraints( | ||
Modifier.sharedElement( | ||
rememberSharedContentState(entry.contentKey), | ||
animatedVisibilityScope = LocalNavAnimatedContentScope.current, | ||
), | ||
) { | ||
if (entry.metadata.containsKey(ListDetailNoPlaceholderSceneStrategy.LIST)) { | ||
numberOfColumns = columnsByComposableWidth(maxWidth) | ||
} | ||
entry.Content() | ||
} | ||
} | ||
} | ||
|
||
|
||
val backStack = rememberNavBackStack(Home) | ||
val strategy = | ||
remember { ListDetailNoPlaceholderSceneStrategy<Any>(listInitialWeight = .5f) } | ||
|
||
SharedTransitionLayout { | ||
CompositionLocalProvider(localNavSharedTransitionScope provides this) { | ||
NavDisplay( | ||
backStack = backStack, | ||
onBack = { keysToRemove -> repeat(keysToRemove) { backStack.removeLastOrNull() } }, | ||
entryDecorators = listOf( | ||
sharedEntryInSceneNavEntryDecorator, | ||
rememberSceneSetupNavEntryDecorator(), | ||
rememberSavedStateNavEntryDecorator() | ||
), | ||
sceneStrategy = strategy, | ||
entryProvider = entryProvider { | ||
entry<Home>( | ||
metadata = ListDetailNoPlaceholderSceneStrategy.Companion.list() | ||
) { | ||
ContentRed("Adaptive List") { | ||
val gridCells = GridCells.Fixed(numberOfColumns) | ||
|
||
LazyVerticalGrid( | ||
columns = gridCells, | ||
modifier = Modifier.fillMaxSize() | ||
) { | ||
items(mockProducts.size) { | ||
Text( | ||
text = "Product $it", | ||
modifier = Modifier | ||
.padding(all = 16.dp) | ||
.clickable { | ||
backStack.addProductRoute(it) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
entry<Product>( | ||
metadata = ListDetailNoPlaceholderSceneStrategy.Companion.detail() | ||
) { product -> | ||
ContentBase( | ||
"Product ${product.id} ", | ||
Modifier.background(colors[product.id % colors.size]) | ||
) { | ||
Column(horizontalAlignment = Alignment.CenterHorizontally) { | ||
Button(onClick = { | ||
backStack.addProductRoute(product.id + 1) | ||
}) { | ||
Text("View the next product") | ||
} | ||
Button(onClick = { | ||
backStack.add(Profile) | ||
}) { | ||
Text("View profile") | ||
} | ||
} | ||
} | ||
} | ||
entry<Profile>( | ||
metadata = ListDetailNoPlaceholderSceneStrategy.thirdPanel() | ||
) { | ||
ContentGreen("Profile") | ||
} | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun NavBackStack<NavKey>.addProductRoute(productId: Int) { | ||
val productRoute = | ||
Product(productId) | ||
|
||
val lastItem = last() | ||
if(lastItem is Product) { | ||
// Avoid adding the same product route to the back stack twice. | ||
if(lastItem == productRoute) { | ||
return | ||
} else { | ||
//Only have a single product as detail | ||
remove(lastItem) | ||
add(productRoute) | ||
} | ||
} else { | ||
add(productRoute) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.