Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
120 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/jshvarts/conditionalbottomnav/model/SnackbarManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters