Skip to content

Commit

Permalink
Merge pull request #12 from adidas/enhancement/remove-MviState-in-fav…
Browse files Browse the repository at this point in the history
…or-of-State

Enhancement/remove mvi state in favor of state
  • Loading branch information
sherviiin authored Aug 4, 2022
2 parents e5153dd + 8ebe37c commit ac17cc2
Show file tree
Hide file tree
Showing 26 changed files with 101 additions and 164 deletions.
47 changes: 0 additions & 47 deletions mvi-declarative/build.gradle.kts

This file was deleted.

1 change: 0 additions & 1 deletion mvi-declarative/gradle.properties

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion mvi/src/main/kotlin/com/adidas/mvi/IntentExecutor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package com.adidas.mvi
import com.adidas.mvi.transform.StateTransform
import kotlinx.coroutines.flow.Flow

public fun interface IntentExecutor<TIntent : Intent, TState : State> {
public fun interface IntentExecutor<TIntent : Intent, TState : LoggableState> {
public fun executeIntent(intent: TIntent, jobTerminator: JobTerminator<TIntent>): Flow<StateTransform<TState>>
}
3 changes: 3 additions & 0 deletions mvi/src/main/kotlin/com/adidas/mvi/LoggableState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.adidas.mvi

public interface LoggableState : Loggable
2 changes: 1 addition & 1 deletion mvi/src/main/kotlin/com/adidas/mvi/Reducer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Reducer<TIntent, TState> @Deprecated("Use the other constructor, th
private val defaultDispatcher: CoroutineDispatcher = Dispatchers.Default,
private val intentExecutor: IntentExecutor<TIntent, TState>
) where TIntent : Intent,
TState : State {
TState : LoggableState {
@Suppress("DEPRECATION")
public constructor(
coroutineScope: CoroutineScope,
Expand Down
4 changes: 3 additions & 1 deletion mvi/src/main/kotlin/com/adidas/mvi/State.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.adidas.mvi

public interface State : Loggable
import com.adidas.mvi.sideeffects.SideEffects

public data class State<out TState, TSideEffect>(val view: TState, val sideEffects: SideEffects<TSideEffect>) : LoggableState
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package com.adidas.mvi.declarative
package com.adidas.mvi.reducer

import com.adidas.mvi.Intent
import com.adidas.mvi.LoggableState
import com.adidas.mvi.Logger
import com.adidas.mvi.Reducer
import com.adidas.mvi.SimplifiedIntentExecutor
import com.adidas.mvi.State
import com.adidas.mvi.actions.Actions
import com.adidas.mvi.sideeffects.SideEffects
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers

public fun <TIntent : Intent, TInnerState : State, TAction> Reducer(
public fun <TIntent : Intent, TInnerState : LoggableState, TAction> Reducer(
coroutineScope: CoroutineScope,
initialInnerState: TInnerState,
intentExecutor: SimplifiedIntentExecutor<TIntent, MviState<TInnerState, TAction>>,
intentExecutor: SimplifiedIntentExecutor<TIntent, State<TInnerState, TAction>>,
logger: Logger? = null,
defaultDispatcher: CoroutineDispatcher = Dispatchers.Default
): Reducer<TIntent, MviState<TInnerState, TAction>> {
): Reducer<TIntent, State<TInnerState, TAction>> {
return Reducer(
coroutineScope = coroutineScope,
initialState = MviState(initialInnerState, Actions()),
initialState = State(initialInnerState, SideEffects()),
intentExecutor = intentExecutor,
logger = logger,
defaultDispatcher = defaultDispatcher
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.adidas.mvi.requirements

import com.adidas.mvi.State
import com.adidas.mvi.LoggableState

public inline fun <TState : State, reified TRequiredState : TState> requireAndReduceState(
public inline fun <TState : LoggableState, reified TRequiredState : TState> requireAndReduceState(
state: TState,
noinline reduce: (TRequiredState) -> TState
): TState {
return StateReduceRequirement(TRequiredState::class, reduce).reduce(state)
}

public inline fun <TState : State, reified TRequiredState : TState> requireState(
public inline fun <TState : LoggableState, reified TRequiredState : TState> requireState(
noinline reduce: (TRequiredState) -> TState
): ReduceRequirement<TState> {
return StateReduceRequirement(TRequiredState::class, reduce)
}

public infix fun <TState : State> ReduceRequirement<TState>.or(another: ReduceRequirement<TState>): ReduceRequirement<TState> {
public infix fun <TState : LoggableState> ReduceRequirement<TState>.or(another: ReduceRequirement<TState>): ReduceRequirement<TState> {
return DoubleReduceRequirement(this, another)
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.adidas.mvi.requirements

import com.adidas.mvi.State
import com.adidas.mvi.LoggableState
import com.adidas.mvi.StateRequiredNotFulfilledException
import kotlin.reflect.KClass
import kotlin.reflect.cast

public class StateReduceRequirement<TState, TRequiredState>(
private val expectedState: KClass<out TRequiredState>,
private val reduceFunction: (TRequiredState) -> TState
) : ReduceRequirement<TState> where TState : State, TRequiredState : TState {
) : ReduceRequirement<TState> where TState : LoggableState, TRequiredState : TState {

public override fun reduce(state: TState): TState {
if (expectedState.isInstance(state)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.adidas.mvi.declarative.transform
package com.adidas.mvi.transform

import com.adidas.mvi.declarative.MviState
import com.adidas.mvi.State
import com.adidas.mvi.sideeffects.SideEffects
import com.adidas.mvi.transform.StateTransform

public abstract class SideEffectTransform<TState, TSideEffect> :
StateTransform<MviState<TState, TSideEffect>> {
StateTransform<State<TState, TSideEffect>> {

protected abstract fun mutate(sideEffects: SideEffects<TSideEffect>): SideEffects<TSideEffect>

final override fun reduce(currentState: MviState<TState, TSideEffect>): MviState<TState, TSideEffect> {
final override fun reduce(currentState: State<TState, TSideEffect>): State<TState, TSideEffect> {
val sideEffects = currentState.sideEffects
return currentState.copy(sideEffects = mutate(sideEffects))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.adidas.mvi.declarative.transform.product
package com.adidas.mvi.product

import com.adidas.mvi.declarative.transform.SideEffectTransform
import com.adidas.mvi.sideeffects.SideEffects
import com.adidas.mvi.transform.SideEffectTransform

class FakeProductSideEffectTransform(private val sideEffect: ProductSideEffect) :
SideEffectTransform<ProductState, ProductSideEffect>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.adidas.mvi.product

import com.adidas.mvi.State
import com.adidas.mvi.transform.StateTransform

class FakeProductStateTransform(private val state: State<ProductState, ProductSideEffect>) :
StateTransform<ProductState> {

override fun reduce(currentState: ProductState): ProductState {
return state.view
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.adidas.mvi.declarative.transform.product
package com.adidas.mvi.product

sealed class ProductSideEffect {
object NavigateToProductDetailsSideEffect : ProductSideEffect()
Expand Down
8 changes: 8 additions & 0 deletions mvi/src/test/kotlin/com/adidas/mvi/product/ProductState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.adidas.mvi.product

import com.adidas.mvi.LoggableState

sealed class ProductState : LoggableState {
object Loading : ProductState()
object Loaded : ProductState()
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.adidas.mvi.declarative.transform
package com.adidas.mvi.reducer

import com.adidas.mvi.Intent
import com.adidas.mvi.declarative.MviState
import com.adidas.mvi.declarative.Reducer
import com.adidas.mvi.declarative.transform.product.ProductState
import com.adidas.mvi.transform.Transform
import com.adidas.mvi.State
import com.adidas.mvi.product.ProductState
import com.adidas.mvi.transform.StateTransform
import io.kotest.core.spec.style.ShouldSpec
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.flow.emptyFlow
Expand All @@ -16,12 +15,12 @@ class ReducerExtensionsTest : ShouldSpec({
coroutineScope = TestScope(),
initialInnerState = ProductState.Loading,
intentExecutor = { _: Intent ->
emptyFlow<Transform<MviState<ProductState, Unit>>>()
emptyFlow<StateTransform<State<ProductState, Unit>>>()
}
)

should("The initial inner state should be Loading") {
reducer.state.value.state shouldBe ProductState.Loading
reducer.state.value.view shouldBe ProductState.Loading
}
}
})
4 changes: 2 additions & 2 deletions mvi/src/test/kotlin/com/adidas/mvi/reducer/TestState.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.adidas.mvi.reducer

import com.adidas.mvi.State
import com.adidas.mvi.LoggableState

internal sealed class TestState : State {
internal sealed class TestState : LoggableState {
object InitialState : TestState()

object StateFromTransform1 : TestState()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.adidas.mvi.requirements

import com.adidas.mvi.State
import com.adidas.mvi.LoggableState
import com.adidas.mvi.StateRequiredNotFulfilledException
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeInstanceOf

private sealed class TestState : State {
private sealed class TestState : LoggableState {
object State1 : TestState()
object State2 : TestState()
object State3 : TestState()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.adidas.mvi.transform

import com.adidas.mvi.State
import com.adidas.mvi.product.FakeProductSideEffectTransform
import com.adidas.mvi.product.ProductSideEffect
import com.adidas.mvi.product.ProductState
import com.adidas.mvi.sideeffects.SideEffects
import io.kotest.core.spec.style.ShouldSpec
import io.kotest.matchers.collections.shouldContainExactly

class SideEffectTransformTest : ShouldSpec({

context("SideEffectTransform") {
val sideEffect = ProductSideEffect.NavigateToProductDetailsSideEffect

should("use mutate() function for reducing SideEffects") {
val state = State(ProductState.Loading, SideEffects<ProductSideEffect>())
FakeProductSideEffectTransform(sideEffect).reduce(state).sideEffects shouldContainExactly
listOf<ProductSideEffect>(sideEffect)
}
}
})
Loading

0 comments on commit ac17cc2

Please sign in to comment.