-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resource.kt
105 lines (80 loc) · 2.53 KB
/
Resource.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package extension
import Resource
// basic
inline val Resource<*, *>.isLoading get() = this == Resource.Loading
inline val Resource<*, *>.isSuccess get() = this is Resource.Result.Success
inline val Resource<*, *>.isError get() = this is Resource.Result.Failure
fun <T, E> Resource.Result<T, E>.asResource() = this as Resource<T, E>
// map
fun <T, E, T2> Resource<T, E>.mapSuccess(
transform: (T) -> T2
) = when (this) {
is Resource.Result.Failure -> Resource.Result.Failure(error)
is Resource.Result.Success -> Resource.Result.Success(transform(data))
Resource.Loading -> Resource.Loading
}
fun <T, E, T2> Resource.Result<T, E>.mapSuccess(
transform: (T) -> T2
) = when (this) {
is Resource.Result.Failure -> Resource.Result.Failure(error)
is Resource.Result.Success -> Resource.Result.Success(transform(data))
}
fun <T, E, E2> Resource<T, E>.mapError(
transform: (E) -> E2
) = when (this) {
is Resource.Result.Failure -> Resource.Result.Failure(transform(error))
is Resource.Result.Success -> Resource.Result.Success(data)
Resource.Loading -> Resource.Loading
}
fun <T, E, E2> Resource.Result<T, E>.mapError(
transform: (E) -> E2
) = when (this) {
is Resource.Result.Failure -> Resource.Result.Failure(transform(error))
is Resource.Result.Success -> Resource.Result.Success(data)
}
// action
inline fun <T, E> Resource<T, E>.ifSuccess(
onSuccess: (T) -> Unit,
) = apply {
if (this is Resource.Result.Success) {
onSuccess(data)
}
}
inline fun <T, E> Resource.Result<T, E>.ifSuccess(
onSuccess: (T) -> Unit,
) = apply {
asResource().ifSuccess(onSuccess)
}
inline fun <T, E> Resource<T, E>.ifFailure(
onError: (E) -> Unit,
) = apply {
if (this is Resource.Result.Failure) {
onError(error)
}
}
inline fun <T, E> Resource.Result<T, E>.ifFailure(
onError: (E) -> Unit,
) = apply {
asResource().ifFailure(onError)
}
inline fun <T, E> Resource<T, E>.ifLoading(
onLoading: () -> Unit,
) = apply {
if (this == Resource.Loading) {
onLoading()
}
}
// getter
inline fun <R, T : R, E> Resource<T, E>.getOrElse(onElse: () -> R): R {
ifSuccess { return it }
return onElse()
}
fun <T, E> Resource<T, E>.getOrNull() = getOrElse { null }
fun <T> Resource<T, *>.getOrThrow(
error: Throwable = Throwable("$this isn't success"),
) = getOrElse { throw error }
// parser
fun <T> Resource.Result<T, Throwable>.toResult() = when (this) {
is Resource.Result.Success -> Result.success(data)
is Resource.Result.Failure -> Result.failure(error)
}