Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#183 state remastering #185

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ plugins {
}

dependencies {
commonMainApi(projects.mvvmLivedata)

commonTestApi(libs.mokoTest)
commonTestApi(projects.mvvmTest)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.mvvm.state

fun <T1, E, T2, OT> mergeState(
firstState: ResourceState<T1, E>,
secondState: ResourceState<T2, E>,
function: (T1, T2) -> OT
): ResourceState<OT, E> = when {
(firstState is ResourceState.Loading || secondState is ResourceState.Loading) -> ResourceState.Loading()
(firstState is ResourceState.Failed) -> ResourceState.Failed(firstState.error)
(secondState is ResourceState.Failed) -> ResourceState.Failed(secondState.error)
(firstState is ResourceState.Empty || secondState is ResourceState.Empty) -> ResourceState.Empty()
(firstState is ResourceState.Success && secondState is ResourceState.Success) -> ResourceState.Success(
function(
firstState.data,
secondState.data
)
)
else -> ResourceState.Empty()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.mvvm.state

sealed class ResourceState<out TData, out TError> {
data class Success<out TData, out TError>(val data: TData) : ResourceState<TData, TError>()
data class Failed<out TData, out TError>(val error: TError) : ResourceState<TData, TError>()
class Loading<out TData, out TError> : ResourceState<TData, TError>()
class Empty<out TData, out TError> : ResourceState<TData, TError>()

fun isLoading(): Boolean = this is Loading
fun isSuccess(): Boolean = this is Success
fun isEmpty(): Boolean = this is Empty
fun isFailed(): Boolean = this is Failed

fun dataValue(): TData? = (this as? Success)?.data

fun errorValue(): TError? = (this as? Failed)?.error
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Copyright 2019 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.mvvm
package dev.icerock.moko.mvvm.state

fun <T, E> T.asState(): ResourceState<T, E> =
ResourceState.Success(this)
Expand Down
17 changes: 17 additions & 0 deletions mvvm-state-flow/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2019 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

plugins {
id("kmp-library-convention")
id("detekt-convention")
id("publication-convention")
}

dependencies {
commonMainApi(libs.coroutines)
commonMainApi(projects.mvvmStateCore)

commonTestApi(libs.mokoTest)
commonTestApi(projects.mvvmStateTest)
}
2 changes: 2 additions & 0 deletions mvvm-state-flow/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="dev.icerock.moko.mvvm.state.flow" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

@file:Suppress("MaximumLineLength", "MaxLineLength")

package dev.icerock.moko.mvvm.flow

import dev.icerock.moko.mvvm.state.ResourceState
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map

fun <TData, TError> Flow<ResourceState<TData, TError>>.isSuccessState(): Flow<Boolean> =
map { it.isSuccess() }

fun <TData, TError> Flow<ResourceState<TData, TError>>.isLoadingState(): Flow<Boolean> =
map { it.isLoading() }

fun <TData, TError> Flow<ResourceState<TData, TError>>.isErrorState(): Flow<Boolean> =
map { it.isFailed() }

fun <TData, TError> Flow<ResourceState<TData, TError>>.isEmptyState(): Flow<Boolean> =
map { it.isEmpty() }

inline fun <TData, TError, reified ST : ResourceState<TData, TError>, FL : Flow<ST>> List<FL>.isSuccessState(): Flow<Boolean> =
combine(this) { list ->
list.firstOrNull { it !is ResourceState.Success<*, *> } == null
}

inline fun <TData, TError, reified ST : ResourceState<TData, TError>, F : Flow<ST>> List<F>.isLoadingState(): Flow<Boolean> =
combine(this) { list ->
list.firstOrNull() { it is ResourceState.Loading<*, *> } != null
}

inline fun <TData, TError, reified ST : ResourceState<TData, TError>, F : Flow<ST>> List<F>.isErrorState(): Flow<Boolean> =
combine(this) { list ->
list.firstOrNull { it is ResourceState.Failed<*, *> } != null
}

inline fun <TData, TError, reified ST : ResourceState<TData, TError>, F : Flow<ST>> List<F>.isEmptyState(): Flow<Boolean> =
combine(this) { list ->
list.firstOrNull { it is ResourceState.Empty<*, *> } != null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.mvvm.flow

import dev.icerock.moko.mvvm.state.ResourceState
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.lastOrNull
import kotlinx.coroutines.flow.map

fun <TData, TError> Flow<ResourceState<TData, TError>>.data(): Flow<TData?> =
map { it.dataValue() }

fun <TData, TError> StateFlow<ResourceState<TData, TError>>.dataValue(): TData? =
value.dataValue()

suspend fun <TData, TError> Flow<ResourceState<TData, TError>>.dataValue(): TData? =
lastOrNull()?.dataValue()

fun <TData, TError> Flow<ResourceState<TData, TError>>.error(): Flow<TError?> =
map { it.errorValue() }

fun <TData, TError> StateFlow<ResourceState<TData, TError>>.errorValue(): TError? =
value.errorValue()

suspend fun <TData, TError> Flow<ResourceState<TData, TError>>.errorValue(): TError? =
lastOrNull()?.errorValue()

inline fun <TData, TError, reified ST : ResourceState<TData, TError>, LD : Flow<ST>> List<LD>.error(): Flow<TError?> =
combine(this) { list ->
@Suppress("UNCHECKED_CAST")
val errorItem = list.firstOrNull {
(it is ResourceState.Failed<*, *>)
} as? ResourceState.Failed<TData, TError>
errorItem?.error
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.mvvm.flow

import dev.icerock.moko.mvvm.state.ResourceState
import dev.icerock.moko.mvvm.state.mergeState
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine

fun <T1, E, T2, OT> Flow<ResourceState<T1, E>>.concatData(
flow: Flow<ResourceState<T2, E>>,
function: (T1, T2) -> OT
): Flow<ResourceState<OT, E>> =
combine(
this, flow
) { firstState, secondState ->
mergeState(firstState, secondState, function)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

@file:OptIn(ExperimentalCoroutinesApi::class)

package dev.icerock.moko.mvvm.flow

import dev.icerock.moko.mvvm.state.ResourceState
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map

fun <IT, E, OT> Flow<ResourceState<IT, E>>.dataTransform(
transform: Flow<IT>.() -> Flow<OT>
): Flow<ResourceState<OT, E>> = flatMapLatest { state ->
when (state) {
is ResourceState.Success -> transform(flowOf(state.data))
.map { ResourceState.Success(it) }
is ResourceState.Empty -> flowOf(ResourceState.Empty())
is ResourceState.Failed -> flowOf(ResourceState.Failed(state.error))
is ResourceState.Loading -> flowOf(ResourceState.Loading())
}
}

fun <T, IE, OE> Flow<ResourceState<T, IE>>.errorTransform(
transform: Flow<IE>.() -> Flow<OE>
): Flow<ResourceState<T, OE>> = flatMapLatest { state ->
when (state) {
is ResourceState.Success -> flowOf(ResourceState.Success(state.data))
is ResourceState.Loading -> flowOf(ResourceState.Loading())
is ResourceState.Empty -> flowOf(ResourceState.Empty())
is ResourceState.Failed -> transform.invoke(flowOf(state.error))
.map { ResourceState.Failed(it) }
}
}

fun <T, E> Flow<ResourceState<T, E>>.emptyAsError(
errorBuilder: () -> E
): Flow<ResourceState<T, E>> = map {
when (it) {
is ResourceState.Empty -> ResourceState.Failed(errorBuilder())
else -> it
}
}

fun <T, E> Flow<ResourceState<T, E>>.emptyAsData(
dataBuilder: () -> T
): Flow<ResourceState<T, E>> = map {
when (it) {
is ResourceState.Empty -> ResourceState.Success(dataBuilder())
else -> it
}
}

fun <T, E> Flow<ResourceState<T, E>>.emptyIf(
emptyPredicate: (T) -> Boolean
): Flow<ResourceState<T, E>> = map {
when {
it is ResourceState.Success && emptyPredicate(it.data) -> ResourceState.Empty()
else -> it
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.mvvm.flow


import dev.icerock.moko.mvvm.test.flow.FlowTest
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestResult
import kotlin.test.Test

@OptIn(ExperimentalCoroutinesApi::class)
class Test : FlowTest() {
@Test
override fun dataTransformTest(): TestResult {
return super.dataTransformTest()
}

@Test
override fun dataTransformMergeWithTest(): TestResult {
return super.dataTransformMergeWithTest()
}
}
17 changes: 17 additions & 0 deletions mvvm-state-livedata/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2019 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

plugins {
id("kmp-library-convention")
id("detekt-convention")
id("publication-convention")
}

dependencies {
commonMainApi(projects.mvvmLivedata)
commonMainApi(projects.mvvmStateCore)

commonTestApi(projects.mvvmTest)
commonTestApi(projects.mvvmStateTest)
}
2 changes: 2 additions & 0 deletions mvvm-state-livedata/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="dev.icerock.moko.mvvm.state.livedata" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.mvvm

import dev.icerock.moko.mvvm.state.ResourceState

@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("ResourceState", "dev.icerock.moko.mvvm.state"),
level = DeprecationLevel.WARNING
)
typealias ResourceState<TData, TError> = ResourceState<TData, TError>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2019 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.mvvm

import dev.icerock.moko.mvvm.state.ResourceState
import dev.icerock.moko.mvvm.state.asState
import dev.icerock.moko.mvvm.state.nullAsEmpty
import dev.icerock.moko.mvvm.state.nullAsLoading

@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("asState", "dev.icerock.moko.mvvm.state"),
level = DeprecationLevel.WARNING
)
fun <T, E> T.asState(): ResourceState<T, E> =
this.asState()

@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("asState", "dev.icerock.moko.mvvm.state"),
level = DeprecationLevel.WARNING
)
fun <T, E> T?.asState(whenNull: () -> ResourceState<T, E>): ResourceState<T, E> =
this?.asState() ?: whenNull()

@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("asState", "dev.icerock.moko.mvvm.state"),
level = DeprecationLevel.WARNING
)
fun <T, E> List<T>.asState(): ResourceState<List<T>, E> =
this.asState()

@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("asState", "dev.icerock.moko.mvvm.state"),
level = DeprecationLevel.WARNING
)
fun <T, E> List<T>?.asState(whenNull: () -> ResourceState<List<T>, E>): ResourceState<List<T>, E> =
this.asState(whenNull)

@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("nullAsEmpty", "dev.icerock.moko.mvvm.state"),
level = DeprecationLevel.WARNING
)
inline fun <reified T, reified E> ResourceState<T, E>?.nullAsEmpty(): ResourceState<T, E> =
this.nullAsEmpty()

@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("nullAsLoading", "dev.icerock.moko.mvvm.state"),
level = DeprecationLevel.WARNING
)
inline fun <reified T, reified E> ResourceState<T, E>?.nullAsLoading(): ResourceState<T, E> =
this.nullAsLoading()
Loading