-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadingState.kt
56 lines (50 loc) · 1.95 KB
/
LoadingState.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
package com.kazakago.storeflowable.core
import com.kazakago.storeflowable.core.LoadingState.*
/**
* This sealed class that represents the state of the data.
*
* The following three states are shown.
* - [Loading] is acquiring data.
* - [Completed] has not been processed.
* - [Error] is an error when processing.
*
* @param T Types of data to be included.
*/
public sealed class LoadingState<out T> {
/**
* when data fetch is processing.
*
* @param content Indicates the existing or not existing of data.
*/
public data class Loading<out T>(val content: T?) : LoadingState<T>()
/**
* When data fetch is successful.
*
* @param content Raw data.
* @param next next pagination state of the data. If not pagination, Always returned [AdditionalLoadingState.Fixed] state.
* @param prev prev pagination state of the data. If not pagination, Always returned [AdditionalLoadingState.Fixed] state.
*/
public data class Completed<out T>(val content: T, val next: AdditionalLoadingState, val prev: AdditionalLoadingState) : LoadingState<T>()
/**
* when data fetch is failure.
*
* @param exception Occurred exception.
*/
public data class Error<out T>(val exception: Exception) : LoadingState<T>()
/**
* Provides state-specific callbacks.
* Same as `when (state) { ... }`.
*
* @param onLoading Callback for [Loading].
* @param onCompleted Callback for [Completed].
* @param onError Callback for [Error].
* @return Can return a value of any type.
*/
public inline fun <V> doAction(onLoading: ((content: T?) -> V), onCompleted: ((content: T, next: AdditionalLoadingState, prev: AdditionalLoadingState) -> V), onError: ((exception: Exception) -> V)): V {
return when (this) {
is Loading -> onLoading(content)
is Completed -> onCompleted(content, next, prev)
is Error -> onError(exception)
}
}
}