-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateResource.kt
More file actions
65 lines (51 loc) · 1.63 KB
/
Copy pathStateResource.kt
File metadata and controls
65 lines (51 loc) · 1.63 KB
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
package com.anikinkirill.powerfulandroidapp.ui
data class Loading(val isLoading: Boolean)
data class Data<T>(val data: Event<T>?, val response: Event<Response>?)
data class StateError(val response: Response)
data class Response(val message: String?, val responseType: ResponseType)
sealed class ResponseType {
class Toast : ResponseType()
class Dialog : ResponseType()
class None : ResponseType()
}
/**
* Used as a wrapper for data that is exposed via a LiveData that represents an event.
*/
class Event<T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}
/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
override fun toString(): String {
return "Event(content=$content,hasBeenHandled=$hasBeenHandled)"
}
companion object{
// we don't want an event if there's no data
fun <T> dataEvent(data: T?): Event<T>?{
data?.let {
return Event(it)
}
return null
}
// we don't want an event if the response is null
fun responseEvent(response: Response?): Event<Response>?{
response?.let{
return Event(response)
}
return null
}
}
}