Skip to content

Commit

Permalink
Disable button "next" as the files are getting copied locally
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarX committed Oct 18, 2024
1 parent 6b89d02 commit 9776057
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class NewTransferViewModel @Inject constructor(
private val _failedFiles = MutableSharedFlow<PickedFile>()
val failedFiles: SharedFlow<PickedFile> = _failedFiles

private val filesToImport: Channel<PickedFile> = Channel(capacity = Channel.UNLIMITED)
private val filesToImport: CountChannel<PickedFile> = CountChannel()
val filesToImportCount: StateFlow<Int> = filesToImport.count

private val filesMutationMutex = Mutex()

Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -174,6 +175,27 @@ class NewTransferViewModel @Inject constructor(
_failedFiles.emit(file)
}

private class CountChannel<T> {
private val channel = Channel<T>(capacity = Channel.UNLIMITED)

private val _count = MutableStateFlow(0)
val count: StateFlow<Int> = _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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileUi>,
filesToImportCount: () -> Int,
removeFileByUid: (uid: String) -> Unit,
addFiles: (List<Uri>) -> Unit,
closeActivity: () -> Unit,
Expand All @@ -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()
Expand Down Expand Up @@ -113,6 +120,6 @@ private fun ImportFilesScreen(
@Composable
private fun ImportFilesScreenPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List<FileUi>) {
SwissTransferTheme {
ImportFilesScreen({ files }, {}, {}, closeActivity = {})
ImportFilesScreen({ files }, { 0 }, {}, {}, closeActivity = {})
}
}

0 comments on commit 9776057

Please sign in to comment.