У меня есть NotificationService в моем приложении (одиночка, в которую разные компоненты могут добавлять уведомления ), который имеет составную функцию для отображения текущих уведомлений в отдельных окнах.
Это упрощенная версия
Код: Выделить всё
object NotificationService {
private val notifications = mutableListOf()
@Composable
fun renderNotifications() {
// cleanup old notifications
notifications.removeAll { it.showUntil.isBefore(OffsetDateTime.now()) }
var index = 0
notifications.forEach {
Notification(it.message, index)
index++
}
}
fun notificationCount(): Int {
return notifications.size
}
fun addNotification(message: String) {
notifications.add(NotificationContainer(message, OffsetDateTime.now().plusSeconds(5)))
}
}
data class NotificationContainer(val message: String, val showUntil: OffsetDateTime)
Код: Выделить всё
fun main() = application {
// dependencies
val notificationService = NotificationService
notificationService.renderNotifications()
OtherComposable() { ... }
}
Код: Выделить всё
OtherComposable
Поздравляем и спасибо!
Подробнее здесь: https://stackoverflow.com/questions/789 ... te-changes