-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathComposeInAppNotification.kt
More file actions
97 lines (87 loc) · 2.98 KB
/
Copy pathComposeInAppNotification.kt
File metadata and controls
97 lines (87 loc) · 2.98 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
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 dev.dimension.flare.common
import androidx.annotation.StringRes
import dev.dimension.flare.R
import dev.dimension.flare.data.repository.LoginExpiredException
import dev.dimension.flare.ui.presenter.login.ReloginTarget
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import org.koin.core.annotation.Single
internal sealed interface Notification {
data class Progress(
val progress: Int,
val total: Int,
) : Notification {
val percentage: Float
get() = progress.toFloat() / total
}
data class StringNotification(
@param:StringRes
@field:StringRes
val messageId: Int,
val success: Boolean,
val args: List<Any> = emptyList(),
val reloginTarget: ReloginTarget? = null,
) : Notification
}
@Single(binds = [InAppNotification::class])
internal class ComposeInAppNotification : InAppNotification {
private val _source = MutableStateFlow(Event<Notification>(null, initialHandled = true))
val source
get() = _source.asStateFlow()
override fun onProgress(
message: Message,
progress: Int,
total: Int,
) {
_source.value = Event(Notification.Progress(progress, total))
}
override fun onSuccess(message: Message) {
_source.value = Event(Notification.StringNotification(message.successTitle, success = true))
}
fun message(
@StringRes messageId: Int,
) {
_source.value = Event(Notification.StringNotification(messageId, success = true))
}
override fun onError(
message: Message,
throwable: Throwable,
) {
_source.value =
Event(
Notification.StringNotification(
message.errorTitle,
success = false,
args =
listOfNotNull(
if (throwable is LoginExpiredException) {
throwable.accountKey
} else {
null
},
),
reloginTarget =
if (throwable is LoginExpiredException) {
ReloginTarget(
accountKey = throwable.accountKey,
platformType = throwable.platformType,
)
} else {
null
},
),
)
}
}
private val Message.successTitle
get() =
when (this) {
Message.Compose -> R.string.compose_notification_success
Message.LoginExpired -> R.string.notification_login_expired
}
private val Message.errorTitle
get() =
when (this) {
Message.Compose -> R.string.compose_notification_error
Message.LoginExpired -> R.string.notification_login_expired
}