Skip to content

Commit

Permalink
feat: added ComposeWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
y9san9 committed May 4, 2024
1 parent ef2c5ce commit ec93759
Show file tree
Hide file tree
Showing 45 changed files with 350 additions and 165 deletions.
2 changes: 1 addition & 1 deletion core/src/commonMain/kotlin/ksm/StateController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ksm
import ksm.annotation.LibraryConstructor
import ksm.builder.StateControllerBuilder
import ksm.context.StateContext
import ksm.context.configuration.plugin.ConfigurationPlugin
import ksm.configuration.plugin.ConfigurationPlugin
import ksm.context.finish
import ksm.finish.once.plugin.FinishOncePlugin
import ksm.lifecycle.plugin.LifecyclePlugin
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ksm.configuration.interceptor

import ksm.annotation.MutateContext
import ksm.context.StateContext

internal class CombinedConfigurationInterceptor(
private val first: ConfigurationInterceptor?,
private val second: ConfigurationInterceptor
) : ConfigurationInterceptor {
@MutateContext
override fun onConfigure(context: StateContext): StateContext {
var applied = context
applied = first?.onConfigure(applied) ?: applied
applied = second.onConfigure(applied)
return applied
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ksm.configuration.interceptor

import ksm.annotation.MutateContext
import ksm.context.StateContext

public fun interface ConfigurationInterceptor {
@MutateContext
public fun onConfigure(context: StateContext): StateContext
}

public operator fun ConfigurationInterceptor?.plus(other: ConfigurationInterceptor): ConfigurationInterceptor {
return CombinedConfigurationInterceptor(
first = this,
second = other
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ksm.context.configuration.interceptor
package ksm.configuration.interceptor

import ksm.context.StateContext
import ksm.context.configuration.plugin.ConfigurationPlugin
import ksm.configuration.plugin.ConfigurationPlugin
import ksm.plugin.plugin

public fun StateContext.addConfigurationInterceptor(interceptor: ConfigurationInterceptor) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package ksm.context.configuration.plugin
package ksm.configuration.plugin

import ksm.annotation.MutateContext
import ksm.context.StateContext
import ksm.context.configuration.interceptor.ConfigurationInterceptor
import ksm.configuration.interceptor.ConfigurationInterceptor
import ksm.configuration.interceptor.plus
import ksm.plugin.Plugin

public object ConfigurationPlugin : Plugin.Singleton<ConfigurationPlugin> {
Expand All @@ -16,11 +17,13 @@ public object ConfigurationPlugin : Plugin.Singleton<ConfigurationPlugin> {
context: StateContext,
interceptor: ConfigurationInterceptor
) {
context.require(ConfigurationStateController).addInterceptor(interceptor)
context.require(ConfigurationStateController).interceptor += interceptor
}

@MutateContext
public fun onConfigure(context: StateContext): StateContext {
return context.require(ConfigurationStateController).onConfigure(context)
return context.require(ConfigurationStateController)
.interceptor?.onConfigure(context)
?: context
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ksm.configuration.plugin

import ksm.configuration.interceptor.ConfigurationInterceptor
import ksm.context.StateContext

internal class ConfigurationStateController : StateContext.Element {
override val key = ConfigurationStateController

var interceptor: ConfigurationInterceptor? = null

companion object : StateContext.Key<ConfigurationStateController>
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package ksm.context

import ksm.annotation.MutateContext
import ksm.context.configuration.plugin.ConfigurationPlugin
import ksm.configuration.plugin.ConfigurationPlugin
import ksm.lifecycle.plugin.LifecyclePlugin

@OptIn(MutateContext::class)
public inline fun StateContext.createChildContext(
setup: (StateContext) -> Unit = {}
): StateContext {
val applied = this[ConfigurationPlugin]
?.onConfigure(context = this)
?: this
var applied = this

applied = this[ConfigurationPlugin]?.onConfigure(applied) ?: applied

// intentionally using this[LifecyclePlugin], not applied[LifecyclePlugin]
// in order to ignore lifecycle interceptors installed in onConfigure
applied = this[LifecyclePlugin]?.onChildCreate(applied) ?: applied

applied.apply(setup)

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package ksm.finish.once.plugin

import ksm.annotation.MutateContext
import ksm.context.StateContext
import ksm.context.configuration.interceptor.ConfigurationInterceptor
import ksm.context.configuration.interceptor.addConfigurationInterceptor
import ksm.configuration.interceptor.ConfigurationInterceptor
import ksm.configuration.interceptor.addConfigurationInterceptor
import ksm.plugin.Plugin

public object FinishOncePlugin : Plugin.Singleton<FinishOncePlugin> {
Expand Down
10 changes: 0 additions & 10 deletions core/src/commonMain/kotlin/ksm/lifecycle/LifecycleInterceptor.kt

This file was deleted.

3 changes: 2 additions & 1 deletion core/src/commonMain/kotlin/ksm/lifecycle/StateContext.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package ksm.lifecycle

import ksm.context.StateContext
import ksm.lifecycle.interceptor.LifecycleInterceptor
import ksm.lifecycle.plugin.LifecyclePlugin
import ksm.plugin.plugin

public fun StateContext.addLifecycleInterceptor(interceptor: LifecycleInterceptor) {
plugin(LifecyclePlugin).addLifecycleInterceptor(
context = this,
observer = interceptor
interceptor = interceptor
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ksm.lifecycle.interceptor

import ksm.context.StateContext

internal class CombinedLifecycleInterceptor(
private val first: LifecycleInterceptor?,
private val second: LifecycleInterceptor
) : LifecycleInterceptor {
override fun onCreate(context: StateContext) {
first?.onCreate(context)
second.onCreate(context)
}

override fun onPause(context: StateContext) {
first?.onPause(context)
second.onPause(context)
}

override fun onResume(context: StateContext) {
first?.onResume(context)
second.onResume(context)
}

override fun onFinish(context: StateContext) {
first?.onFinish(context)
second.onResume(context)
}

override fun onChildCreate(context: StateContext): StateContext {
var applied = context
applied = first?.onChildCreate(applied) ?: applied
applied = second.onChildCreate(applied)
return applied
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ksm.lifecycle.interceptor

import ksm.context.StateContext

public interface LifecycleInterceptor {
public fun onCreate(context: StateContext) {}
public fun onResume(context: StateContext) {}
public fun onPause(context: StateContext) {}
public fun onFinish(context: StateContext) {}
public fun onChildCreate(context: StateContext): StateContext = context
}

public operator fun LifecycleInterceptor?.plus(other: LifecycleInterceptor): LifecycleInterceptor {
return CombinedLifecycleInterceptor(
first = this,
second = other
)
}
30 changes: 3 additions & 27 deletions core/src/commonMain/kotlin/ksm/lifecycle/plugin/LifecycleEntry.kt
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
package ksm.lifecycle.plugin

import ksm.context.StateContext
import ksm.lifecycle.LifecycleInterceptor
import ksm.lifecycle.interceptor.LifecycleInterceptor
import ksm.lifecycle.interceptor.plus

internal class LifecycleEntry : StateContext.Element {
override val key = LifecycleEntry

private val observers = mutableListOf<LifecycleInterceptor>()

fun addInterceptor(observer: LifecycleInterceptor) {
observers += observer
}

fun onCreate(context: StateContext) {
for (observer in observers) {
observer.onCreate(context)
}
}
fun onResume(context: StateContext) {
for (observer in observers) {
observer.onResume(context)
}
}
fun onPause(context: StateContext) {
for (observer in observers) {
observer.onPause(context)
}
}
fun onFinish(context: StateContext) {
for (observer in observers) {
observer.onFinish(context)
}
}
var interceptor: LifecycleInterceptor? = null

companion object : StateContext.Key<LifecycleEntry>
}
22 changes: 13 additions & 9 deletions core/src/commonMain/kotlin/ksm/lifecycle/plugin/LifecyclePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package ksm.lifecycle.plugin

import ksm.annotation.MutateContext
import ksm.context.StateContext
import ksm.context.configuration.interceptor.ConfigurationInterceptor
import ksm.context.configuration.interceptor.addConfigurationInterceptor
import ksm.lifecycle.LifecycleInterceptor
import ksm.configuration.interceptor.ConfigurationInterceptor
import ksm.configuration.interceptor.addConfigurationInterceptor
import ksm.lifecycle.interceptor.LifecycleInterceptor
import ksm.lifecycle.interceptor.plus
import ksm.plugin.Plugin

public object LifecyclePlugin : Plugin.Singleton<LifecyclePlugin> {
Expand All @@ -24,21 +25,24 @@ public object LifecyclePlugin : Plugin.Singleton<LifecyclePlugin> {

public fun addLifecycleInterceptor(
context: StateContext,
observer: LifecycleInterceptor
interceptor: LifecycleInterceptor
) {
context.require(LifecycleEntry).addInterceptor(observer)
context.require(LifecycleEntry).interceptor += interceptor
}

public fun onCreate(context: StateContext) {
context.require(LifecycleEntry).onCreate(context)
context.require(LifecycleEntry).interceptor?.onCreate(context)
}
public fun onResume(context: StateContext) {
context.require(LifecycleEntry).onResume(context)
context.require(LifecycleEntry).interceptor?.onResume(context)
}
public fun onPause(context: StateContext) {
context.require(LifecycleEntry).onPause(context)
context.require(LifecycleEntry).interceptor?.onPause(context)
}
public fun onFinish(context: StateContext) {
context.require(LifecycleEntry).onFinish(context)
context.require(LifecycleEntry).interceptor?.onFinish(context)
}
public fun onChildCreate(context: StateContext): StateContext {
return context.require(LifecycleEntry).interceptor?.onChildCreate(context) ?: context
}
}
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ compose-compiler = "1.5.11"
androidx-navigation = "2.7.7"
androidx-lifecycle = "2.7.0"
ktgbotapi = "11.0.0"
mdi = "0.0.37"
mdi = "0.0.38"

ksm = "0.0.28"
ksm = "0.0.29"

[libraries]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package ksm.coroutines.plugin

import ksm.annotation.MutateContext
import ksm.context.StateContext
import ksm.context.configuration.interceptor.ConfigurationInterceptor
import ksm.context.configuration.interceptor.addConfigurationInterceptor
import ksm.configuration.interceptor.ConfigurationInterceptor
import ksm.configuration.interceptor.addConfigurationInterceptor
import ksm.coroutines.interceptor.AwaitInterceptor
import ksm.plugin.Plugin

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import ksm.annotation.MutateContext
import ksm.context.StateContext
import ksm.context.configuration.interceptor.ConfigurationInterceptor
import ksm.configuration.interceptor.ConfigurationInterceptor
import ksm.plugin.Plugin
import ksm.context.configuration.interceptor.addConfigurationInterceptor
import ksm.configuration.interceptor.addConfigurationInterceptor
import ksm.context.finish
import ksm.lifecycle.LifecycleInterceptor
import ksm.lifecycle.interceptor.LifecycleInterceptor
import ksm.lifecycle.addLifecycleInterceptor
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

public class CoroutinesPlugin(
private val scope: CoroutineScope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import ksm.annotation.LibraryConstructor
import ksm.builder.StateControllerBuilder
import ksm.navigation.compose.plugin.ComposePlugin
import ksm.navigation.compose.plugin.ComposeSerializationStore
import ksm.navigation.compose.wrapper.ComposeWrapper
import ksm.navigation.navigationStateController
import ksm.navigation.serialization.restore

@OptIn(LibraryConstructor::class)
@Composable
public fun rememberStateController(
wrapper: ComposeWrapper? = null,
builder: StateControllerBuilder.() -> Unit
): StateController {
val store = rememberSaveable(
Expand All @@ -23,7 +25,7 @@ public fun rememberStateController(
)
return remember(store) {
navigationStateController {
install(ComposePlugin(store))
install(ComposePlugin(store, wrapper))
builder()
}.apply {
context.restore()
Expand Down
Loading

0 comments on commit ec93759

Please sign in to comment.