-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathResource.kt
97 lines (84 loc) · 3.05 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
package com.javiersc.resource
import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable
/**
* This class lets wrap any thing and add an associated state:
* Loading, Success and Error.
* @param S is the type of the Resource
* @Param E is the type of the Error
*/
@Serializable
public sealed class Resource<out S, out E> {
/**
* Loading state which has no params
*/
@Serializable
public object Loading : Resource<@Contextual Nothing, @Contextual Nothing>()
/**
* Success state which has a param [data] of type [S].
* @param data can be any object to be wrapped.
*/
@Serializable
public data class Success<out S>(val data: S) : Resource<S, @Contextual Nothing>()
/**
* Error state which has a param [error] of type [E]
* @param error can be any object to be wrapped.
*/
@Serializable
public data class Error<out E>(val error: E) : Resource<@Contextual Nothing, E>()
public val isLoading: Boolean get() = this is Loading
public val isSuccess: Boolean get() = this is Success
public val isError: Boolean get() = this is Error
/**
* A builder which lets fold a Resource with a series of functions that will be invoked based on
* the Resource state.
* @param resource to be folded.
*/
@Suppress("TooManyFunctions")
public inner class Folder(private val resource: Resource<S, E>) {
/**
* @param function callback will be invoked if Resource is Loading.
* Ideal to show a progress bar. Example:
* resource.fold {
* loading { progressBar.show() }
* }
*/
public fun loading(function: () -> Unit) {
if (resource is Loading) function()
}
/**
* @param function callback will be invoked if Resource is not Loading
* Here is easy to hide the progress bar because it is invoked if resource pass from Loading
* to Success, Error
*/
public fun noLoading(function: () -> Unit) {
if (resource !is Loading) function()
}
/**
* @param function callback will be invoked if Resource is Success and has data
* Ideal to populate the success data
*/
public fun success(function: (S) -> Unit) {
if (resource is Success) function(resource.data)
}
/**
* @param function callback will be invoked if Resource is not Success
*/
public fun noSuccess(function: () -> Unit) {
if (resource !is Success) function()
}
/**
* @param function callback will be invoked if Resource is Error
* Ideal to show an error message when something has failed
*/
public fun error(function: (E) -> Unit) {
if (resource is Error) function(resource.error)
}
/**
* @param function callback will be invoked if Resource is not Error
*/
public fun noError(function: () -> Unit) {
if (resource !is Error) function()
}
}
}