package com.craigvg.lichun_android.managers import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import javax.inject.Inject import javax.inject.Singleton /** * Toast types for different notification styles */ enum class ToastType { SUCCESS, ERROR, WARNING, INFO } /** * Toast data class */ data class ToastData( val id: String = java.util.UUID.randomUUID().toString(), val type: ToastType, val message: String, val duration: Long = 3000L ) /** * Toast manager for showing toast notifications * Ported from iOS ToastManager.swift */ @Singleton class ToastManager @Inject constructor() { private val _currentToast = MutableStateFlow(null) val currentToast: StateFlow = _currentToast.asStateFlow() private val lock = Any() private val toastQueue = mutableListOf() /** * Show a toast notification */ fun show(type: ToastType, message: String, duration: Long = 3000L) { synchronized(lock) { val toast = ToastData( type = type, message = message, duration = duration ) toastQueue.add(toast) if (_currentToast.value == null) { showNextToast() } } } /** * Show success toast */ fun success(message: String) { show(ToastType.SUCCESS, message) } /** * Show error toast */ fun error(message: String) { show(ToastType.ERROR, message) } /** * Show warning toast */ fun warning(message: String) { show(ToastType.WARNING, message) } /** * Show info toast */ fun info(message: String) { show(ToastType.INFO, message) } /** * Dismiss current toast */ fun dismiss() { synchronized(lock) { _currentToast.value = null showNextToast() } } private fun showNextToast() { if (toastQueue.isNotEmpty()) { _currentToast.value = toastQueue.removeAt(0) } } }