Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Load the selected transfer from database using flow #187

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading