-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
Showing
32 changed files
with
654 additions
and
425 deletions.
There are no files selected for viewing
37 changes: 0 additions & 37 deletions
37
decompose/src/main/java/com/arkivanov/decompose/AndroidRouterStateKeeper.kt
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
decompose/src/main/java/com/arkivanov/decompose/BackStack.kt
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
decompose/src/main/java/com/arkivanov/decompose/BackStackExt.kt
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
decompose/src/main/java/com/arkivanov/decompose/ComponentBuilder.kt
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
decompose/src/main/java/com/arkivanov/decompose/ComponentContext.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,11 @@ | ||
package com.arkivanov.decompose | ||
|
||
import androidx.activity.OnBackPressedDispatcher | ||
import androidx.lifecycle.Lifecycle | ||
|
||
interface ComponentContext : RouterFactory { | ||
|
||
val lifecycle: Lifecycle | ||
val savedStateKeeper: SavedStateKeeper | ||
val onBackPressedDispatcher: OnBackPressedDispatcher | ||
} |
14 changes: 14 additions & 0 deletions
14
decompose/src/main/java/com/arkivanov/decompose/ComponentContextExt.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,14 @@ | ||
package com.arkivanov.decompose | ||
|
||
import androidx.lifecycle.Lifecycle | ||
|
||
fun ComponentContext.child(key: String, lifecycle: Lifecycle = this.lifecycle): ComponentContext { | ||
val childSavedStateKeeper = savedStateKeeper.child(key) | ||
|
||
return ComponentContextImpl( | ||
lifecycle, | ||
savedStateKeeper = childSavedStateKeeper, | ||
onBackPressedDispatcher = onBackPressedDispatcher, | ||
routerFactory = DefaultRouterFactory(lifecycle, childSavedStateKeeper, onBackPressedDispatcher) | ||
) | ||
} |
11 changes: 11 additions & 0 deletions
11
decompose/src/main/java/com/arkivanov/decompose/ComponentContextImpl.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,11 @@ | ||
package com.arkivanov.decompose | ||
|
||
import androidx.activity.OnBackPressedDispatcher | ||
import androidx.lifecycle.Lifecycle | ||
|
||
internal class ComponentContextImpl( | ||
override val lifecycle: Lifecycle, | ||
override val savedStateKeeper: SavedStateKeeper, | ||
override val onBackPressedDispatcher: OnBackPressedDispatcher, | ||
routerFactory: RouterFactory | ||
) : ComponentContext, RouterFactory by routerFactory |
38 changes: 38 additions & 0 deletions
38
decompose/src/main/java/com/arkivanov/decompose/DefaultRouterFactory.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,38 @@ | ||
package com.arkivanov.decompose | ||
|
||
import android.os.Parcelable | ||
import androidx.activity.OnBackPressedDispatcher | ||
import androidx.lifecycle.Lifecycle | ||
|
||
internal class DefaultRouterFactory( | ||
private val lifecycle: Lifecycle, | ||
private val savedStateKeeper: SavedStateKeeper, | ||
private val onBackPressedDispatcher: OnBackPressedDispatcher | ||
) : RouterFactory { | ||
|
||
override fun <C : Parcelable> router( | ||
initialConfiguration: C, | ||
configurationClassLoader: ClassLoader?, | ||
key: String, | ||
handleBackButton: Boolean, | ||
componentFactory: (configuration: C, ComponentContext) -> Component | ||
): Router<C> = | ||
RouterImpl( | ||
initialConfiguration = initialConfiguration, | ||
configurationClassLoader = configurationClassLoader, | ||
lifecycle = lifecycle, | ||
savedStateKeeper = savedStateKeeper, | ||
savedStateKey = key, | ||
onBackPressedDispatcher = onBackPressedDispatcher.takeIf { handleBackButton } | ||
) { configuration, lifecycle, savedStateKeeper -> | ||
componentFactory( | ||
configuration, | ||
ComponentContextImpl( | ||
lifecycle, | ||
savedStateKeeper, | ||
onBackPressedDispatcher, | ||
DefaultRouterFactory(lifecycle, savedStateKeeper, onBackPressedDispatcher) | ||
) | ||
) | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
decompose/src/main/java/com/arkivanov/decompose/DefaultSavedStateKeeper.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,52 @@ | ||
package com.arkivanov.decompose | ||
|
||
import android.os.Bundle | ||
|
||
internal class DefaultSavedStateKeeper( | ||
private val savedState: Bundle? | ||
) : SavedStateKeeper { | ||
|
||
private val suppliers = HashMap<String, () -> Bundle>() | ||
private var onFirstSupplierAdded: (() -> Unit)? = null | ||
private var onLastSupplierRemoved: (() -> Unit)? = null | ||
|
||
fun setCallbacks(onFirstSupplierAdded: (() -> Unit)? = null, onLastSupplierRemoved: (() -> Unit)? = null) { | ||
this.onFirstSupplierAdded = onFirstSupplierAdded | ||
this.onLastSupplierRemoved = onLastSupplierRemoved | ||
} | ||
|
||
override fun consume(key: String): Bundle? { | ||
val bundle = savedState?.getBundle(key) ?: return null | ||
savedState.remove(key) | ||
|
||
return bundle | ||
} | ||
|
||
override fun register(key: String, supplier: () -> Bundle) { | ||
check(!suppliers.containsKey(key)) { "Another supplier is already registered for the key: $key" } | ||
|
||
val isFirst = suppliers.isEmpty() | ||
suppliers[key] = supplier | ||
|
||
if (isFirst) { | ||
onFirstSupplierAdded?.invoke() | ||
} | ||
} | ||
|
||
override fun unregister(key: String) { | ||
check(key in suppliers) { "No supplier is registered for the key: $key" } | ||
|
||
suppliers -= key | ||
|
||
if (suppliers.isEmpty()) { | ||
onLastSupplierRemoved?.invoke() | ||
} | ||
} | ||
|
||
fun save(): Bundle = | ||
Bundle().apply { | ||
suppliers.forEach { (key, supplier) -> | ||
putBundle(key, supplier()) | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
decompose/src/main/java/com/arkivanov/decompose/LifecycleExt.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,15 @@ | ||
package com.arkivanov.decompose | ||
|
||
import androidx.lifecycle.DefaultLifecycleObserver | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleOwner | ||
|
||
internal inline fun Lifecycle.doOnDestroy(crossinline block: () -> Unit) { | ||
addObserver( | ||
object : DefaultLifecycleObserver { | ||
override fun onDestroy(owner: LifecycleOwner) { | ||
block() | ||
} | ||
} | ||
) | ||
} |
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
30 changes: 0 additions & 30 deletions
30
decompose/src/main/java/com/arkivanov/decompose/ParcelableRouterStateKeeper.kt
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
decompose/src/main/java/com/arkivanov/decompose/RootComponentBuilder.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,41 @@ | ||
package com.arkivanov.decompose | ||
|
||
import androidx.activity.OnBackPressedDispatcher | ||
import androidx.activity.OnBackPressedDispatcherOwner | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.savedstate.SavedStateRegistry | ||
import androidx.savedstate.SavedStateRegistryOwner | ||
|
||
@Composable | ||
fun RootComponent( | ||
savedStateRegistry: SavedStateRegistry, | ||
onBackPressedDispatcher: OnBackPressedDispatcher, | ||
factory: (ComponentContext) -> Component | ||
) { | ||
val lifecycle = lifecycle() | ||
|
||
val component = | ||
remember { | ||
val savedStateKeeper = rootSavedStateKeeper(savedStateRegistry) | ||
val routerFactory = DefaultRouterFactory(lifecycle, savedStateKeeper, onBackPressedDispatcher) | ||
factory(ComponentContextImpl(lifecycle, savedStateKeeper, onBackPressedDispatcher, routerFactory)) | ||
} | ||
|
||
component.content() | ||
} | ||
|
||
@Composable | ||
fun <T> T.RootComponent(factory: (ComponentContext) -> Component) where T : SavedStateRegistryOwner, T : OnBackPressedDispatcherOwner { | ||
RootComponent(savedStateRegistry, onBackPressedDispatcher, factory) | ||
} | ||
|
||
private fun rootSavedStateKeeper(registry: SavedStateRegistry): SavedStateKeeper = | ||
DefaultSavedStateKeeper(registry.consumeRestoredStateForKey(KEY_ROOT_SAVED_STATE_KEEPER_STATE)).also { keeper -> | ||
keeper.setCallbacks( | ||
onFirstSupplierAdded = { registry.registerSavedStateProvider(KEY_ROOT_SAVED_STATE_KEEPER_STATE, keeper::save) }, | ||
onLastSupplierRemoved = { registry.unregisterSavedStateProvider(KEY_ROOT_SAVED_STATE_KEEPER_STATE) } | ||
) | ||
} | ||
|
||
private const val KEY_ROOT_SAVED_STATE_KEEPER_STATE = "ROOT_SAVED_STATE_KEEPER_STATE" |
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 |
---|---|---|
@@ -1,27 +1,14 @@ | ||
package com.arkivanov.decompose | ||
|
||
import androidx.activity.OnBackPressedDispatcher | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.lifecycle.Lifecycle | ||
import android.os.Parcelable | ||
|
||
interface Router<in C> : Component { | ||
interface Router<C : Parcelable> : Component { | ||
|
||
val stackSize: Int | ||
|
||
fun getStack(): List<C> | ||
|
||
fun push(configuration: C) | ||
|
||
fun pop() | ||
} | ||
|
||
fun <C> Router( | ||
initialConfiguration: C, | ||
lifecycle: Lifecycle, | ||
stateKeeper: RouterStateKeeper<C>? = null, | ||
onBackPressedDispatcher: OnBackPressedDispatcher? = null, | ||
resolve: (configuration: C, Lifecycle) -> Component | ||
): Router<C> = | ||
RouterImpl( | ||
initialConfiguration = initialConfiguration, | ||
lifecycle = lifecycle, | ||
stateKeeper = stateKeeper, | ||
backPressedDispatcher = onBackPressedDispatcher, | ||
resolve = resolve | ||
) |
14 changes: 14 additions & 0 deletions
14
decompose/src/main/java/com/arkivanov/decompose/RouterFactory.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,14 @@ | ||
package com.arkivanov.decompose | ||
|
||
import android.os.Parcelable | ||
|
||
interface RouterFactory { | ||
|
||
fun <C : Parcelable> router( | ||
initialConfiguration: C, | ||
configurationClassLoader: ClassLoader?, | ||
key: String = "DefaultRouter", | ||
handleBackButton: Boolean = false, | ||
componentFactory: (configuration: C, ComponentContext) -> Component | ||
): Router<C> | ||
} |
Oops, something went wrong.