Skip to content

Commit

Permalink
icerockdev#183 remove dublicated class
Browse files Browse the repository at this point in the history
  • Loading branch information
mdubkov committed Jan 11, 2023
1 parent 1eedabc commit 9e9b65b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 127 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,53 @@

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> =
ResourceState.Success(this)
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()
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> = if (this.isEmpty()) {
ResourceState.Empty()
} else {
ResourceState.Success(this)
}
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()
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 ?: ResourceState.Empty()
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 ?: ResourceState.Loading()
this.nullAsLoading()
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,69 @@

package dev.icerock.moko.mvvm.livedata

import dev.icerock.moko.mvvm.ResourceState
import dev.icerock.moko.mvvm.state.ResourceState
import dev.icerock.moko.mvvm.state.livedata.isEmptyState
import dev.icerock.moko.mvvm.state.livedata.isErrorState
import dev.icerock.moko.mvvm.state.livedata.isLoadingState
import dev.icerock.moko.mvvm.state.livedata.isSuccessState


@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("isSuccessState", "dev.icerock.moko.mvvm.state.livedata"),
level = DeprecationLevel.WARNING
)
fun <T, E> LiveData<ResourceState<T, E>>.isSuccessState(): LiveData<Boolean> = map { it.isSuccess() }
fun <T, E> LiveData<ResourceState<T, E>>.isSuccessState(): LiveData<Boolean> = this.isSuccessState()

@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("isLoadingState", "dev.icerock.moko.mvvm.state.livedata"),
level = DeprecationLevel.WARNING
)
fun <T, E> LiveData<ResourceState<T, E>>.isLoadingState(): LiveData<Boolean> = map { it.isLoading() }
fun <T, E> LiveData<ResourceState<T, E>>.isLoadingState(): LiveData<Boolean> = this.isLoadingState()

@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("isErrorState", "dev.icerock.moko.mvvm.state.livedata"),
level = DeprecationLevel.WARNING
)
fun <T, E> LiveData<ResourceState<T, E>>.isErrorState(): LiveData<Boolean> = map { it.isFailed() }
fun <T, E> LiveData<ResourceState<T, E>>.isErrorState(): LiveData<Boolean> = this.isErrorState()

@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("isEmptyState", "dev.icerock.moko.mvvm.state.livedata"),
level = DeprecationLevel.WARNING
)
fun <T, E> LiveData<ResourceState<T, E>>.isEmptyState(): LiveData<Boolean> = map { it.isEmpty() }
fun <T, E> LiveData<ResourceState<T, E>>.isEmptyState(): LiveData<Boolean> = this.isEmptyState()

@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("isSuccessState", "dev.icerock.moko.mvvm.state.livedata"),
level = DeprecationLevel.WARNING
)
fun <T, E, ST : ResourceState<T, E>, LD : LiveData<out ST>> List<LD>.isSuccessState(): LiveData<Boolean> =
MediatorLiveData(false)
.composition(this) { list ->
list.firstOrNull { it !is ResourceState.Success<*, *> } == null
}
this.isSuccessState()

@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("isLoadingState", "dev.icerock.moko.mvvm.state.livedata"),
level = DeprecationLevel.WARNING
)
fun <T, E, ST : ResourceState<T, E>, LD : LiveData<out ST>> List<LD>.isLoadingState(): LiveData<Boolean> =
MediatorLiveData(false)
.composition(this) { list ->
list.firstOrNull { it is ResourceState.Loading<*, *> } != null
}
this.isLoadingState()

@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("isErrorState", "dev.icerock.moko.mvvm.state.livedata"),
level = DeprecationLevel.WARNING
)
fun <T, E, ST : ResourceState<T, E>, LD : LiveData<out ST>> List<LD>.isErrorState(): LiveData<Boolean> =
MediatorLiveData(false)
.composition(this) { list ->
list.firstOrNull { it is ResourceState.Failed<*, *> } != null
}
this.isErrorState()

@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("isEmptyState", "dev.icerock.moko.mvvm.state.livedata"),
level = DeprecationLevel.WARNING
)
fun <T, E, ST : ResourceState<T, E>, LD : LiveData<out ST>> List<LD>.isEmptyState(): LiveData<Boolean> =
MediatorLiveData(false)
.composition(this) { list ->
list.firstOrNull { it is ResourceState.Empty<*, *> } != null
}
this.isEmptyState()
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,45 @@

package dev.icerock.moko.mvvm.livedata

import dev.icerock.moko.mvvm.ResourceState
import dev.icerock.moko.mvvm.state.ResourceState
import dev.icerock.moko.mvvm.state.livedata.data
import dev.icerock.moko.mvvm.state.livedata.dataValue
import dev.icerock.moko.mvvm.state.livedata.error
import dev.icerock.moko.mvvm.state.livedata.errorValue


@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("data", "dev.icerock.moko.mvvm.state.livedata"),
level = DeprecationLevel.WARNING
)
fun <T, E> LiveData<ResourceState<T, E>>.data(): LiveData<T?> = map { it.dataValue() }
fun <T, E> LiveData<ResourceState<T, E>>.data(): LiveData<T?> = this.data()

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

@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("error", "dev.icerock.moko.mvvm.state.livedata"),
level = DeprecationLevel.WARNING
)
fun <T, E> LiveData<ResourceState<T, E>>.error(): LiveData<E?> = map { it.errorValue() }
fun <T, E> LiveData<ResourceState<T, E>>.error(): LiveData<E?> = this.error()

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

