Skip to content

Commit

Permalink
Code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
JekaK committed Mar 24, 2023
1 parent 6d4cd0a commit 77e9884
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
15 changes: 4 additions & 11 deletions sample/src/main/java/com/krykun/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
Expand All @@ -19,6 +16,7 @@ import com.krykun.sample.presentation.MainViewModel
import com.krykun.sample.theme.ReduxMVITheme
import com.krykun.sample.ui.view.AddButton
import com.krykun.sample.ui.view.CounterView
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.viewModel

Expand All @@ -35,20 +33,15 @@ class MainActivity : ComponentActivity() {
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
val props = viewModel.mainProps().collectAsState(initial = MainProps())
val propertyPropsFlow = viewModel.propertyProps()

val counter by propertyPropsFlow.collectAsState(0)

Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
CounterView(counter)
CounterView(viewModel.counter.value)
Spacer(modifier = Modifier.height(20.dp))
AddButton {
props.value.addCounterAction()
viewModel.props.value.addCounterAction()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.krykun.sample.presentation

import android.util.Log
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.krykun.reduxmvi.action.AddStateAction
import com.krykun.reduxmvi.ext.getStateUpdatesMapped
import com.krykun.reduxmvi.ext.getStateUpdatesProperty
Expand All @@ -10,24 +11,54 @@ import com.krykun.reduxmvi.global.AppState
import com.krykun.reduxmvi.global.Store
import com.krykun.sample.action.AddCounterAction
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.launch

/**
* `getStateUpdatesMapped` takes a `State` and returns a `Flow<Props>`
*
* The `getStateUpdatesMapped` function takes a `State` and returns a `Flow<Props>`. The `State` is
* mapped to a `Props` object. The `Props` object is then emitted as a `Flow<Props>`
*/
class MainViewModel(
private val store: Store<Action, AppState>,
private val bindingDispatcher: CoroutineDispatcher,
) : ViewModel() {

var counter = mutableStateOf(0)
val props = mutableStateOf(MainProps())

/* Creating a new instance of the MainState class and adding it to the store. */
init {
store.dispatch(AddStateAction(MainState()))
viewModelScope.launch {
propertyProps()
.collect { value ->
counter.value = value
}
}
viewModelScope.launch {
mainProps().collect {
props.value = it
}
}
}

fun mainProps() = store.stateFlow()
/**
* `mainProps` is a function that returns a `Flow` of `MainProps` that is updated whenever the
* `MainState` changes
*/
private fun mainProps() = store.stateFlow()
.getStateUpdatesMapped<MainState, MainProps>(bindingDispatcher) {
it.toProps {
store.dispatch(AddCounterAction())
}
}

fun propertyProps() = store.stateFlow()
/**
* `propertyProps()` returns a `Property<Int>` that emits the current value of `MainState.counter`
* whenever it changes
*/
private fun propertyProps() = store.stateFlow()
.getStateUpdatesProperty<MainState, Int>(bindingDispatcher) {
it.counter
}
Expand Down

0 comments on commit 77e9884

Please sign in to comment.