Skip to content

Commit

Permalink
feat: added better typing
Browse files Browse the repository at this point in the history
  • Loading branch information
y9san9 committed May 9, 2024
1 parent 3ecf97d commit fdc42bc
Show file tree
Hide file tree
Showing 76 changed files with 453 additions and 274 deletions.
35 changes: 4 additions & 31 deletions core/src/commonMain/kotlin/ksm/StateController.kt
Original file line number Diff line number Diff line change
@@ -1,40 +1,13 @@
package ksm

import ksm.annotation.LibraryConstructor
import ksm.builder.StateControllerBuilder
import ksm.annotation.LibraryApi
import ksm.context.StateContext
import ksm.configuration.plugin.ConfigurationPlugin
import ksm.context.finish
import ksm.finish.once.plugin.FinishOncePlugin
import ksm.lifecycle.plugin.LifecyclePlugin

@LibraryConstructor
public inline fun pluginStateController(
context: StateContext = StateContext.Empty,
enableConfiguration: Boolean = true,
enableLifecycle: Boolean = true,
enableFinishOnce: Boolean = true,
builder: StateControllerBuilder.() -> Unit = {}
): StateController {
return StateControllerBuilder(context).apply {
if (enableConfiguration) install(ConfigurationPlugin)
if (enableLifecycle) install(LifecyclePlugin)
if (enableFinishOnce) install(FinishOncePlugin)

builder()
}.context.asStateController()
}

public interface StateController {
public val context: StateContext
}

public fun StateController.finish() {
context.finish()
}

public fun StateContext.asStateController(): StateController {
return object : StateController {
override val context = this@asStateController
public interface Builder {
@LibraryApi
public var context: StateContext
}
}
7 changes: 7 additions & 0 deletions core/src/commonMain/kotlin/ksm/annotation/LibraryApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ksm.annotation

@RequiresOptIn(
message = "This api is intended to be used by library developers only, consider not to use it",
level = RequiresOptIn.Level.ERROR
)
public annotation class LibraryApi

This file was deleted.

13 changes: 0 additions & 13 deletions core/src/commonMain/kotlin/ksm/builder/StateControllerBuilder.kt

This file was deleted.

11 changes: 0 additions & 11 deletions core/src/commonMain/kotlin/ksm/context/FinishStateContext.kt

This file was deleted.

12 changes: 0 additions & 12 deletions core/src/commonMain/kotlin/ksm/context/InstallPlugin.kt

This file was deleted.

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.34"
ksm = "0.0.35"

[libraries]

Expand Down
2 changes: 1 addition & 1 deletion kotlinx-coroutines/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ plugins {
version = libs.versions.ksm.get()

dependencies {
commonMainApi(projects.core)
commonMainApi(projects.plugin)
commonMainImplementation(libs.kotlinx.coroutines.core)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import ksm.coroutines.interceptor.AwaitInterceptor
import ksm.coroutines.plugin.AwaitPlugin
import ksm.coroutines.plugin.CoroutinesPlugin
import ksm.plugin.plugin
import kotlin.coroutines.CoroutineContext

public val StateContext.scope: CoroutineScope get() {
return plugin(CoroutinesPlugin).coroutineScope(context = this)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ksm.coroutines

import kotlinx.coroutines.CoroutineScope
import ksm.StateController
import ksm.plugin.PluginController

public val StateController.scope: CoroutineScope
public val PluginController.scope: CoroutineScope
get() = context.scope
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package ksm.coroutines.plugin

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

public object AwaitPlugin : Plugin.Singleton<AwaitPlugin> {

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

public class CoroutinesPlugin(
private val scope: CoroutineScope
Expand Down
2 changes: 1 addition & 1 deletion navigation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ plugins {
version = libs.versions.ksm.get()

dependencies {
commonMainApi(projects.core)
commonMainApi(projects.plugin)
commonTestImplementation(kotlin("test"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import ksm.StateController
import ksm.annotation.LibraryConstructor
import ksm.annotation.LibraryApi
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)
@OptIn(LibraryApi::class)
@Composable
public fun rememberStateController(
wrapper: ComposeWrapper? = null,
Expand All @@ -24,7 +23,7 @@ public fun rememberStateController(
init = { ComposeSerializationStore() }
)
return remember(store) {
navigationStateController {
enableNavigation {
install(ComposePlugin(store, wrapper))
builder()
}.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import ksm.navigation.state.route.states
import ksm.navigation.state.data.receive
import ksm.navigation.state.name.named
import ksm.navigation.result.*
import ksm.viewmodel.viewModelStateController
import ksm.viewmodel.viewModelRuntime

private class MainViewModel {
val actions: Flow<Action> = emptyFlow()

val controller = viewModelStateController()
val controller = viewModelRuntime()

sealed interface Action {
data object RouteDetails : Action
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package ksm.kotlinx.serialization.plugin
import kotlinx.serialization.json.Json
import ksm.annotation.MutateContext
import ksm.context.StateContext
import ksm.context.install
import ksm.navigation.serialization.BaseSerializationStore
import ksm.navigation.serialization.plugin.BaseSerializationPlugin
import ksm.plugin.Plugin
import ksm.plugin.install

public class KotlinxSerializationPlugin(
private val json: Json = Json,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.types.update.abstracts.Update
import kotlinx.coroutines.flow.Flow
import ksm.StateController
import ksm.annotation.LibraryConstructor
import ksm.asStateController
import ksm.builder.StateControllerBuilder
import ksm.annotation.LibraryApi
import ksm.navigation.state.navigate
import ksm.navigation.ktgbotapi.plugin.TelegramBotApiPlugin
import ksm.navigation.navigationStateController
import ksm.navigation.navigationRuntime
import ksm.navigation.serialization.restore
import ksm.navigation.stack.hasNextContext
import ksm.navigation.stack.lastContext
Expand Down Expand Up @@ -39,13 +37,13 @@ public class TelegramBotStateMachine(
}
}

@OptIn(LibraryConstructor::class)
@OptIn(LibraryApi::class)
private fun createStateController(
key: TelegramPeerKey,
update: Update,
telegramBot: TelegramBot
): StateController {
return navigationStateController {
return navigationRuntime {
install(TelegramBotApiPlugin(key, update, telegramBot))
builder()
}.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package ksm.navigation.mdi
import app.meetacy.di.DI
import ksm.navigation.mdi.plugin.DIPlugin
import ksm.context.StateContext
import ksm.lifecycle.addLifecycleInterceptor
import ksm.lifecycle.interceptor.LifecycleInterceptor
import ksm.navigation.mdi.interceptor.DIInterceptor
import ksm.plugin.plugin

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package ksm.navigation.mdi
import app.meetacy.di.DI
import app.meetacy.di.builder.DIBuilder
import app.meetacy.di.builder.di
import ksm.builder.StateControllerBuilder
import ksm.annotation.LibraryApi
import ksm.navigation.mdi.plugin.DIPlugin
import ksm.plugin.PluginController
import ksm.plugin.install

public inline fun StateControllerBuilder.installDI(
@OptIn(LibraryApi::class)
public inline fun PluginController.Builder.installDI(
di: DI = di {},
checkDependencies: Boolean = true,
block: DIBuilder.() -> Unit = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import app.meetacy.di.DI
import app.meetacy.di.builder.DIBuilder
import app.meetacy.di.builder.di
import app.meetacy.di.dependency.Dependencies
import app.meetacy.di.dependency.DependencyKey
import ksm.annotation.MutateContext
import ksm.asStateController
import ksm.context.StateContext
import ksm.configuration.interceptor.ConfigurationInterceptor
import ksm.navigation.NavigationController
import ksm.plugin.Plugin
import ksm.configuration.interceptor.addConfigurationInterceptor
import ksm.lifecycle.addLifecycleInterceptor
import ksm.lifecycle.interceptor.LifecycleInterceptor
import ksm.navigation.mdi.addDIInterceptor
import ksm.navigation.mdi.interceptor.DIInterceptor
import ksm.navigation.mdi.interceptor.plus
import ksm.plugin.configuration.interceptor.ConfigurationInterceptor
import ksm.plugin.configuration.interceptor.addConfigurationInterceptor
import ksm.plugin.factory.asController
import ksm.plugin.factory.controllerFactory
import ksm.plugin.lifecycle.addLifecycleInterceptor
import ksm.plugin.lifecycle.interceptor.LifecycleInterceptor

public class DIPlugin(
private val root: DI = di { },
Expand Down Expand Up @@ -54,7 +57,13 @@ public class DIPlugin(
override fun intercept(context: StateContext, base: DI): DI {
var applied = base
applied += di(root, checkDependencies, perStateDI)
applied += di { val stateController by constant(context.asStateController()) }
applied += di {
val key = DependencyKey<NavigationController>(
type = context.controllerFactory.type,
name = "stateController"
)
register(key) { context.asController() }
}
return applied
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,7 @@
package ksm.navigation

import ksm.StateController
import ksm.pluginStateController
import ksm.annotation.LibraryConstructor
import ksm.builder.StateControllerBuilder
import ksm.context.StateContext
import ksm.navigation.state.name.plugin.StateNamePlugin
import ksm.navigation.state.parameters.plugin.StateParametersPlugin
import ksm.navigation.stack.installStackPlugin
import ksm.navigation.state.route.plugin.StateRoutePlugin
import ksm.plugin.PluginController

@LibraryConstructor
public inline fun navigationStateController(
context: StateContext = StateContext.Empty,
enableConfiguration: Boolean = true,
enableLifecycle: Boolean = true,
enableStateName: Boolean = true,
enableStateParameters: Boolean = true,
enableStack: Boolean = true,
enableFinishOnce: Boolean = true,
enableStateRoutePlugin: Boolean = true,
block: StateControllerBuilder.() -> Unit = {}
): StateController {
return pluginStateController(
context = context,
enableConfiguration = enableConfiguration,
enableLifecycle = enableLifecycle,
enableFinishOnce = enableFinishOnce
) {
// Installing built-in features
installStackPlugin(enableStack) {
if (enableStateName) install(StateNamePlugin)
if (enableStateParameters) install(StateParametersPlugin)
if (enableStateRoutePlugin) install(StateRoutePlugin)
block()
}
}
public interface NavigationController : PluginController {
public interface Builder : PluginController.Builder
}
Loading

0 comments on commit fdc42bc

Please sign in to comment.