Skip to content

Commit

Permalink
feat: added better handling of DIInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
y9san9 committed May 4, 2024
1 parent 9082fdc commit e1b8314
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ public inline fun StateContext.createChildContext(
): StateContext {
var applied = this

applied = this[ConfigurationPlugin]?.onConfigure(applied) ?: applied
applied = applied[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 = applied[LifecyclePlugin]?.onChildCreate(
// this makes us to ignore newly
// installed LifecycleInterceptors from onConfigure
context = this,
afterConfigure = applied
) ?: applied

applied.apply(setup)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ public object LifecyclePlugin : Plugin.Singleton<LifecyclePlugin> {
public fun onFinish(context: StateContext) {
context.require(LifecycleEntry).interceptor?.onFinish(context)
}
public fun onChildCreate(context: StateContext): StateContext {
return context.require(LifecycleEntry).interceptor?.onChildCreate(context) ?: context
public fun onChildCreate(
context: StateContext,
afterConfigure: StateContext
): StateContext {
return context.require(LifecycleEntry)
.interceptor?.onChildCreate(afterConfigure)
?: afterConfigure
}
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ androidx-lifecycle = "2.7.0"
ktgbotapi = "11.0.0"
mdi = "0.0.38"

ksm = "0.0.32"
ksm = "0.0.33"

[libraries]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package ksm.navigation.mdi.plugin
import app.meetacy.di.DI
import app.meetacy.di.builder.DIBuilder
import app.meetacy.di.builder.di
import app.meetacy.di.dependency.Dependencies
import ksm.annotation.MutateContext
import ksm.asStateController
import ksm.context.StateContext
import ksm.configuration.interceptor.ConfigurationInterceptor
import ksm.plugin.Plugin
import ksm.configuration.interceptor.addConfigurationInterceptor
import ksm.lifecycle.interceptor.LifecycleInterceptor
import ksm.lifecycle.addLifecycleInterceptor
import ksm.navigation.mdi.addDIInterceptor
import ksm.navigation.mdi.interceptor.DIInterceptor
import ksm.navigation.mdi.interceptor.plus
import ksm.navigation.state.route.addStateReadyInterceptor
import ksm.navigation.state.route.interceptor.StateRouteInterceptor

public class DIPlugin(
private val root: DI = di { },
Expand All @@ -33,9 +34,18 @@ public class DIPlugin(
private inner class Configuration : ConfigurationInterceptor {
@MutateContext
override fun onConfigure(context: StateContext): StateContext {
val applied = context + DIEntry()
applied.addDIInterceptor(DefaultInterceptor())
return applied
context.addStateReadyInterceptor(StateRoute())
return context + DIEntry()
}
}

private inner class StateRoute : StateRouteInterceptor {
override fun onStateRoute(context: StateContext) {
context.addDIInterceptor(DefaultInterceptor())
val entry = context.require(DIEntry)
val di = entry.di ?: DI(Dependencies.Empty)
val interceptor = entry.interceptor
entry.di = interceptor?.intercept(context, di) ?: di
}
}

Expand All @@ -57,9 +67,7 @@ public class DIPlugin(
}

public fun setDI(context: StateContext, di: DI) {
val entry = context.require(DIEntry)
val interceptor = entry.interceptor
entry.di = interceptor?.intercept(context, di) ?: di
context.require(DIEntry).di = di
}

public companion object : StateContext.Key<DIPlugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ksm.navigation.state.route

import ksm.context.StateContext
import ksm.navigation.state.route.interceptor.StateRouteInterceptor
import ksm.navigation.state.route.plugin.StateRoutePlugin
import ksm.plugin.plugin

public fun StateContext.addStateReadyInterceptor(interceptor: StateRouteInterceptor) {
plugin(StateRoutePlugin).addStateRouteInterceptor(
context = this,
interceptor = interceptor
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ksm.navigation.state.route

import ksm.builder.StateControllerBuilder
import ksm.navigation.state.route.plugin.StateRoutePlugin

public fun StateControllerBuilder.states(block: StateRouteScope.() -> Unit) {
install(StateRoutePlugin(block))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ksm.navigation.state.route.interceptor

import ksm.context.StateContext

internal class CombinedStateRouteInterceptor(
private val first: StateRouteInterceptor?,
private val second: StateRouteInterceptor
) : StateRouteInterceptor {
override fun onStateRoute(context: StateContext) {
first?.onStateRoute(context)
second.onStateRoute(context)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ksm.navigation.state.route.interceptor

import ksm.context.StateContext

public fun interface StateRouteInterceptor {
// Called after StateBuilderScope was built
public fun onStateRoute(context: StateContext)
}

public operator fun StateRouteInterceptor?.plus(other: StateRouteInterceptor): StateRouteInterceptor {
return CombinedStateRouteInterceptor(
first = this,
second = other
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ksm.navigation.state.route.plugin

import ksm.context.StateContext
import ksm.navigation.state.route.interceptor.StateRouteInterceptor

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

var interceptor: StateRouteInterceptor? = null

companion object : StateContext.Key<StateRouteEntry>
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package ksm.navigation.state.route
package ksm.navigation.state.route.plugin

import ksm.annotation.MutateContext
import ksm.context.StateContext
import ksm.configuration.interceptor.ConfigurationInterceptor
import ksm.configuration.interceptor.addConfigurationInterceptor
import ksm.lifecycle.interceptor.LifecycleInterceptor
import ksm.lifecycle.addLifecycleInterceptor
import ksm.navigation.state.route.StateRouteScope
import ksm.navigation.state.route.interceptor.StateRouteInterceptor
import ksm.navigation.state.route.interceptor.plus
import ksm.plugin.Plugin

public class StateRoutePlugin(
Expand All @@ -23,19 +26,27 @@ public class StateRoutePlugin(
@MutateContext
override fun onConfigure(context: StateContext): StateContext {
context.addLifecycleInterceptor(Lifecycle())
return context
return context + StateRouteEntry()
}
}

private inner class Lifecycle : LifecycleInterceptor {
override fun onCreate(context: StateContext) {
val scope = StateRouteScope(context)
block(scope)
context.require(StateRouteEntry).interceptor?.onStateRoute(context)
if (!scope.intercepted) {
error("Cannot launch state because there is no handlers for this")
}
}
}

public fun addStateRouteInterceptor(
context: StateContext,
interceptor: StateRouteInterceptor
) {
context.require(StateRouteEntry).interceptor += interceptor
}

public companion object : StateContext.Key<StateRoutePlugin>
}

0 comments on commit e1b8314

Please sign in to comment.