Bump agp versions and add navigation 3 dependencypull/1902/head
parent
7cc2b1ae42
commit
a163dd7929
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* https://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.google.samples.apps.nowinandroid.di
|
||||
|
||||
import com.google.samples.apps.nowinandroid.core.navigation.NiaBackStack
|
||||
import com.google.samples.apps.nowinandroid.navigation.TopLevelDestination
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import javax.inject.Singleton
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
object NiaAppNavigation {
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideNiaBackStack(): NiaBackStack =
|
||||
NiaBackStack(startKey = TopLevelDestination.FOR_YOU)
|
||||
}
|
@ -0,0 +1 @@
|
||||
/build
|
@ -0,0 +1,8 @@
|
||||
plugins {
|
||||
alias(libs.plugins.nowinandroid.jvm.library)
|
||||
alias(libs.plugins.nowinandroid.hilt)
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(libs.androidx.navigation3.runtime)
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* https://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.google.samples.apps.nowinandroid.core.navigation
|
||||
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.runtime.snapshots.SnapshotStateList
|
||||
import javax.inject.Inject
|
||||
import kotlin.collections.remove
|
||||
|
||||
class NiaBackStack @Inject constructor(
|
||||
startKey: Any,
|
||||
) {
|
||||
val backStack = mutableStateListOf(startKey)
|
||||
|
||||
// Maintain a stack for each top level route
|
||||
private var topLevelStacks : LinkedHashMap<Any, SnapshotStateList<Any>> = linkedMapOf(
|
||||
startKey to mutableStateListOf(startKey)
|
||||
)
|
||||
|
||||
// Expose the current top level route for consumers
|
||||
var topLevelKey by mutableStateOf(startKey)
|
||||
private set
|
||||
|
||||
private fun updateBackStack() =
|
||||
backStack.apply {
|
||||
clear()
|
||||
addAll(topLevelStacks.flatMap { it.value })
|
||||
}
|
||||
|
||||
fun navigateToTopLevelDestination(key: Any){
|
||||
// If the top level doesn't exist, add it
|
||||
if (topLevelStacks[key] == null){
|
||||
topLevelStacks.put(key, mutableStateListOf(key))
|
||||
} else {
|
||||
// Otherwise just move it to the end of the stacks
|
||||
topLevelStacks.apply {
|
||||
remove(key)?.let {
|
||||
put(key, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
topLevelKey = key
|
||||
updateBackStack()
|
||||
}
|
||||
|
||||
fun navigate(key: Any){
|
||||
println("cfok navigate $key")
|
||||
topLevelStacks[topLevelKey]?.add(key)
|
||||
updateBackStack()
|
||||
}
|
||||
|
||||
fun removeLast(){
|
||||
val removedKey = topLevelStacks[topLevelKey]?.removeLastOrNull()
|
||||
// If the removed key was a top level key, remove the associated top level stack
|
||||
topLevelStacks.remove(removedKey)
|
||||
topLevelKey = topLevelStacks.keys.last()
|
||||
updateBackStack()
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue