-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
…y-compose interop
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package pro.respawn.flowmvi.compose.dsl | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import pro.respawn.flowmvi.compose.api.SubscriberLifecycle | ||
|
||
@Composable | ||
public fun ProvideSubscriberLifecycle( | ||
Check warning on line 8 in compose/src/commonMain/kotlin/pro/respawn/flowmvi/compose/dsl/CompositionLocals.kt Codacy Production / Codacy Static Code Analysiscompose/src/commonMain/kotlin/pro/respawn/flowmvi/compose/dsl/CompositionLocals.kt#L8
|
||
lifecycleOwner: SubscriberLifecycle, | ||
content: @Composable () -> Unit | ||
): Unit = CompositionLocalProvider( | ||
LocalSubscriberLifecycle provides lifecycleOwner, | ||
content = content, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package pro.respawn.flowmvi.compose.dsl | ||
|
||
import kotlinx.coroutines.coroutineScope | ||
import pro.respawn.flowmvi.compose.api.SubscriberLifecycle | ||
|
||
@PublishedApi | ||
internal val ImmediateLifecycle: SubscriberLifecycle = SubscriberLifecycle { _, block -> coroutineScope(block) } |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package pro.respawn.flowmvi.compose.dsl | ||
|
||
import androidx.compose.runtime.Composable | ||
import pro.respawn.flowmvi.compose.api.SubscriberLifecycleOwner | ||
import pro.respawn.flowmvi.compose.api.SubscriberLifecycle | ||
|
||
internal actual val PlatformLifecycle: SubscriberLifecycleOwner? @Composable get() = null | ||
internal actual val PlatformLifecycle: SubscriberLifecycle? @Composable get() = null |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package pro.respawn.flowmvi.compose.dsl | ||
|
||
import androidx.compose.runtime.Composable | ||
import pro.respawn.flowmvi.compose.api.SubscriberLifecycleOwner | ||
import pro.respawn.flowmvi.compose.api.SubscriberLifecycle | ||
|
||
internal actual val PlatformLifecycle: SubscriberLifecycleOwner? @Composable get() = null | ||
internal actual val PlatformLifecycle: SubscriberLifecycle? @Composable get() = null |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package pro.respawn.flowmvi.compose.dsl | ||
|
||
import androidx.compose.runtime.Composable | ||
import pro.respawn.flowmvi.compose.api.SubscriberLifecycleOwner | ||
import pro.respawn.flowmvi.compose.api.SubscriberLifecycle | ||
|
||
internal actual val PlatformLifecycle: SubscriberLifecycleOwner? @Composable get() = null | ||
internal actual val PlatformLifecycle: SubscriberLifecycle? @Composable get() = null |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package pro.respawn.flowmvi.compose.dsl | ||
|
||
import androidx.compose.runtime.Composable | ||
import pro.respawn.flowmvi.compose.api.SubscriberLifecycleOwner | ||
import pro.respawn.flowmvi.compose.api.SubscriberLifecycle | ||
|
||
internal actual val PlatformLifecycle: SubscriberLifecycleOwner? @Composable get() = null | ||
internal actual val PlatformLifecycle: SubscriberLifecycle? @Composable get() = null |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
plugins { | ||
id(libs.plugins.kotlinMultiplatform.id) | ||
id(libs.plugins.androidLibrary.id) | ||
alias(libs.plugins.jetbrainsCompose) | ||
id("maven-publish") | ||
signing | ||
} | ||
|
||
android { | ||
configureAndroidLibrary(this) | ||
namespace = "${Config.namespace}.decompose.compose" | ||
|
||
buildFeatures { | ||
compose = true | ||
} | ||
} | ||
|
||
kotlin { | ||
configureMultiplatform( | ||
ext = this, | ||
tvOs = false, | ||
watchOs = false, | ||
linux = false, | ||
windows = false, | ||
) | ||
sourceSets { | ||
androidMain.dependencies { | ||
implementation(libs.compose.foundation) | ||
implementation(libs.compose.preview) | ||
implementation(libs.compose.lifecycle.runtime) | ||
api(projects.android) | ||
} | ||
commonMain.dependencies { | ||
implementation(compose.runtime) | ||
implementation(compose.foundation) | ||
api(projects.core) | ||
api(projects.compose) | ||
|
||
api(libs.essenty.lifecycle) | ||
api(libs.essenty.lifecycle.coroutines) | ||
api(libs.essenty.instancekeeper) | ||
} | ||
jvmMain.dependencies { | ||
implementation(compose.desktop.common) | ||
implementation(libs.compose.lifecycle.runtime) | ||
} | ||
} | ||
} | ||
|
||
publishMultiplatform() | ||
|
||
dependencies { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package pro.respawn.flowmvi.decompose.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import com.arkivanov.essenty.lifecycle.LifecycleOwner | ||
import pro.respawn.flowmvi.compose.dsl.LocalSubscriberLifecycle | ||
|
||
@Composable | ||
public fun ProvideSubscriberLifecycle( | ||
Check warning on line 9 in essenty/essenty-compose/src/commonMain/kotlin/pro/respawn/flowmvi/decompose/compose/CompositionLocals.kt Codacy Production / Codacy Static Code Analysisessenty/essenty-compose/src/commonMain/kotlin/pro/respawn/flowmvi/decompose/compose/CompositionLocals.kt#L9
|
||
owner: LifecycleOwner, | ||
content: @Composable () -> Unit | ||
): Unit = CompositionLocalProvider( | ||
LocalSubscriberLifecycle provides owner.asSubscriberLifecycle, | ||
content = content, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package pro.respawn.flowmvi.decompose.compose | ||
|
||
import com.arkivanov.essenty.lifecycle.Lifecycle | ||
import com.arkivanov.essenty.lifecycle.LifecycleOwner | ||
import com.arkivanov.essenty.lifecycle.coroutines.repeatOnLifecycle | ||
import pro.respawn.flowmvi.compose.api.SubscriberLifecycle | ||
import pro.respawn.flowmvi.compose.api.SubscriptionMode | ||
|
||
public val LifecycleOwner.asSubscriberLifecycle: SubscriberLifecycle | ||
Check warning on line 9 in essenty/essenty-compose/src/commonMain/kotlin/pro/respawn/flowmvi/decompose/compose/Lifecycle.kt Codacy Production / Codacy Static Code Analysisessenty/essenty-compose/src/commonMain/kotlin/pro/respawn/flowmvi/decompose/compose/Lifecycle.kt#L9
|
||
get() = SubscriberLifecycle { mode, block -> repeatOnLifecycle(mode.asEssentyLifecycle, block = block) } | ||
|
||
public val SubscriptionMode.asEssentyLifecycle: Lifecycle.State | ||
Check warning on line 12 in essenty/essenty-compose/src/commonMain/kotlin/pro/respawn/flowmvi/decompose/compose/Lifecycle.kt Codacy Production / Codacy Static Code Analysisessenty/essenty-compose/src/commonMain/kotlin/pro/respawn/flowmvi/decompose/compose/Lifecycle.kt#L12
|
||
get() = when (this) { | ||
SubscriptionMode.Immediate -> Lifecycle.State.CREATED | ||
SubscriptionMode.Started -> Lifecycle.State.STARTED | ||
SubscriptionMode.Visible -> Lifecycle.State.RESUMED | ||
} | ||
public val Lifecycle.State.asSubscriptionMode: SubscriptionMode | ||
get() = when (this) { | ||
Lifecycle.State.CREATED -> SubscriptionMode.Immediate | ||
Lifecycle.State.STARTED -> SubscriptionMode.Started | ||
Lifecycle.State.RESUMED -> SubscriptionMode.Visible | ||
Lifecycle.State.DESTROYED, | ||
Lifecycle.State.INITIALIZED -> error("Essenty does not provide support for using $name as subscriber lifecycle") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package pro.respawn.flowmvi.decompose.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.State | ||
import com.arkivanov.essenty.lifecycle.Lifecycle | ||
import com.arkivanov.essenty.lifecycle.LifecycleOwner | ||
import kotlinx.coroutines.CoroutineScope | ||
import pro.respawn.flowmvi.api.FlowMVIDSL | ||
import pro.respawn.flowmvi.api.ImmutableStore | ||
import pro.respawn.flowmvi.api.MVIAction | ||
import pro.respawn.flowmvi.api.MVIIntent | ||
import pro.respawn.flowmvi.api.MVIState | ||
import pro.respawn.flowmvi.compose.dsl.subscribe | ||
import pro.respawn.flowmvi.dsl.subscribe | ||
|
||
/** | ||
* A function to subscribe to the store that follows the system lifecycle. | ||
* | ||
* * This function will assign the store a new subscriber when invoked, then populate the returned [State] with new states. | ||
* * Provided [consume] parameter will be used to consume actions that come from the store. | ||
* * Store's subscribers will **not** wait until the store is launched when they subscribe to the store. | ||
* Such subscribers will not receive state updates or actions. Don't forget to launch the store. | ||
* | ||
* @param lifecycleState the minimum lifecycle state that should be reached in order to subscribe to the store, | ||
* upon leaving that state, the function will unsubscribe. | ||
* @param consume a lambda to consume actions with. | ||
* @return the [State] that contains the current state. | ||
* @see ImmutableStore.subscribe | ||
* @see subscribe | ||
*/ | ||
@Suppress("ComposableParametersOrdering") | ||
@Composable | ||
@FlowMVIDSL | ||
public inline fun <S : MVIState, I : MVIIntent, A : MVIAction> ImmutableStore<S, I, A>.subscribe( | ||
lifecycleOwner: LifecycleOwner, | ||
lifecycleState: Lifecycle.State = Lifecycle.State.STARTED, | ||
noinline consume: suspend CoroutineScope.(action: A) -> Unit, | ||
): State<S> = subscribe(lifecycleState.asSubscriptionMode, lifecycleOwner.asSubscriberLifecycle, consume) | ||
|
||
/** | ||
* A function to subscribe to the store that follows the system lifecycle. | ||
* | ||
* * This function will not collect [MVIAction]s. | ||
* * This function will assign the store a new subscriber when invoked, then populate the returned [State] with new states. | ||
* * Store's subscribers will **not** wait until the store is launched when they subscribe to the store. | ||
* Such subscribers will not receive state updates or actions. Don't forget to launch the store. | ||
* @param lifecycleState the minimum lifecycle state that should be reached in order to subscribe to the store, | ||
* upon leaving that state, the function will unsubscribe. | ||
* @return the [State] that contains the current state. | ||
* @see ImmutableStore.subscribe | ||
* @see subscribe | ||
*/ | ||
@Composable | ||
@FlowMVIDSL | ||
public inline fun <S : MVIState, I : MVIIntent, A : MVIAction> ImmutableStore<S, I, A>.subscribe( | ||
lifecycleOwner: LifecycleOwner, | ||
lifecycleState: Lifecycle.State = Lifecycle.State.CREATED, | ||
): State<S> = subscribe(lifecycleState.asSubscriptionMode, lifecycleOwner.asSubscriberLifecycle) |