Add SnackBarHost (#2800)

This commit is contained in:
Phil Oliver 2025-08-20 18:49:06 -04:00 committed by GitHub
parent 4f6fc7d345
commit 75561e06bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 61 additions and 41 deletions

View file

@ -20,7 +20,9 @@ package com.geeksville.mesh.model
import android.app.Application
import android.net.Uri
import android.os.RemoteException
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
@ -455,11 +457,25 @@ constructor(
fun getUser(userId: String?) = nodeDB.getUser(userId ?: DataPacket.ID_BROADCAST)
private val snackBarHostState = SnackbarHostState()
val snackBarHostState = SnackbarHostState()
fun showSnackBar(text: Int) = showSnackBar(app.getString(text))
fun showSnackBar(text: String) = viewModelScope.launch { snackBarHostState.showSnackbar(text) }
fun showSnackBar(
text: String,
actionLabel: String? = null,
withDismissAction: Boolean = false,
duration: SnackbarDuration = if (actionLabel == null) SnackbarDuration.Short else SnackbarDuration.Indefinite,
onActionPerformed: (() -> Unit) = {},
onDismissed: (() -> Unit) = {},
) = viewModelScope.launch {
snackBarHostState.showSnackbar(text, actionLabel, withDismissAction, duration).run {
when (this) {
SnackbarResult.ActionPerformed -> onActionPerformed()
SnackbarResult.Dismissed -> onDismissed()
}
}
}
init {
radioConfigRepository.errorMessage