diff --git a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/relationships/RelationshipFragment.kt b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/relationships/RelationshipFragment.kt index 878bbfd918..fe6644c61c 100644 --- a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/relationships/RelationshipFragment.kt +++ b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/relationships/RelationshipFragment.kt @@ -140,8 +140,6 @@ class RelationshipFragment : FragmentGlobalAbstract(), RelationshipView { val uiState by relationShipsViewModel.relationshipsUiState.collectAsState() val relationshipSelectionState by relationShipsViewModel.relationshipSelectionState.collectAsState() val showDeleteConfirmation by relationShipsViewModel.showDeleteConfirmation.collectAsState() - val snackbarMessage = - relationShipsViewModel.snackbarMessage.collectAsState(initial = null) when (showMap) { true -> RelationshipMapScreen(savedInstanceState) @@ -163,7 +161,6 @@ class RelationshipFragment : FragmentGlobalAbstract(), RelationshipView { ) }, onRelationShipSelected = relationShipsViewModel::updateSelectedList, - snackbarMessage = snackbarMessage.value, ) } diff --git a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/ui/RelationshipsScreen.kt b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/ui/RelationshipsScreen.kt index 86ccec79ce..f14aef4a19 100644 --- a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/ui/RelationshipsScreen.kt +++ b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/ui/RelationshipsScreen.kt @@ -16,14 +16,15 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.wrapContentHeight import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items -import androidx.compose.material.Scaffold -import androidx.compose.material.SnackbarHost import androidx.compose.material.icons.Icons import androidx.compose.material.icons.outlined.Add import androidx.compose.material.icons.outlined.Delete import androidx.compose.material.icons.outlined.ErrorOutline -import androidx.compose.material.rememberScaffoldState import androidx.compose.material3.Icon +import androidx.compose.material3.Scaffold +import androidx.compose.material3.SnackbarDuration +import androidx.compose.material3.SnackbarHost +import androidx.compose.material3.SnackbarHostState import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect @@ -79,20 +80,20 @@ fun RelationShipsScreen( onCreateRelationshipClick: (RelationshipSection) -> Unit, onRelationshipClick: (RelationshipItem) -> Unit, onRelationShipSelected: (String) -> Unit, - snackbarMessage: String?, ) { - val scaffoldState = rememberScaffoldState() - val snackbarHostState = scaffoldState.snackbarHostState + val snackbarHostState = remember { SnackbarHostState() } - LaunchedEffect(snackbarMessage) { - snackbarMessage?.let { message -> - snackbarHostState.showSnackbar(message) + LaunchedEffect(uiState.snackbarMessage) { + uiState.snackbarMessage.collect { message -> + snackbarHostState.showSnackbar( + message = message, + duration = SnackbarDuration.Long + ) } } Scaffold( - scaffoldState = scaffoldState, - snackbarHost = { SnackbarHost(hostState = snackbarHostState) } + snackbarHost = { SnackbarHost(snackbarHostState) } ) { contentPadding -> LazyColumn( modifier = Modifier @@ -374,7 +375,6 @@ fun RelationShipScreenPreview() { onCreateRelationshipClick = {}, onRelationshipClick = {}, onRelationShipSelected = {}, - snackbarMessage = null, ) } diff --git a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/ui/RelationshipsUiState.kt b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/ui/RelationshipsUiState.kt index dc70158f9f..ea4fdbbac0 100644 --- a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/ui/RelationshipsUiState.kt +++ b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/ui/RelationshipsUiState.kt @@ -1,6 +1,16 @@ package org.dhis2.tracker.relationships.ui +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.SharedFlow + sealed class RelationshipsUiState { + private val _snackbarMessage = MutableSharedFlow() + val snackbarMessage: SharedFlow get() = _snackbarMessage + + suspend fun sendSnackbarMessage(message: String) { + _snackbarMessage.emit(message) + } + data object Loading : RelationshipsUiState() data object Empty : RelationshipsUiState() data class Success(val data: T) : RelationshipsUiState() diff --git a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/ui/RelationshipsViewModel.kt b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/ui/RelationshipsViewModel.kt index 79c4bdb416..66ed2203c1 100644 --- a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/ui/RelationshipsViewModel.kt +++ b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/ui/RelationshipsViewModel.kt @@ -3,10 +3,8 @@ package org.dhis2.tracker.relationships.ui import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow -import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flowOf @@ -41,9 +39,6 @@ class RelationshipsViewModel( private val _showDeleteConfirmation = MutableStateFlow(false) val showDeleteConfirmation = _showDeleteConfirmation.asStateFlow() - private val _snackbarMessage = MutableSharedFlow() - val snackbarMessage = _snackbarMessage.asSharedFlow() - fun refreshRelationships() { viewModelScope.launch(dispatcher.io()) { getRelationshipsByType() @@ -162,7 +157,7 @@ class RelationshipsViewModel( private fun showSnackbar(message: String) { viewModelScope.launch { - _snackbarMessage.emit(message) + _relationshipsUiState.value.sendSnackbarMessage(message) } } } \ No newline at end of file