Skip to content

Commit

Permalink
fix: Load the selected transfer from database using flow
Browse files Browse the repository at this point in the history
  • Loading branch information
sirambd committed Nov 14, 2024
1 parent dbffa63 commit d896c64
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -63,17 +63,26 @@ fun TransferDetailsScreen(
navigateBack: (() -> Unit)?,
transferDetailsViewModel: TransferDetailsViewModel = hiltViewModel<TransferDetailsViewModel>(),
) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,9 +38,20 @@ class TransferDetailsViewModel @Inject constructor(
private val sharedApiUrlCreator: SharedApiUrlCreator,
) : ViewModel() {

private val _transferUuidFlow = MutableSharedFlow<String>(1)

@OptIn(ExperimentalCoroutinesApi::class)
val transfer = _transferUuidFlow
.flatMapLatest { transferManager.getTransferFlow(it) }
.stateIn(viewModelScope, SharingStarted.Eagerly, null)

val checkedFiles: SnapshotStateMap<String, Boolean> = 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)
}

0 comments on commit d896c64

Please sign in to comment.