From 9776057f917d91c616f1b3a4a2db3c492c83be04 Mon Sep 17 00:00:00 2001 From: Gibran Chevalley Date: Wed, 16 Oct 2024 12:58:54 +0200 Subject: [PATCH] Disable button "next" as the files are getting copied locally --- .../newtransfer/NewTransferViewModel.kt | 28 +++++++++++++++++-- .../importfiles/ImportFilesScreen.kt | 15 +++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt index 010b1befd..a28e39f92 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt @@ -53,7 +53,8 @@ class NewTransferViewModel @Inject constructor( private val _failedFiles = MutableSharedFlow() val failedFiles: SharedFlow = _failedFiles - private val filesToImport: Channel = Channel(capacity = Channel.UNLIMITED) + private val filesToImport: CountChannel = CountChannel() + val filesToImportCount: StateFlow = filesToImport.count private val filesMutationMutex = Mutex() @@ -125,13 +126,13 @@ class NewTransferViewModel @Inject constructor( } private suspend fun observeFilesToImport() { - for (fileToImport in filesToImport) { + filesToImport.consume { fileToImport -> Log.i(TAG, "Importing ${fileToImport.uri}") val copiedFile = copyFileLocally(fileToImport.uri, fileToImport.fileName) if (copiedFile == null) { reportFailedImportation(fileToImport) - continue + return@consume } Log.i(TAG, "Successfully imported ${fileToImport.uri}") @@ -174,6 +175,27 @@ class NewTransferViewModel @Inject constructor( _failedFiles.emit(file) } + private class CountChannel { + private val channel = Channel(capacity = Channel.UNLIMITED) + + private val _count = MutableStateFlow(0) + val count: StateFlow = _count + + private val countMutex = Mutex() + + suspend fun send(element: T) { + countMutex.withLock { _count.value += 1 } + channel.send(element) + } + + suspend fun consume(process: suspend (T) -> Unit) { + for (element in channel) { + process(element) + countMutex.withLock { _count.value -= 1 } + } + } + } + companion object { private const val TAG = "File importation" const val LOCAL_COPY_FOLDER = "local_copy_folder" diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt index ce5b3d6be..d69d4d4dc 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt @@ -46,12 +46,20 @@ fun ImportFilesScreen( closeActivity: () -> Unit, ) { val files by newTransferViewModel.files.collectAsStateWithLifecycle() - ImportFilesScreen({ files }, newTransferViewModel::removeFileByUid, newTransferViewModel::addFiles, closeActivity) + val filesToImportCount by newTransferViewModel.filesToImportCount.collectAsStateWithLifecycle() + ImportFilesScreen( + files = { files }, + filesToImportCount = { filesToImportCount }, + removeFileByUid = newTransferViewModel::removeFileByUid, + addFiles = newTransferViewModel::addFiles, + closeActivity = closeActivity + ) } @Composable private fun ImportFilesScreen( files: () -> List, + filesToImportCount: () -> Int, removeFileByUid: (uid: String) -> Unit, addFiles: (List) -> Unit, closeActivity: () -> Unit, @@ -65,8 +73,7 @@ private fun ImportFilesScreen( val spaceLeft = (TOTAL_FILE_SIZE - usedSpace).coerceAtLeast(0) getHumanReadableSize(context, spaceLeft) } - - val isSendButtonEnabled by remember { derivedStateOf { importedFiles.isNotEmpty() } } + val isSendButtonEnabled by remember { derivedStateOf { importedFiles.isNotEmpty() && filesToImportCount() == 0 } } val filePickerLauncher = rememberLauncherForActivityResult( contract = ActivityResultContracts.OpenMultipleDocuments() @@ -113,6 +120,6 @@ private fun ImportFilesScreen( @Composable private fun ImportFilesScreenPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List) { SwissTransferTheme { - ImportFilesScreen({ files }, {}, {}, closeActivity = {}) + ImportFilesScreen({ files }, { 0 }, {}, {}, closeActivity = {}) } }