Skip to content

Commit

Permalink
Merge pull request #44 from stslex/dev
Browse files Browse the repository at this point in the history
refactor all stores
  • Loading branch information
stslex authored Nov 2, 2024
2 parents 4ddca64 + 8d482ad commit 7dc26ed
Show file tree
Hide file tree
Showing 89 changed files with 1,935 additions and 1,922 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.stslex.wizard.core.ui.mvi

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.stslex.wizard.core.core.AppDispatcher
import com.stslex.wizard.core.core.Logger
import com.stslex.wizard.core.core.coroutine.AppCoroutineScope
import com.stslex.wizard.core.core.coroutine.AppCoroutineScopeImpl
import com.stslex.wizard.core.core.coroutineExceptionHandler
import com.stslex.wizard.core.ui.mvi.Store.Action
import com.stslex.wizard.core.ui.mvi.Store.Event
import com.stslex.wizard.core.ui.mvi.Store.State
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

abstract class BaseStore<S : State, A : Action, E : Event>(
private val appDispatcher: AppDispatcher,
initialState: S
) : ViewModel(), Store<S, A, E> {

private val _event: MutableSharedFlow<E> = MutableSharedFlow()
override val event: SharedFlow<E> = _event.asSharedFlow()

private val _state: MutableStateFlow<S> = MutableStateFlow(initialState)
override val state: StateFlow<S> = _state.asStateFlow()

protected val scope: AppCoroutineScope = AppCoroutineScopeImpl(
scope = viewModelScope,
appDispatcher = appDispatcher
)

private var _lastAction: A? = null
protected val lastAction: A?
get() = _lastAction

override fun sendAction(action: A) {
if (lastAction != action && action !is Action.RepeatLast) {
_lastAction = action
}
process(action)
}

/** Process the action. This method should be overridden in the child class.*/
protected abstract fun process(action: A)

private fun exceptionHandler(
onError: suspend (cause: Throwable) -> Unit = {},
) = CoroutineExceptionHandler { _, throwable ->
Logger.e(throwable)
viewModelScope.launch(appDispatcher.default + coroutineExceptionHandler) {
onError(throwable)
}
}

/**
* Updates the state of the screen.
* @param update - function that updates the state
* */
protected fun updateState(update: (S) -> S) {
_state.update(update)
}

/**
* Sends an event to the screen. The event is sent on the default dispatcher of the AppDispatcher.
* @param event - event to be sent
* @see AppDispatcher
* */
protected fun sendEvent(event: E) {
viewModelScope.launch(appDispatcher.default) {
this@BaseStore._event.emit(event)
}
}

/**
* Launches a coroutine and catches exceptions. The coroutine is launched on the default dispatcher of the AppDispatcher.
* @param onError - error handler
* @param onSuccess - success handler
* @param action - action to be executed
* @return Job
* @see Job
* @see AppDispatcher
* */
protected fun <T> launch(
onError: suspend (Throwable) -> Unit = {},
onSuccess: suspend CoroutineScope.(T) -> Unit = {},
action: suspend CoroutineScope.() -> T,
): Job = scope.launch(
onError = onError,
onSuccess = onSuccess,
action = action
)

/**
* Launches a flow and collects it in the screenModelScope. The flow is collected on the default dispatcher. of the AppDispatcher.
* @param onError - error handler
* @param each - action for each element of the flow
* @return Job
* @see Flow
* @see Job
* @see AppDispatcher
* */
protected fun <T> Flow<T>.launch(
onError: suspend (cause: Throwable) -> Unit = {},
each: suspend (T) -> Unit
): Job = scope.launch(
flow = this,
onError = onError,
each = each,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.stslex.wizard.core.ui.mvi

import androidx.compose.material3.SnackbarDuration
import androidx.compose.runtime.Stable
import com.stslex.wizard.core.ui.components.SnackbarType

interface CommonEvents {

@Stable
sealed class Snackbar(
open val message: String,
open val duration: SnackbarDuration,
open val withDismissAction: Boolean,
val action: String,
) : CommonEvents {

@Stable
data class Error(
override val message: String,
override val duration: SnackbarDuration = SnackbarDuration.Short,
override val withDismissAction: Boolean = false,
) : Snackbar(
message = message,
action = SnackbarType.ERROR.label,
duration = duration,
withDismissAction = withDismissAction
)

@Stable
data class Success(
override val message: String,
override val duration: SnackbarDuration = SnackbarDuration.Short,
override val withDismissAction: Boolean = false,
) : Snackbar(
message = message,
action = SnackbarType.SUCCESS.label,
duration = duration,
withDismissAction = withDismissAction
)

@Stable
data class Info(
override val message: String,
override val duration: SnackbarDuration = SnackbarDuration.Short,
override val withDismissAction: Boolean = false,
) : Snackbar(
message = message,
action = SnackbarType.INFO.label,
duration = duration,
withDismissAction = withDismissAction
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.stslex.wizard.core.ui.mvi

fun interface Router<in E : StoreComponent.Navigation> {
fun interface Router<in E : Store.Action.Navigation> {
operator fun invoke(event: E)
}
137 changes: 21 additions & 116 deletions core/ui/src/commonMain/kotlin/com/stslex/wizard/core/ui/mvi/Store.kt
Original file line number Diff line number Diff line change
@@ -1,131 +1,36 @@
package com.stslex.wizard.core.ui.mvi

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.stslex.wizard.core.core.AppDispatcher
import com.stslex.wizard.core.core.Logger
import com.stslex.wizard.core.core.coroutine.AppCoroutineScope
import com.stslex.wizard.core.core.coroutine.AppCoroutineScopeImpl
import com.stslex.wizard.core.core.coroutineExceptionHandler
import com.stslex.wizard.core.ui.mvi.StoreComponent.Action
import com.stslex.wizard.core.ui.mvi.StoreComponent.Event
import com.stslex.wizard.core.ui.mvi.StoreComponent.Navigation
import com.stslex.wizard.core.ui.mvi.StoreComponent.State
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import com.stslex.wizard.core.ui.mvi.Store.Action
import com.stslex.wizard.core.ui.mvi.Store.Event
import com.stslex.wizard.core.ui.mvi.Store.State
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

abstract class Store<S : State, E : Event, A : Action, N : Navigation>(
private val router: Router<N>,
private val appDispatcher: AppDispatcher,
initialState: S
) : ViewModel(), StoreAbstraction<S, E, A> {
interface Store<out S : State, in A : Action, out E : Event> {

private val _event: MutableSharedFlow<E> = MutableSharedFlow()
override val event: SharedFlow<E> = _event.asSharedFlow()
/** Flow of the state of the screen. */
val state: StateFlow<S>

private val _state: MutableStateFlow<S> = MutableStateFlow(initialState)
override val state: StateFlow<S> = _state.asStateFlow()
/** Flow of events that are sent to the screen. */
val event: SharedFlow<E>

protected val scope: AppCoroutineScope = AppCoroutineScopeImpl(
scope = viewModelScope,
appDispatcher = appDispatcher
)
/**
* Sends an action to the store. Checks if the action is not the same as the last action.
* If the action is not the same as the last action, the last action is updated.
* The action is then processed.
* @param action - action to be sent
*/
fun sendAction(action: A)

private var _lastAction: A? = null
protected val lastAction: A?
get() = _lastAction
interface State

override fun sendAction(action: A) {
if (lastAction != action && action !is Action.RepeatLastAction) {
_lastAction = action
}
process(action)
}
interface Event

/** Process the action. This method should be overridden in the child class.*/
protected abstract fun process(action: A)
interface Action {

private fun exceptionHandler(
onError: suspend (cause: Throwable) -> Unit = {},
) = CoroutineExceptionHandler { _, throwable ->
Logger.e(throwable)
viewModelScope.launch(appDispatcher.default + coroutineExceptionHandler) {
onError(throwable)
}
}
interface RepeatLast : Action

/**
* Updates the state of the screen.
* @param update - function that updates the state
* */
protected fun updateState(update: (S) -> S) {
_state.update(update)
}

/**
* Sends an event to the screen. The event is sent on the default dispatcher of the AppDispatcher.
* @param event - event to be sent
* @see AppDispatcher
* */
protected fun sendEvent(event: E) {
viewModelScope.launch(appDispatcher.default) {
this@Store._event.emit(event)
}
interface Navigation : Action
}
}

/**
* Navigates to the specified screen. The router is called with the specified event.
* @param event - event to be passed to the router
* @see Router
* */
protected fun consumeNavigation(event: N) {
router(event)
}

/**
* Launches a coroutine and catches exceptions. The coroutine is launched on the default dispatcher of the AppDispatcher.
* @param onError - error handler
* @param onSuccess - success handler
* @param action - action to be executed
* @return Job
* @see Job
* @see AppDispatcher
* */
protected fun <T> launch(
onError: suspend (Throwable) -> Unit = {},
onSuccess: suspend CoroutineScope.(T) -> Unit = {},
action: suspend CoroutineScope.() -> T,
): Job = scope.launch(
onError = onError,
onSuccess = onSuccess,
action = action
)

/**
* Launches a flow and collects it in the screenModelScope. The flow is collected on the default dispatcher. of the AppDispatcher.
* @param onError - error handler
* @param each - action for each element of the flow
* @return Job
* @see Flow
* @see Job
* @see AppDispatcher
* */
protected fun <T> Flow<T>.launch(
onError: suspend (cause: Throwable) -> Unit = {},
each: suspend (T) -> Unit
): Job = scope.launch(
flow = this,
onError = onError,
each = each,
)
}

This file was deleted.

Loading

0 comments on commit 7dc26ed

Please sign in to comment.