-
Notifications
You must be signed in to change notification settings - Fork 96
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
Work on generics in ResourceState #152
base: develop
Are you sure you want to change the base?
Changes from all commits
e3e814c
007bb4c
1f4798f
f5f7f5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,18 +4,18 @@ | |
|
||
package dev.icerock.moko.mvvm | ||
|
||
sealed class ResourceState<out T, out E> { | ||
data class Success<out T, out E>(val data: T) : ResourceState<T, E>() | ||
data class Failed<out T, out E>(val error: E) : ResourceState<T, E>() | ||
class Loading<out T, out E> : ResourceState<T, E>() | ||
class Empty<out T, out E> : ResourceState<T, E>() | ||
sealed class ResourceState<out TData, out TError> { | ||
data class Success<out TData>(val data: TData) : ResourceState<TData, Nothing>() | ||
data class Failed<out TError>(val error: TError) : ResourceState<Nothing, TError>() | ||
object Loading : ResourceState<Nothing, Nothing>() | ||
object Empty : ResourceState<Nothing, Nothing>() | ||
Comment on lines
+8
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in common:
try on swift:
|
||
|
||
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(): T? = (this as? Success)?.data | ||
fun dataValue(): TData? = (this as? Success)?.data | ||
|
||
fun errorValue(): E? = (this as? Failed)?.error | ||
fun errorValue(): TError? = (this as? Failed)?.error | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ fun <T, E> T?.asState(whenNull: () -> ResourceState<T, E>): ResourceState<T, E> | |
this?.asState() ?: whenNull() | ||
|
||
fun <T, E> List<T>.asState(): ResourceState<List<T>, E> = if (this.isEmpty()) { | ||
ResourceState.Empty() | ||
ResourceState.Empty | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should also add into release notes guide to migration. please add migration regexp to pr description, we will add it into release notes later |
||
} else { | ||
ResourceState.Success(this) | ||
} | ||
|
@@ -20,7 +20,7 @@ fun <T, E> List<T>?.asState(whenNull: () -> ResourceState<List<T>, E>): Resource | |
this?.asState() ?: whenNull() | ||
|
||
inline fun <reified T, reified E> ResourceState<T, E>?.nullAsEmpty(): ResourceState<T, E> = | ||
this ?: ResourceState.Empty() | ||
this ?: ResourceState.Empty | ||
|
||
inline fun <reified T, reified E> ResourceState<T, E>?.nullAsLoading(): ResourceState<T, E> = | ||
this ?: ResourceState.Loading() | ||
this ?: ResourceState.Loading |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this change should support simple automatically migration if we want to merge it...try to write some regexp which will allow us and others migrate to new version in minutes on any size of project codebase