Skip to content

Commit

Permalink
navigate between floating windows
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielittner committed Jun 30, 2023
1 parent 1b6d012 commit b658fb5
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ dependencies {
implementation(libs.androidx.compose.material)
implementation(libs.mad.codegen)
implementation(libs.mad.codegen.scope)
implementation(projects.feature.dialog.nav)
implementation(projects.feature.screen.nav)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,26 @@ package com.freeletics.mad.sample.feature.bottomsheet
import com.freeletics.mad.codegen.ScopeTo
import com.freeletics.mad.navigation.NavEventNavigator
import com.freeletics.mad.sample.feature.bottomsheet.nav.BottomSheetRoute
import com.freeletics.mad.sample.feature.dialog.nav.DialogRoute
import com.freeletics.mad.sample.feature.screen.nav.ScreenRoute
import com.squareup.anvil.annotations.ContributesBinding
import javax.inject.Inject

@ScopeTo(BottomSheetRoute::class)
@ContributesBinding(BottomSheetRoute::class, NavEventNavigator::class)
class BottomSheetNavigator @Inject constructor() : NavEventNavigator()
class BottomSheetNavigator @Inject constructor(
private val route: BottomSheetRoute,
) : NavEventNavigator() {

fun navigateToScreen() {
navigateTo(ScreenRoute(route.number + 1))
}

fun navigateToDialog() {
navigateTo(DialogRoute(route.number + 1))
}

fun navigateToBottomSheet() {
navigateTo(BottomSheetRoute(route.number + 1))
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.freeletics.mad.sample.feature.bottomsheet

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicText
import androidx.compose.material3.ExperimentalMaterial3Api
Expand All @@ -22,6 +25,7 @@ import com.freeletics.mad.sample.feature.bottomsheet.nav.BottomSheetRoute
)
@Composable
fun BottomSheetScreen(
state: BottomSheetState,
sendAction: (BottomSheetAction) -> Unit,
) {
ModalBottomSheet(onDismissRequest = { sendAction(BottomSheetAction.DismissRequested) }) {
Expand All @@ -31,7 +35,28 @@ fun BottomSheetScreen(
.padding(vertical = 48.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
BasicText("Feature Bottom Sheet")
BasicText("Feature Bottom Sheet ${state.number}")

Spacer(Modifier.height(12.dp))

BasicText(
modifier = Modifier.clickable { sendAction(BottomSheetAction.ScreenButtonClicked) },
text = "Open Screen",
)

Spacer(Modifier.height(12.dp))

BasicText(
modifier = Modifier.clickable { sendAction(BottomSheetAction.DialogButtonClicked) },
text = "Open Dialog",
)

Spacer(Modifier.height(12.dp))

BasicText(
modifier = Modifier.clickable { sendAction(BottomSheetAction.BottomSheetButtonClicked) },
text = "Open Bottom Sheet",
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
package com.freeletics.mad.sample.feature.bottomsheet

import com.freeletics.mad.sample.feature.bottomsheet.nav.BottomSheetRoute
import com.freeletics.mad.statemachine.StateMachine
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.MutableStateFlow

sealed interface BottomSheetState

object Init : BottomSheetState
data class BottomSheetState(val number: Int)

sealed interface BottomSheetAction {
object DismissRequested : BottomSheetAction
object ScreenButtonClicked : BottomSheetAction
object DialogButtonClicked : BottomSheetAction
object BottomSheetButtonClicked : BottomSheetAction
}

class BottomSheetStateMachine @Inject constructor(
route: BottomSheetRoute,
private val navigator: BottomSheetNavigator,
) : StateMachine<BottomSheetState, BottomSheetAction> {
override val state: Flow<BottomSheetState> = flowOf(Init)
private val _state = MutableStateFlow(BottomSheetState(route.number))
override val state: Flow<BottomSheetState> = _state

override suspend fun dispatch(action: BottomSheetAction) {
when (action) {
BottomSheetAction.DismissRequested -> navigator.navigateBack()
BottomSheetAction.ScreenButtonClicked -> navigator.navigateToScreen()
BottomSheetAction.DialogButtonClicked -> navigator.navigateToDialog()
BottomSheetAction.BottomSheetButtonClicked -> navigator.navigateToBottomSheet()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import com.freeletics.mad.navigation.NavRoute
import kotlinx.parcelize.Parcelize

@Parcelize
object BottomSheetRoute : NavRoute
data class BottomSheetRoute(val number: Int) : NavRoute
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ dependencies {
implementation(libs.androidx.compose.foundation)
implementation(libs.mad.codegen)
implementation(libs.mad.codegen.scope)
implementation(projects.feature.bottomSheet.nav)
implementation(projects.feature.screen.nav)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@ package com.freeletics.mad.sample.feature.dialog

import com.freeletics.mad.codegen.ScopeTo
import com.freeletics.mad.navigation.NavEventNavigator
import com.freeletics.mad.sample.feature.bottomsheet.nav.BottomSheetRoute
import com.freeletics.mad.sample.feature.dialog.nav.DialogRoute
import com.freeletics.mad.sample.feature.screen.nav.ScreenRoute
import com.squareup.anvil.annotations.ContributesBinding
import javax.inject.Inject

@ScopeTo(DialogRoute::class)
@ContributesBinding(DialogRoute::class, NavEventNavigator::class)
class DialogNavigator @Inject constructor() : NavEventNavigator()
class DialogNavigator @Inject constructor(
private val route: DialogRoute,
) : NavEventNavigator() {
fun navigateToScreen() {
navigateTo(ScreenRoute(route.number + 1))
}

fun navigateToDialog() {
navigateTo(DialogRoute(route.number + 1))
}

fun navigateToBottomSheet() {
navigateTo(BottomSheetRoute(route.number + 1))
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.freeletics.mad.sample.feature.dialog

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
Expand All @@ -22,6 +25,7 @@ import com.freeletics.mad.sample.feature.dialog.nav.DialogRoute
)
@Composable
fun DialogScreen(
state: DialogState,
sendAction: (DialogAction) -> Unit,
) {
Dialog(onDismissRequest = { sendAction(DialogAction.DismissRequested) }) {
Expand All @@ -32,7 +36,28 @@ fun DialogScreen(
.padding(vertical = 48.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
BasicText("Feature Dialog")
BasicText("Feature Dialog ${state.number}")

Spacer(Modifier.height(12.dp))

BasicText(
modifier = Modifier.clickable { sendAction(DialogAction.ScreenButtonClicked) },
text = "Open Screen",
)

Spacer(Modifier.height(12.dp))

BasicText(
modifier = Modifier.clickable { sendAction(DialogAction.DialogButtonClicked) },
text = "Open Dialog",
)

Spacer(Modifier.height(12.dp))

BasicText(
modifier = Modifier.clickable { sendAction(DialogAction.BottomSheetButtonClicked) },
text = "Open Bottom Sheet",
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package com.freeletics.mad.sample.feature.dialog

import com.freeletics.mad.sample.feature.dialog.nav.DialogRoute
import com.freeletics.mad.statemachine.StateMachine
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow

object DialogState
data class DialogState(val number: Int)

sealed interface DialogAction {
object DismissRequested : DialogAction
object ScreenButtonClicked : DialogAction
object DialogButtonClicked : DialogAction
object BottomSheetButtonClicked : DialogAction
}

class DialogStateMachine @Inject constructor(
route: DialogRoute,
private val navigator: DialogNavigator,
) : StateMachine<DialogState, DialogAction> {
private val _state = MutableStateFlow(DialogState)
private val _state = MutableStateFlow(DialogState(route.number))
override val state: Flow<DialogState> = _state

override suspend fun dispatch(action: DialogAction) {
when (action) {
DialogAction.DismissRequested -> navigator.navigateBack()
DialogAction.ScreenButtonClicked -> navigator.navigateToScreen()
DialogAction.DialogButtonClicked -> navigator.navigateToDialog()
DialogAction.BottomSheetButtonClicked -> navigator.navigateToBottomSheet()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import com.freeletics.mad.navigation.NavRoute
import kotlinx.parcelize.Parcelize

@Parcelize
object DialogRoute : NavRoute
data class DialogRoute(val number: Int) : NavRoute
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class RootNavigator @Inject constructor() : NavEventNavigator() {
}

fun navigateToDialog() {
navigateTo(DialogRoute)
navigateTo(DialogRoute(1))
}

fun navigateToBottomSheet() {
navigateTo(BottomSheetRoute)
navigateTo(BottomSheetRoute(1))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class ScreenNavigator @Inject constructor(
}

fun navigateToDialog() {
navigateTo(DialogRoute)
navigateTo(DialogRoute(route.number + 1))
}

fun navigateToBottomSheet() {
navigateTo(BottomSheetRoute)
navigateTo(BottomSheetRoute(route.number + 1))
}
}

0 comments on commit b658fb5

Please sign in to comment.