-
Notifications
You must be signed in to change notification settings - Fork 3
/
SnackbarManager.kt
35 lines (28 loc) · 993 Bytes
/
SnackbarManager.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
package com.jshvarts.conditionalbottomnav.model
import androidx.annotation.StringRes
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import java.util.*
data class Message(val id: Long, @StringRes val messageId: Int)
/**
* Class responsible for managing Snackbar messages to show on the screen
*/
object SnackbarManager {
private val _messages: MutableStateFlow<List<Message>> = MutableStateFlow(emptyList())
val messages: StateFlow<List<Message>> get() = _messages.asStateFlow()
fun showMessage(@StringRes messageTextId: Int) {
_messages.update { currentMessages ->
currentMessages + Message(
id = UUID.randomUUID().mostSignificantBits,
messageId = messageTextId
)
}
}
fun setMessageShown(messageId: Long) {
_messages.update { currentMessages ->
currentMessages.filterNot { it.id == messageId }
}
}
}