From 89f674b2f208e6b71b1a4087c9b3deec84f85b81 Mon Sep 17 00:00:00 2001 From: Gibran Chevalley Date: Thu, 15 Aug 2024 11:50:15 +0200 Subject: [PATCH] Already define a compose state and a "loading" status for the screen --- .../ui/screen/main/sent/SentScreen.kt | 19 +++++++++++++------ .../ui/screen/main/sent/SentViewModel.kt | 8 ++++++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/sent/SentScreen.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/sent/SentScreen.kt index b4322a703..fb5c30b50 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/sent/SentScreen.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/sent/SentScreen.kt @@ -24,11 +24,13 @@ import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteType import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp +import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.viewmodel.compose.viewModel import com.infomaniak.swisstransfer.R import com.infomaniak.swisstransfer.ui.components.NewTransferFab @@ -42,17 +44,20 @@ import com.infomaniak.swisstransfer.ui.utils.PreviewTablet @Composable fun SentScreen( navigateToDetails: (transferId: Int) -> Unit, + sentViewModel: SentViewModel = viewModel(), ) { - val viewmodel = viewModel() + val transfers by sentViewModel.transfers.collectAsStateWithLifecycle() SentScreen( - isEmpty = viewmodel.transfers.isEmpty(), + transfers = transfers, navType = LocalNavType.current, ) } @Composable -private fun SentScreen(isEmpty: Boolean, navType: NavigationSuiteType) { - if (isEmpty) { +private fun SentScreen(transfers: List?, navType: NavigationSuiteType) { + if (transfers == null) return + + if (transfers.isEmpty()) { EmptyScreen() } else { TransferScreen(navType) @@ -108,8 +113,9 @@ private fun SentScreenMobilePreview() { SwissTransferTheme { Surface { SentScreen( - isEmpty = true, + transfers = emptyList(), navType = NavigationSuiteType.NavigationBar, + isLoading = sentViewModel.isLoading, ) } } @@ -121,8 +127,9 @@ private fun SentScreenTabletPreview() { SwissTransferTheme { Surface { SentScreen( - isEmpty = true, + transfers = emptyList(), navType = NavigationSuiteType.NavigationRail, + isLoading = sentViewModel.isLoading, ) } } diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/sent/SentViewModel.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/sent/SentViewModel.kt index e7a403b84..4169e54d7 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/sent/SentViewModel.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/sent/SentViewModel.kt @@ -19,7 +19,11 @@ package com.infomaniak.swisstransfer.ui.screen.main.sent import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.callbackFlow +import kotlinx.coroutines.flow.stateIn -class SentViewModel: ViewModel() { - val transfers = emptyList() +class SentViewModel : ViewModel() { + val transfers = callbackFlow> { emptyList() }.stateIn(viewModelScope, SharingStarted.Eagerly, null) }