Skip to content

Commit

Permalink
added FeaturesNavigator
Browse files Browse the repository at this point in the history
  • Loading branch information
miduch committed Feb 4, 2022
1 parent fc26ac1 commit 4c6256e
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 27 deletions.
3 changes: 1 addition & 2 deletions homescreen/build.gradle
Expand Up @@ -45,8 +45,7 @@ ksp {
}

dependencies {
implementation project(":onboarding")
implementation project(":settings")
implementation project(":shared")

implementation 'io.github.raamcosta.compose-destinations:animations-core:1.2.1-beta'
ksp 'io.github.raamcosta.compose-destinations:ksp:1.2.1-beta'
Expand Down
25 changes: 9 additions & 16 deletions homescreen/src/main/java/com/example/homescreen/HomeScreen.kt
Expand Up @@ -5,16 +5,14 @@ import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.example.homescreen.destinations.HomeScreenLevel2Destination
import com.example.onboarding.destinations.OnBoardingScreenDestination
import com.example.settings.destinations.SettingsScreenDestination
import com.example.shared.FeaturesNavigator
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator

Expand All @@ -24,22 +22,17 @@ import com.ramcosta.composedestinations.navigation.DestinationsNavigator
)
@Destination
@Composable
fun HomeScreen(navigator: DestinationsNavigator) {
fun HomeScreen(
navigator: DestinationsNavigator,
featuresNavigator: FeaturesNavigator,
) {
Column(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colors.background),
.background(Color.Cyan),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
LaunchedEffect(Unit) {
if (listOf(true, false).random()) {
navigator.navigate(
OnBoardingScreenDestination
)
}
}

Text(text = "Homescreen")
Spacer(modifier = Modifier.padding(vertical = 12.dp))
Button(
Expand All @@ -52,7 +45,7 @@ fun HomeScreen(navigator: DestinationsNavigator) {
Spacer(modifier = Modifier.padding(vertical = 12.dp))
Button(
onClick = {
navigator.navigate(SettingsScreenDestination)
featuresNavigator.openSettings()
}
) {
Text(text = "Settings")
Expand All @@ -66,7 +59,7 @@ fun HomeScreenLevel2(navigator: DestinationsNavigator) {
Column(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colors.background),
.background(Color.Magenta),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Expand Down
1 change: 1 addition & 0 deletions navigation/build.gradle
Expand Up @@ -45,6 +45,7 @@ ksp {
}

dependencies {
implementation project(":shared")
implementation project(":onboarding")
implementation project(":homescreen")
implementation project(":settings")
Expand Down
46 changes: 44 additions & 2 deletions navigation/src/main/java/com/example/navigation/AppNavigation.kt
Expand Up @@ -2,18 +2,25 @@ package com.example.navigation

import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.runtime.Composable
import com.example.homescreen.HomeScreen
import com.example.homescreen.destinations.HomeScreenDestination
import com.example.homescreen.destinations.HomeScreenLevel2Destination
import com.example.onboarding.OnBoardingScreen
import com.example.onboarding.destinations.OnBoardingScreenDestination
import com.example.settings.destinations.SettingsScreenDestination
import com.example.settings.destinations.SettingsScreenLevel2Destination
import com.example.shared.FeaturesNavigator
import com.ramcosta.composedestinations.DestinationsNavHost
import com.ramcosta.composedestinations.manualcomposablecalls.DestinationScope
import com.ramcosta.composedestinations.manualcomposablecalls.composable
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.spec.NavGraphSpec

object AppNavGraphs {
val onboarding = object : NavGraphSpec {
override val route = "onboarding_route"
override val destinationsByRoute = listOf(OnBoardingScreenDestination).associateBy { it.route }
override val destinationsByRoute =
listOf(OnBoardingScreenDestination).associateBy { it.route }
override val startRoute = OnBoardingScreenDestination
}

Expand All @@ -37,8 +44,43 @@ object AppNavGraphs {
}
}

class FeaturesNavigatorImpl(private val navigator: DestinationsNavigator): FeaturesNavigator {

override fun closeOnboarding() {
navigator.navigate(HomeScreenDestination) {
this.popUpTo(OnBoardingScreenDestination.route) {
inclusive = true
}
}
}

override fun openSettings() {
navigator.navigate(SettingsScreenDestination)
}
}

fun DestinationScope<*>.featuresNavigator() = FeaturesNavigatorImpl(destinationsNavigator)


@OptIn(ExperimentalAnimationApi::class)
@Composable
fun AppNavigation() {
DestinationsNavHost(navGraph = AppNavGraphs.home)
val showOnboarding = listOf(true, false).random()
DestinationsNavHost(
navGraph = AppNavGraphs.home,
startRoute = if (showOnboarding) AppNavGraphs.onboarding else AppNavGraphs.home.startRoute
) {
composable(HomeScreenDestination) {
HomeScreen(
navigator = destinationsNavigator,
featuresNavigator = featuresNavigator()
)
}
composable(OnBoardingScreenDestination) {
OnBoardingScreen(
navigator = destinationsNavigator,
featuresNavigator = featuresNavigator()
)
}
}
}
1 change: 1 addition & 0 deletions onboarding/build.gradle
Expand Up @@ -45,6 +45,7 @@ ksp {
}

dependencies {
implementation project(":shared")

implementation 'io.github.raamcosta.compose-destinations:animations-core:1.2.1-beta'
ksp 'io.github.raamcosta.compose-destinations:ksp:1.2.1-beta'
Expand Down
Expand Up @@ -5,12 +5,13 @@ import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.example.shared.FeaturesNavigator
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator

Expand All @@ -20,19 +21,22 @@ import com.ramcosta.composedestinations.navigation.DestinationsNavigator
)
@Destination
@Composable
fun OnBoardingScreen(navigator: DestinationsNavigator) {
fun OnBoardingScreen(
navigator: DestinationsNavigator,
featuresNavigator: FeaturesNavigator,
) {
Column(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colors.background),
.background(Color.Yellow),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = "Onboarding")
Spacer(modifier = Modifier.padding(vertical = 12.dp))
Button(
onClick = {
navigator.popBackStack()
featuresNavigator.closeOnboarding()
}
) {
Text(text = "Done")
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Expand Up @@ -18,3 +18,4 @@ include ':navigation'
include ':homescreen'
include ':onboarding'
include ':settings'
include ':shared'
1 change: 1 addition & 0 deletions settings/build.gradle
Expand Up @@ -45,6 +45,7 @@ ksp {
}

dependencies {
implementation project(":shared")

implementation 'io.github.raamcosta.compose-destinations:animations-core:1.2.1-beta'
ksp 'io.github.raamcosta.compose-destinations:ksp:1.2.1-beta'
Expand Down
6 changes: 3 additions & 3 deletions settings/src/main/java/com/example/settings/SettingsScreen.kt
Expand Up @@ -5,11 +5,11 @@ import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.example.settings.destinations.SettingsScreenLevel2Destination
import com.ramcosta.composedestinations.annotation.Destination
Expand All @@ -25,7 +25,7 @@ fun SettingsScreen(navigator: DestinationsNavigator) {
Column(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colors.background),
.background(Color.Green),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Expand Down Expand Up @@ -55,7 +55,7 @@ fun SettingsScreenLevel2(navigator: DestinationsNavigator) {
Column(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colors.background),
.background(Color.Red),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Expand Down
24 changes: 24 additions & 0 deletions shared/build.gradle
@@ -0,0 +1,24 @@
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}

android {
compileSdk 31

defaultConfig {
minSdk 24
targetSdk 31
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_11.toString()
}
}

dependencies {
}
5 changes: 5 additions & 0 deletions shared/src/main/AndroidManifest.xml
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shared">

</manifest>
6 changes: 6 additions & 0 deletions shared/src/main/java/com/example/shared/FeaturesNavigator.kt
@@ -0,0 +1,6 @@
package com.example.shared

interface FeaturesNavigator {
fun closeOnboarding()
fun openSettings()
}

0 comments on commit 4c6256e

Please sign in to comment.