Skip to content

Commit

Permalink
Nullable update
Browse files Browse the repository at this point in the history
  • Loading branch information
JekaK committed Apr 5, 2023
1 parent f4b37a7 commit 193478d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
26 changes: 22 additions & 4 deletions ReduxMVI/src/main/java/com/krykun/reduxmvi/ext/FlowExt.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.krykun.reduxmvi.ext

import com.krykun.reduxmvi.global.AppState
import com.krykun.reduxmvilib.R
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.*
Expand All @@ -20,7 +19,13 @@ inline fun <reified T> Flow<AppState>.getStateUpdates(bindingDispatcher: Corouti
.flowOn(bindingDispatcher)
}

fun <R> Flow<Any?>.toDedicatedType(): Flow<R?> {
fun <R> Flow<Any?>.toDedicatedTypeNullable(): Flow<R?> {
return this.map {
it as R
}
}

fun <R> Flow<Any>.toDedicatedType(): Flow<R> {
return this.map {
it as R
}
Expand All @@ -29,8 +34,21 @@ fun <R> Flow<Any?>.toDedicatedType(): Flow<R?> {
@OptIn(ExperimentalCoroutinesApi::class)
inline fun <reified T> Flow<AppState>.getStateUpdatesProperty(
bindingDispatcher: CoroutineDispatcher,
crossinline transform: suspend (value: T) -> Any?,
): Flow<Any?> {
crossinline transform: suspend (value: T) -> Any,
): Flow<Any> {

return this
.flatMapLatest {
it.findStateFlow<T>().takeWhenChanged(transform)
}
.flowOn(bindingDispatcher)
}

@OptIn(ExperimentalCoroutinesApi::class)
inline fun <reified T> Flow<AppState>.getStateUpdatesPropertyNullable(
bindingDispatcher: CoroutineDispatcher,
crossinline transform: suspend (value: T) -> Any,
): Flow<Any> {

return this
.flatMapLatest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package com.krykun.sample.presentation

data class MainState(
val isOpen: Boolean = false,
val counter: Int? = 0
val counter: Int = 0
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.krykun.reduxmvi.action.AddStateAction
import com.krykun.reduxmvi.ext.getStateUpdatesMapped
import com.krykun.reduxmvi.ext.getStateUpdatesProperty
import com.krykun.reduxmvi.ext.toDedicatedType
import com.krykun.reduxmvi.ext.toDedicatedTypeNullable
import com.krykun.reduxmvi.global.Action
import com.krykun.reduxmvi.global.AppState
import com.krykun.reduxmvi.global.Store
Expand Down Expand Up @@ -44,7 +45,7 @@ class MainViewModel(
viewModelScope.launch {
propertyProps()
.collect { value ->
counter.value = value ?: 0
counter.value = value
}
}
/* Collecting the `Flow<MainProps>` emitted by `mainProps()` and setting the value of
Expand Down

0 comments on commit 193478d

Please sign in to comment.