-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8f4bfc
commit 51803a0
Showing
15 changed files
with
229 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
31 changes: 31 additions & 0 deletions
31
sample/src/main/java/cafe/adriel/voyager/sample/bottomSheetNavigation/BackScreen.kt
This file contains 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,31 @@ | ||
package cafe.adriel.voyager.sample.bottomSheetNavigation | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import cafe.adriel.voyager.core.screen.Screen | ||
import cafe.adriel.voyager.navigator.bottomSheet.LocalBottomSheetNavigator | ||
import cafe.adriel.voyager.sample.basicNavigation.BasicNavigationScreen | ||
|
||
class BackScreen : Screen { | ||
|
||
@Composable | ||
override fun Content() { | ||
val bottomSheetNavigator = LocalBottomSheetNavigator.current | ||
|
||
Box( | ||
contentAlignment = Alignment.Center, | ||
modifier = Modifier.fillMaxSize() | ||
) { | ||
Button( | ||
onClick = { bottomSheetNavigator.show(BasicNavigationScreen(index = 0, wrapContent = true)) } | ||
) { | ||
Text(text = "Show BottomSheet") | ||
} | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...in/java/cafe/adriel/voyager/sample/bottomSheetNavigation/BottomSheetNavigationActivity.kt
This file contains 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,22 @@ | ||
package cafe.adriel.voyager.sample.bottomSheetNavigation | ||
|
||
import android.os.Bundle | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.compose.material.ExperimentalMaterialApi | ||
import cafe.adriel.voyager.navigator.Navigator | ||
import cafe.adriel.voyager.navigator.bottomSheet.BottomSheetNavigator | ||
|
||
class BottomSheetNavigationActivity : ComponentActivity() { | ||
|
||
@ExperimentalMaterialApi | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
setContent { | ||
BottomSheetNavigator { | ||
Navigator(BackScreen()) | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 @@ | ||
/build |
This file contains 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,25 @@ | ||
apply plugin: "com.android.library" | ||
apply from: "../android-module.gradle" | ||
|
||
android { | ||
defaultConfig { | ||
consumerProguardFiles "consumer-rules.pro" | ||
} | ||
kotlinOptions { | ||
freeCompilerArgs += '-Xexplicit-api=strict' | ||
} | ||
} | ||
|
||
dependencies { | ||
api projects.voyagerCore | ||
implementation projects.voyagerNavigator | ||
|
||
implementation libs.compose.runtimeSaveable | ||
implementation libs.compose.material | ||
implementation libs.compose.activity | ||
|
||
testRuntimeOnly libs.junit.engine | ||
testImplementation libs.junit.api | ||
} | ||
|
||
apply plugin: "com.vanniktech.maven.publish" |
Empty file.
This file contains 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,3 @@ | ||
POM_NAME=VoyagerBottomSheetNavigator | ||
POM_ARTIFACT_ID=voyager-bottom-sheet-navigator | ||
POM_PACKAGING=aar |
This file contains 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,2 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest package="cafe.adriel.voyager.navigator.bottomSheet"/> |
112 changes: 112 additions & 0 deletions
112
...navigator/src/main/java/cafe/adriel/voyager/navigator/bottomSheet/BottomSheetNavigator.kt
This file contains 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,112 @@ | ||
package cafe.adriel.voyager.navigator.bottomSheet | ||
|
||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.material.ExperimentalMaterialApi | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.ModalBottomSheetDefaults | ||
import androidx.compose.material.ModalBottomSheetLayout | ||
import androidx.compose.material.ModalBottomSheetState | ||
import androidx.compose.material.ModalBottomSheetValue | ||
import androidx.compose.material.contentColorFor | ||
import androidx.compose.material.rememberModalBottomSheetState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.ProvidableCompositionLocal | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.runtime.staticCompositionLocalOf | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.Shape | ||
import androidx.compose.ui.unit.Dp | ||
import androidx.compose.ui.unit.dp | ||
import cafe.adriel.voyager.core.screen.Screen | ||
import cafe.adriel.voyager.core.stack.Stack | ||
import cafe.adriel.voyager.navigator.CurrentScreen | ||
import cafe.adriel.voyager.navigator.Navigator | ||
import cafe.adriel.voyager.navigator.bottomSheet.internal.BottomSheetNavigatorBackHandler | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
|
||
public typealias BottomSheetNavigatorContent = @Composable (bottomSheetNavigator: BottomSheetNavigator) -> Unit | ||
|
||
public val LocalBottomSheetNavigator: ProvidableCompositionLocal<BottomSheetNavigator> = | ||
staticCompositionLocalOf { error("BottomSheetNavigator not initialized") } | ||
|
||
@ExperimentalMaterialApi | ||
@Composable | ||
public fun BottomSheetNavigator( | ||
modifier: Modifier = Modifier, | ||
hideOnBackPress: Boolean = true, | ||
scrimColor: Color = ModalBottomSheetDefaults.scrimColor, | ||
sheetShape: Shape = MaterialTheme.shapes.large, | ||
sheetElevation: Dp = ModalBottomSheetDefaults.Elevation, | ||
sheetBackgroundColor: Color = MaterialTheme.colors.surface, | ||
sheetContentColor: Color = contentColorFor(sheetBackgroundColor), | ||
sheetContent: BottomSheetNavigatorContent = { CurrentScreen() }, | ||
content: BottomSheetNavigatorContent | ||
) { | ||
val sheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden) | ||
val coroutineScope = rememberCoroutineScope() | ||
|
||
Navigator(HiddenBottomSheetScreen, onBackPressed = null) { navigator -> | ||
val bottomSheetNavigator = remember(navigator, sheetState, coroutineScope) { | ||
BottomSheetNavigator(navigator, sheetState, coroutineScope) | ||
} | ||
|
||
CompositionLocalProvider( | ||
LocalBottomSheetNavigator provides bottomSheetNavigator | ||
) { | ||
ModalBottomSheetLayout( | ||
modifier = modifier, | ||
scrimColor = scrimColor, | ||
sheetState = sheetState, | ||
sheetShape = sheetShape, | ||
sheetElevation = sheetElevation, | ||
sheetBackgroundColor = sheetBackgroundColor, | ||
sheetContentColor = sheetContentColor, | ||
sheetContent = { | ||
BottomSheetNavigatorBackHandler(bottomSheetNavigator, sheetState, hideOnBackPress) | ||
sheetContent(bottomSheetNavigator) | ||
}, | ||
content = { | ||
content(bottomSheetNavigator) | ||
} | ||
) | ||
} | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalMaterialApi::class) | ||
public class BottomSheetNavigator internal constructor( | ||
navigator: Navigator, | ||
private val sheetState: ModalBottomSheetState, | ||
private val coroutineScope: CoroutineScope | ||
) : Stack<Screen> by navigator { | ||
|
||
public val isVisible: Boolean | ||
get() = sheetState.isVisible | ||
|
||
public fun show(screen: Screen) { | ||
coroutineScope.launch { | ||
replaceAll(screen) | ||
sheetState.show() | ||
} | ||
} | ||
|
||
public fun hide() { | ||
coroutineScope.launch { | ||
sheetState.hide() | ||
replaceAll(HiddenBottomSheetScreen) | ||
} | ||
} | ||
} | ||
|
||
private object HiddenBottomSheetScreen : Screen { | ||
|
||
@Composable | ||
override fun Content() { | ||
Spacer(modifier = Modifier.height(1.dp)) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ava/cafe/adriel/voyager/navigator/bottomSheet/internal/BottomSheetNavigatorBackHandler.kt
This file contains 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,24 @@ | ||
package cafe.adriel.voyager.navigator.bottomSheet.internal | ||
|
||
import androidx.activity.compose.BackHandler | ||
import androidx.compose.material.ExperimentalMaterialApi | ||
import androidx.compose.material.ModalBottomSheetState | ||
import androidx.compose.runtime.Composable | ||
import cafe.adriel.voyager.navigator.bottomSheet.BottomSheetNavigator | ||
|
||
@ExperimentalMaterialApi | ||
@Composable | ||
internal fun BottomSheetNavigatorBackHandler( | ||
navigator: BottomSheetNavigator, | ||
sheetState: ModalBottomSheetState, | ||
hideOnBackPress: Boolean | ||
) { | ||
BackHandler( | ||
enabled = sheetState.isVisible, | ||
onBack = { | ||
if (navigator.pop().not() && hideOnBackPress) { | ||
navigator.hide() | ||
} | ||
} | ||
) | ||
} |