Skip to content

Commit 10f1be6

Browse files
author
Pablo Orgaz
committed
Add to string method for resource
1 parent df79566 commit 10f1be6

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
buildscript {
22
ext {
33
kotlin_version = "1.3.41"
4-
mini_version = "4.0.2"
4+
mini_version = "4.0.3"
55
}
66

77
repositories {

mini-common/src/main/java/mini/Resource.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ open class Resource<out T> @PublishedApi internal constructor(val value: Any?) {
1919
}
2020

2121
@PublishedApi
22-
internal data class Failure(val exception: Throwable?)
22+
internal data class Failure(val exception: Throwable?) {
23+
override fun toString(): String = "Failure($exception)"
24+
}
2325

2426
@PublishedApi
25-
internal data class Loading<U>(val value: U? = null)
27+
internal data class Loading<U>(val value: U? = null) {
28+
override fun toString(): String = "Loading($value)"
29+
}
2630

2731
/**
2832
* Get the current value if successful, or null for other cases.
@@ -45,6 +49,10 @@ open class Resource<out T> @PublishedApi internal constructor(val value: Any?) {
4549
fun <T> loading(value: T? = null): Resource<T> = Resource(Loading(value))
4650
fun <T> empty(): Resource<T> = Resource(Empty())
4751
}
52+
53+
override fun toString(): String {
54+
return value.toString()
55+
}
4856
}
4957

5058
/**
@@ -60,6 +68,16 @@ class Task(value: Any?) : Resource<Unit>(value) {
6068
fun loading(): Task = Task(Loading<Unit>())
6169
fun failure(exception: Throwable? = null): Task = Task(Failure(exception))
6270
}
71+
72+
override fun toString(): String {
73+
return when {
74+
isSuccess -> "Success"
75+
isFailure -> "Failure"
76+
isLoading -> "Loading"
77+
isIdle -> "Idle"
78+
else -> value.toString()
79+
}
80+
}
6381
}
6482

6583
inline fun <T> Resource<T>.onSuccess(crossinline action: (data: T) -> Unit): Resource<T> {

0 commit comments

Comments
 (0)