forked from bumble-tech/appyx
-
Notifications
You must be signed in to change notification settings - Fork 0
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
75fe923
commit be19efe
Showing
33 changed files
with
1,929 additions
and
15 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
108 changes: 108 additions & 0 deletions
108
navmodel-samples/src/main/kotlin/com/bumble/appyx/navmodel/dualbackstack/DualBackStack.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,108 @@ | ||
package com.bumble.appyx.navmodel.dualbackstack | ||
|
||
import android.os.Parcelable | ||
import com.bumble.appyx.core.navigation.BaseNavModel | ||
import com.bumble.appyx.core.navigation.NavKey | ||
import com.bumble.appyx.core.navigation.Operation.Noop | ||
import com.bumble.appyx.core.navigation.onscreen.OnScreenStateResolver | ||
import com.bumble.appyx.core.navigation.operationstrategies.ExecuteImmediately | ||
import com.bumble.appyx.core.navigation.operationstrategies.OperationStrategy | ||
import com.bumble.appyx.core.state.SavedStateMap | ||
import com.bumble.appyx.navmodel.dualbackstack.DualBackStack.State | ||
import com.bumble.appyx.navmodel.dualbackstack.DualBackStack.State.Active1 | ||
import com.bumble.appyx.navmodel.dualbackstack.DualBackStack.State.Active2 | ||
import com.bumble.appyx.navmodel.dualbackstack.DualBackStack.State.Destroyed1 | ||
import com.bumble.appyx.navmodel.dualbackstack.DualBackStack.State.Destroyed2 | ||
import com.bumble.appyx.navmodel.dualbackstack.DualBackStack.State.StashedInBackStack1 | ||
import com.bumble.appyx.navmodel.dualbackstack.backpresshandler.DualBackPressHandlerStrategy | ||
import com.bumble.appyx.navmodel.dualbackstack.backpresshandler.PopBackPressHandler | ||
import com.bumble.appyx.navmodel.dualbackstack.operation.panelModeChanged | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.launch | ||
import kotlinx.parcelize.Parcelize | ||
|
||
class DualBackStack<NavTarget : Any>( | ||
leftElement: NavTarget, | ||
rightElement: NavTarget? = null, | ||
savedStateMap: SavedStateMap?, | ||
private val showTwoPanelsFlow: StateFlow<Boolean>, | ||
key: String = KEY_NAV_MODEL, | ||
backPressHandler: DualBackPressHandlerStrategy<NavTarget> = PopBackPressHandler(), | ||
operationStrategy: OperationStrategy<NavTarget, State> = ExecuteImmediately(), | ||
screenResolver: OnScreenStateResolver<State> = DualBackStackOnScreenResolver | ||
) : BaseNavModel<NavTarget, State>( | ||
backPressHandler = backPressHandler, | ||
screenResolver = screenResolver, | ||
operationStrategy = operationStrategy, | ||
finalStates = setOf(Destroyed1, Destroyed2), | ||
savedStateMap = savedStateMap, | ||
key = key, | ||
) { | ||
val twoPanelsEnabled: Boolean | ||
get() = showTwoPanelsFlow.value | ||
|
||
init { | ||
backPressHandler.setTwoPanelsEnabledFunction { twoPanelsEnabled } | ||
|
||
scope.launch { | ||
var initialPanelModeSet = false | ||
showTwoPanelsFlow.collectLatest { twoPanelsEnabled -> | ||
if (initialPanelModeSet) { | ||
panelModeChanged(twoPanelsEnabled) | ||
} | ||
initialPanelModeSet = true | ||
} | ||
} | ||
} | ||
|
||
sealed class State(val panel: Panel) : Parcelable { | ||
@Parcelize | ||
object Created1 : State(Panel.PANEL_1) | ||
|
||
@Parcelize | ||
object Created2 : State(Panel.PANEL_2) | ||
|
||
@Parcelize | ||
object Active1 : State(Panel.PANEL_1) | ||
|
||
@Parcelize | ||
object Active2 : State(Panel.PANEL_2) | ||
|
||
@Parcelize | ||
object StashedInBackStack1 : State(Panel.PANEL_1) | ||
|
||
@Parcelize | ||
object StashedInBackStack2 : State(Panel.PANEL_2) | ||
|
||
@Parcelize | ||
object Destroyed1 : State(Panel.PANEL_1) | ||
|
||
@Parcelize | ||
object Destroyed2 : State(Panel.PANEL_2) | ||
} | ||
|
||
enum class Panel { | ||
PANEL_1, PANEL_2 | ||
} | ||
|
||
override val initialElements by lazy { | ||
listOfNotNull( | ||
DualBackStackElement( | ||
key = NavKey(leftElement), | ||
fromState = if (rightElement == null || twoPanelsEnabled) Active1 else StashedInBackStack1, | ||
targetState = if (rightElement == null || twoPanelsEnabled) Active1 else StashedInBackStack1, | ||
operation = Noop() | ||
), | ||
rightElement?.let { element -> | ||
DualBackStackElement( | ||
key = NavKey(element), | ||
fromState = Active2, | ||
targetState = Active2, | ||
operation = Noop() | ||
) | ||
} | ||
) | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
...l-samples/src/main/kotlin/com/bumble/appyx/navmodel/dualbackstack/DualBackStackElement.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,6 @@ | ||
package com.bumble.appyx.navmodel.dualbackstack | ||
|
||
import com.bumble.appyx.core.navigation.NavElement | ||
import com.bumble.appyx.navmodel.dualbackstack.DualBackStack.State | ||
|
||
typealias DualBackStackElement<T> = NavElement<T, State> |
6 changes: 6 additions & 0 deletions
6
...-samples/src/main/kotlin/com/bumble/appyx/navmodel/dualbackstack/DualBackStackElements.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,6 @@ | ||
package com.bumble.appyx.navmodel.dualbackstack | ||
|
||
import com.bumble.appyx.core.navigation.NavElements | ||
import com.bumble.appyx.navmodel.dualbackstack.DualBackStack.State | ||
|
||
typealias DualBackStackElements<T> = NavElements<T, State> |
19 changes: 19 additions & 0 deletions
19
.../src/main/kotlin/com/bumble/appyx/navmodel/dualbackstack/DualBackStackOnScreenResolver.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,19 @@ | ||
package com.bumble.appyx.navmodel.dualbackstack | ||
|
||
import com.bumble.appyx.core.navigation.onscreen.OnScreenStateResolver | ||
import com.bumble.appyx.navmodel.dualbackstack.DualBackStack.State | ||
|
||
object DualBackStackOnScreenResolver : OnScreenStateResolver<State> { | ||
|
||
override fun isOnScreen(state: State): Boolean = | ||
when (state) { | ||
State.Created1, | ||
State.Created2, | ||
State.StashedInBackStack1, | ||
State.StashedInBackStack2, | ||
State.Destroyed1, | ||
State.Destroyed2 -> false | ||
State.Active1, | ||
State.Active2 -> true | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
.../com/bumble/appyx/navmodel/dualbackstack/backpresshandler/DualBackPressHandlerStrategy.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,10 @@ | ||
package com.bumble.appyx.navmodel.dualbackstack.backpresshandler | ||
|
||
import com.bumble.appyx.core.navigation.backpresshandlerstrategies.BackPressHandlerStrategy | ||
import com.bumble.appyx.navmodel.dualbackstack.DualBackStack | ||
|
||
interface DualBackPressHandlerStrategy<NavTarget : Any> : | ||
BackPressHandlerStrategy<NavTarget, DualBackStack.State> { | ||
|
||
fun setTwoPanelsEnabledFunction(enabledFunc: () -> Boolean) | ||
} |
33 changes: 33 additions & 0 deletions
33
...in/kotlin/com/bumble/appyx/navmodel/dualbackstack/backpresshandler/PopBackPressHandler.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,33 @@ | ||
package com.bumble.appyx.navmodel.dualbackstack.backpresshandler | ||
|
||
import com.bumble.appyx.core.navigation.backpresshandlerstrategies.BaseBackPressHandlerStrategy | ||
import com.bumble.appyx.navmodel.dualbackstack.DualBackStack | ||
import com.bumble.appyx.navmodel.dualbackstack.DualBackStackElements | ||
import com.bumble.appyx.navmodel.dualbackstack.operation.Pop | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
|
||
class PopBackPressHandler<NavTarget : Any> : | ||
BaseBackPressHandlerStrategy<NavTarget, DualBackStack.State>(), | ||
DualBackPressHandlerStrategy<NavTarget> { | ||
|
||
private lateinit var twoPanelsEnabledFunc: () -> Boolean | ||
|
||
override val canHandleBackPressFlow: Flow<Boolean> by lazy { | ||
navModel.elements.map(::areThereStashedElements) | ||
} | ||
|
||
private fun areThereStashedElements(elements: DualBackStackElements<NavTarget>) = | ||
elements.any { | ||
it.targetState == DualBackStack.State.StashedInBackStack1 || | ||
it.targetState == DualBackStack.State.StashedInBackStack2 | ||
} | ||
|
||
override fun onBackPressed() { | ||
navModel.accept(Pop(twoPanelsEnabledFunc())) | ||
} | ||
|
||
override fun setTwoPanelsEnabledFunction(enabledFunc: () -> Boolean) { | ||
twoPanelsEnabledFunc = enabledFunc | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...s/src/main/kotlin/com/bumble/appyx/navmodel/dualbackstack/operation/BackStackOperation.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,10 @@ | ||
package com.bumble.appyx.navmodel.dualbackstack.operation | ||
|
||
import com.bumble.appyx.core.navigation.NavElement | ||
import com.bumble.appyx.core.navigation.Operation | ||
import com.bumble.appyx.navmodel.dualbackstack.DualBackStack | ||
|
||
sealed interface BackStackOperation<T> : Operation<T, DualBackStack.State> | ||
|
||
fun <NavTarget, State> NavElement<NavTarget, State>.eitherState(state: State): Boolean = | ||
fromState == state || targetState == state |
Oops, something went wrong.