Skip to content

Commit

Permalink
ci: [ANDROAPP-6535] Add error message
Browse files Browse the repository at this point in the history
Signed-off-by: andresmr <[email protected]>
  • Loading branch information
andresmr committed Dec 12, 2024
1 parent 88b16fb commit b1a4ec1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -163,7 +161,6 @@ class RelationshipFragment : FragmentGlobalAbstract(), RelationshipView {
)
},
onRelationShipSelected = relationShipsViewModel::updateSelectedList,
snackbarMessage = snackbarMessage.value,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -374,7 +375,6 @@ fun RelationShipScreenPreview() {
onCreateRelationshipClick = {},
onRelationshipClick = {},
onRelationShipSelected = {},
snackbarMessage = null,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
package org.dhis2.tracker.relationships.ui

import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow

sealed class RelationshipsUiState<out T> {
private val _snackbarMessage = MutableSharedFlow<String>()
val snackbarMessage: SharedFlow<String> get() = _snackbarMessage

suspend fun sendSnackbarMessage(message: String) {
_snackbarMessage.emit(message)
}

data object Loading : RelationshipsUiState<Nothing>()
data object Empty : RelationshipsUiState<Nothing>()
data class Success<T>(val data: T) : RelationshipsUiState<T>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -41,9 +39,6 @@ class RelationshipsViewModel(
private val _showDeleteConfirmation = MutableStateFlow(false)
val showDeleteConfirmation = _showDeleteConfirmation.asStateFlow()

private val _snackbarMessage = MutableSharedFlow<String>()
val snackbarMessage = _snackbarMessage.asSharedFlow()

fun refreshRelationships() {
viewModelScope.launch(dispatcher.io()) {
getRelationshipsByType()
Expand Down Expand Up @@ -162,7 +157,7 @@ class RelationshipsViewModel(

private fun showSnackbar(message: String) {
viewModelScope.launch {
_snackbarMessage.emit(message)
_relationshipsUiState.value.sendSnackbarMessage(message)
}
}
}

0 comments on commit b1a4ec1

Please sign in to comment.