-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
aee5e74
commit 23508e6
Showing
11 changed files
with
219 additions
and
95 deletions.
There are no files selected for viewing
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
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
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
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
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
51 changes: 0 additions & 51 deletions
51
...tivestate-compose/src/androidMain/kotlin/com/ensody/reactivestate/compose/ViewModelExt.kt
This file was deleted.
Oops, something went wrong.
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
90 changes: 90 additions & 0 deletions
90
...tivestate-compose/src/composeMain/kotlin/com/ensody/reactivestate/compose/ViewModelExt.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,90 @@ | ||
package com.ensody.reactivestate.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.mutableStateMapOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.runtime.snapshots.SnapshotStateMap | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelStoreOwner | ||
import androidx.lifecycle.viewModelScope | ||
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import com.ensody.reactivestate.ErrorEvents | ||
import com.ensody.reactivestate.ExperimentalReactiveStateApi | ||
import com.ensody.reactivestate.InMemoryStateFlowStore | ||
import com.ensody.reactivestate.ReactiveState | ||
import com.ensody.reactivestate.ReactiveStateContext | ||
import com.ensody.reactivestate.ReactiveViewModel | ||
import com.ensody.reactivestate.ReactiveViewModelContext | ||
import com.ensody.reactivestate.handleEvents | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
/** | ||
* Creates a multiplatform [ReactiveState] ViewModel and observes its [ReactiveState.eventNotifier]. | ||
* | ||
* The [provider] should instantiate the object directly. | ||
*/ | ||
@ExperimentalReactiveStateApi | ||
@Suppress("UNCHECKED_CAST") | ||
@Composable | ||
public inline fun <reified VM : ReactiveViewModel> ErrorEvents.reactiveViewModel( | ||
key: String? = null, | ||
crossinline observeLoadingEffect: @Composable (viewModel: VM) -> Unit, | ||
crossinline provider: ReactiveViewModelContext.() -> VM, | ||
): VM = | ||
reactiveState(key = key, observeLoadingEffect = observeLoadingEffect) { | ||
ReactiveViewModelContext(this).provider() | ||
} | ||
|
||
/** | ||
* Creates a multiplatform [ReactiveState] ViewModel and observes its [ReactiveState.eventNotifier]. | ||
* | ||
* The [provider] should instantiate the object directly. | ||
*/ | ||
@ExperimentalReactiveStateApi | ||
@Suppress("UNCHECKED_CAST") | ||
@Composable | ||
public inline fun <reified E : ErrorEvents, reified VM : ReactiveState<E>> E.reactiveState( | ||
key: String? = null, | ||
crossinline observeLoadingEffect: @Composable (viewModel: VM) -> Unit, | ||
crossinline provider: ReactiveStateContext.() -> VM, | ||
): VM { | ||
val viewModel = onViewModel(key = key, provider = provider) | ||
observeLoadingEffect(viewModel) | ||
LaunchedEffect(this, viewModel.eventNotifier) { | ||
viewModel.eventNotifier.handleEvents(this@reactiveState) | ||
} | ||
return viewModel | ||
} | ||
|
||
/** | ||
* Creates an object living on a wrapper [ViewModel]. This allows for building more flexible (e.g. nestable) ViewModels. | ||
* | ||
* The [provider] should instantiate the object directly. | ||
* | ||
* @see [reactiveState] if you want to instantiate a multiplatform [ReactiveState] ViewModel directly. | ||
*/ | ||
@ExperimentalReactiveStateApi | ||
@Composable | ||
public inline fun <reified T : Any?> onViewModel( | ||
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) { | ||
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner" | ||
}, | ||
key: String? = null, | ||
crossinline provider: ReactiveStateContext.() -> T, | ||
): T { | ||
// TODO: Use qualifiedName once JS supports it | ||
val fullKey = (key ?: "") + ":onViewModel:${T::class.simpleName}" | ||
val storage = rememberSaveable<SnapshotStateMap<String, Any?>> { mutableStateMapOf() } | ||
val stateFlowStore = remember { InMemoryStateFlowStore(storage) } | ||
return viewModel(viewModelStoreOwner = viewModelStoreOwner, key = fullKey) { | ||
WrapperViewModel { scope -> ReactiveStateContext(scope, stateFlowStore).provider() } | ||
}.value | ||
} | ||
|
||
/** A wrapper ViewModel used to hold an arbitrary [value]. */ | ||
public class WrapperViewModel<T : Any?>(provider: (CoroutineScope) -> T) : ViewModel() { | ||
public val value: T = provider(viewModelScope) | ||
} |
37 changes: 37 additions & 0 deletions
37
reactivestate-core/src/commonMain/kotlin/com/ensody/reactivestate/OneTimeEvent.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,37 @@ | ||
package com.ensody.reactivestate | ||
|
||
/** | ||
* Simple synchronous one-time event. Once [trigger] is called, subsequent [observe] calls are triggered immediately. | ||
* | ||
* This is not a stream of multiple events. That's what `Channel` and `Flow` would be used for, instead. | ||
* This kind of event can be activated exactly once and then it stays active forever. | ||
*/ | ||
public interface OneTimeEvent<T> { | ||
public fun observe(block: T.() -> Unit) | ||
public fun unobserve(block: T.() -> Unit) | ||
public fun trigger(source: T) | ||
} | ||
|
||
public class DefaultOneTimeEvent<T> : OneTimeEvent<T> { | ||
private var source: T? = null | ||
private val observers: MutableList<T.() -> Unit> = mutableListOf() | ||
|
||
override fun observe(block: T.() -> Unit) { | ||
source?.block() ?: observers.add(block) | ||
} | ||
|
||
override fun unobserve(block: T.() -> Unit) { | ||
observers.remove(block) | ||
} | ||
|
||
override fun trigger(source: T) { | ||
if (this.source == null) { | ||
this.source = source | ||
} | ||
|
||
observers.forEach { | ||
it.invoke(source) | ||
} | ||
observers.clear() | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
reactivestate-core/src/commonMain/kotlin/com/ensody/reactivestate/ReactiveStateContext.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,21 @@ | ||
package com.ensody.reactivestate | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
|
||
/** | ||
* Contains all values which are part of the | ||
*/ | ||
@ExperimentalReactiveStateApi | ||
public interface ReactiveStateContext { | ||
public val scope: CoroutineScope | ||
public val stateFlowStore: StateFlowStore | ||
} | ||
|
||
@ExperimentalReactiveStateApi | ||
public fun ReactiveStateContext(scope: CoroutineScope, stateFlowStore: StateFlowStore): ReactiveStateContext = | ||
DefaultReactiveStateContext(scope, stateFlowStore) | ||
|
||
private class DefaultReactiveStateContext( | ||
override val scope: CoroutineScope, | ||
override val stateFlowStore: StateFlowStore, | ||
) : ReactiveStateContext |
Oops, something went wrong.