From d896c6455cd0061287ad516d6c19d87ddf784a93 Mon Sep 17 00:00:00 2001 From: Abdourahamane Boinaidi Date: Thu, 14 Nov 2024 12:19:49 +0100 Subject: [PATCH] fix: Load the selected transfer from database using flow --- .../transferdetails/TransferDetailsScreen.kt | 33 ++++++++++++------- .../TransferDetailsViewModel.kt | 21 ++++++++++-- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/transferdetails/TransferDetailsScreen.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/transferdetails/TransferDetailsScreen.kt index e96e3ad60..f9788ed62 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/transferdetails/TransferDetailsScreen.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/transferdetails/TransferDetailsScreen.kt @@ -33,6 +33,7 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.PreviewParameter import androidx.compose.ui.unit.dp import androidx.hilt.navigation.compose.hiltViewModel +import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.infomaniak.core2.FORMAT_DATE_FULL import com.infomaniak.core2.format import com.infomaniak.multiplatform_swisstransfer.common.ext.toDateFromSeconds @@ -46,7 +47,6 @@ import com.infomaniak.swisstransfer.ui.images.icons.LockedTextField import com.infomaniak.swisstransfer.ui.images.icons.QrCode import com.infomaniak.swisstransfer.ui.images.icons.Share import com.infomaniak.swisstransfer.ui.previewparameter.TransferUiListPreviewParameter -import com.infomaniak.swisstransfer.ui.previewparameter.emailsPreviewData import com.infomaniak.swisstransfer.ui.screen.main.components.SmallWindowTopAppBarScaffold import com.infomaniak.swisstransfer.ui.screen.main.transferdetails.components.PasswordBottomSheet import com.infomaniak.swisstransfer.ui.screen.main.transferdetails.components.QrCodeBottomSheet @@ -63,17 +63,26 @@ fun TransferDetailsScreen( navigateBack: (() -> Unit)?, transferDetailsViewModel: TransferDetailsViewModel = hiltViewModel(), ) { - TransferDetailsScreen( - transferUrl = transferDetailsViewModel.getTransferUrl(transferUuid), - direction = direction, - navigateBack = navigateBack, - getTransfer = { transferDetailsViewModel.getTransfer(transferUuid) }, - getCheckedFiles = { transferDetailsViewModel.checkedFiles }, - clearCheckedFiles = { transferDetailsViewModel.checkedFiles.clear() }, - setFileCheckStatus = { fileUid, isChecked -> - transferDetailsViewModel.checkedFiles[fileUid] = isChecked - }, - ) + val transfer by transferDetailsViewModel.transfer.collectAsStateWithLifecycle() + val isLoading by remember { derivedStateOf { transfer == null } } + + LaunchedEffect(transferUuid) { + transferDetailsViewModel.loadTransfer(transferUuid) + } + + if (!isLoading) { + TransferDetailsScreen( + transferUrl = transferDetailsViewModel.getTransferUrl(transferUuid), + direction = direction, + navigateBack = navigateBack, + getTransfer = { transfer }, + getCheckedFiles = { transferDetailsViewModel.checkedFiles }, + clearCheckedFiles = { transferDetailsViewModel.checkedFiles.clear() }, + setFileCheckStatus = { fileUid, isChecked -> + transferDetailsViewModel.checkedFiles[fileUid] = isChecked + }, + ) + } } @Composable diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/transferdetails/TransferDetailsViewModel.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/transferdetails/TransferDetailsViewModel.kt index 07e87f4bb..b3b2e994c 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/transferdetails/TransferDetailsViewModel.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/transferdetails/TransferDetailsViewModel.kt @@ -20,10 +20,16 @@ package com.infomaniak.swisstransfer.ui.screen.main.transferdetails import androidx.compose.runtime.mutableStateMapOf import androidx.compose.runtime.snapshots.SnapshotStateMap import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.infomaniak.multiplatform_swisstransfer.SharedApiUrlCreator -import com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui.TransferUi import com.infomaniak.multiplatform_swisstransfer.managers.TransferManager import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.flatMapLatest +import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.launch import javax.inject.Inject @HiltViewModel @@ -32,9 +38,20 @@ class TransferDetailsViewModel @Inject constructor( private val sharedApiUrlCreator: SharedApiUrlCreator, ) : ViewModel() { + private val _transferUuidFlow = MutableSharedFlow(1) + + @OptIn(ExperimentalCoroutinesApi::class) + val transfer = _transferUuidFlow + .flatMapLatest { transferManager.getTransferFlow(it) } + .stateIn(viewModelScope, SharingStarted.Eagerly, null) + val checkedFiles: SnapshotStateMap = mutableStateMapOf() - fun getTransfer(transferUuid: String): TransferUi? = transferManager.getTransferByUUID(transferUuid) + fun loadTransfer(transferUuid: String) { + viewModelScope.launch { + _transferUuidFlow.emit(transferUuid) + } + } fun getTransferUrl(transferUuid: String): String = sharedApiUrlCreator.shareTransferUrl(transferUuid) }