@Deprecated(
message = "deprecated due to package renaming",
replaceWith = ReplaceWith("error", "dev.icerock.moko.mvvm.state.livedata"),
level = DeprecationLevel.WARNING
)
fun <T, E, ST : ResourceState<T, E>, LD : LiveData<out ST>> List<LD>.error(): LiveData<E?> =
MediatorLiveData<E?>(null)
.composition(this) { list ->
@Suppress("UNCHECKED_CAST")
val errorItem = list.firstOrNull {
(it is ResourceState.Failed<*, *>)
} as? ResourceState.Failed<T, E>
errorItem?.error
}
this.error()
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

package dev.icerock.moko.mvvm.livedata

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


@Deprecated(
message = "deprecated due to package renaming",
Expand All @@ -14,21 +16,4 @@ import dev.icerock.moko.mvvm.ResourceState
fun <T1, E, T2, OT> LiveData<ResourceState<T1, E>>.concatData(
liveData: LiveData<ResourceState<T2, E>>,
function: (T1, T2) -> OT
): LiveData<ResourceState<OT, E>> =
mediatorOf(this, liveData) { firstState, secondState ->
val state: 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()
}

state
}
): LiveData<ResourceState<OT, E>> = concatData(liveData, function)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

package dev.icerock.moko.mvvm.livedata

import dev.icerock.moko.mvvm.ResourceState
import dev.icerock.moko.mvvm.state.ResourceState
import dev.icerock.moko.mvvm.state.livedata.dataTransform
import dev.icerock.moko.mvvm.state.livedata.emptyAsData
import dev.icerock.moko.mvvm.state.livedata.emptyAsError
import dev.icerock.moko.mvvm.state.livedata.emptyIf
import dev.icerock.moko.mvvm.state.livedata.errorTransform


@Deprecated(
message = "deprecated due to package renaming",
Expand All @@ -13,15 +19,7 @@ import dev.icerock.moko.mvvm.ResourceState
)
fun <IT, E, OT> LiveData<ResourceState<IT, E>>.dataTransform(
transform: LiveData<IT>.() -> LiveData<OT>
): LiveData<ResourceState<OT, E>> = flatMap { state ->
when (state) {
is ResourceState.Success -> transform.invoke(MutableLiveData(state.data))
.map { ResourceState.Success(it) }
is ResourceState.Loading -> MutableLiveData(ResourceState.Loading())
is ResourceState.Empty -> MutableLiveData(ResourceState.Empty())
is ResourceState.Failed -> MutableLiveData(ResourceState.Failed(state.error))
}
}
): LiveData<ResourceState<OT, E>> = dataTransform(transform)

@Deprecated(
message = "deprecated due to package renaming",
Expand All @@ -30,15 +28,7 @@ fun <IT, E, OT> LiveData<ResourceState<IT, E>>.dataTransform(
)
fun <T, IE, OE> LiveData<ResourceState<T, IE>>.errorTransform(
transform: LiveData<IE>.() -> LiveData<OE>
): LiveData<ResourceState<T, OE>> = flatMap { state ->
when (state) {
is ResourceState.Success -> MutableLiveData(ResourceState.Success(state.data))
is ResourceState.Loading -> MutableLiveData(ResourceState.Loading())
is ResourceState.Empty -> MutableLiveData(ResourceState.Empty())
is ResourceState.Failed -> transform.invoke(MutableLiveData(state.error))
.map { ResourceState.Failed<T, OE>(it) }
}
}
): LiveData<ResourceState<T, OE>> = errorTransform(transform)

@Deprecated(
message = "deprecated due to package renaming",
Expand All @@ -47,12 +37,7 @@ fun <T, IE, OE> LiveData<ResourceState<T, IE>>.errorTransform(
)
fun <T, E> LiveData<ResourceState<T, E>>.emptyAsError(
errorBuilder: () -> E
): LiveData<ResourceState<T, E>> = map {
when (it) {
is ResourceState.Empty -> ResourceState.Failed(errorBuilder())
else -> it
}
}
): LiveData<ResourceState<T, E>> = emptyAsError(errorBuilder)

@Deprecated(
message = "deprecated due to package renaming",
Expand All @@ -61,12 +46,7 @@ fun <T, E> LiveData<ResourceState<T, E>>.emptyAsError(
)
fun <T, E> LiveData<ResourceState<T, E>>.emptyAsData(
dataBuilder: () -> T
): LiveData<ResourceState<T, E>> = map {
when (it) {
is ResourceState.Empty -> ResourceState.Success(dataBuilder())
else -> it
}
}
): LiveData<ResourceState<T, E>> = emptyAsData(dataBuilder)

@Deprecated(
message = "deprecated due to package renaming",
Expand All @@ -75,9 +55,4 @@ fun <T, E> LiveData<ResourceState<T, E>>.emptyAsData(
)
fun <T, E> LiveData<ResourceState<T, E>>.emptyIf(
emptyPredicate: (T) -> Boolean
): LiveData<ResourceState<T, E>> = map {
when {
it is ResourceState.Success && emptyPredicate(it.data) -> ResourceState.Empty()
else -> it
}
}
): LiveData<ResourceState<T, E>> = emptyIf(emptyPredicate)

0 comments on commit 9e9b65b

Please sign in to comment.