Skip to content

Commit

Permalink
Compute importation progress
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarX committed Oct 18, 2024
1 parent 3ccf72c commit 7cdfb2c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ class NewTransferViewModel @Inject constructor(
private val _failedFiles = MutableSharedFlow<PickedFile>()
val failedFiles: SharedFlow<PickedFile> = _failedFiles

private val filesToImport: CountChannel<PickedFile> = CountChannel()
private val filesToImport: TransferCountChannel = TransferCountChannel()
val filesToImportCount: StateFlow<Int> = filesToImport.count
val currentSessionTotalUploadedFiles: StateFlow<Int> = filesToImport.currentSessionTotalUploadedFiles

private val filesMutationMutex = Mutex()

Expand Down Expand Up @@ -181,23 +182,34 @@ class NewTransferViewModel @Inject constructor(
_failedFiles.emit(file)
}

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

private val _count = MutableStateFlow(0)
val count: StateFlow<Int> = _count

// Session resets when reaching 0 files in the queue
private val _currentSessionTotalUploadedFiles = MutableStateFlow(0)
val currentSessionTotalUploadedFiles: StateFlow<Int> = _currentSessionTotalUploadedFiles

private val countMutex = Mutex()

suspend fun send(element: T) {
countMutex.withLock { _count.value += 1 }
suspend fun send(element: PickedFile) {
countMutex.withLock {
_count.value += 1
_currentSessionTotalUploadedFiles.value += 1
}
channel.send(element)
}

suspend fun consume(process: suspend (T) -> Unit) {
suspend fun consume(process: suspend (PickedFile) -> Unit) {
for (element in channel) {
process(element)
countMutex.withLock { _count.value -= 1 }
countMutex.withLock {
val newValue = _count.value - 1
_count.value = newValue
if (newValue == 0) _currentSessionTotalUploadedFiles.value = 0
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ fun ImportFilesScreen(
) {
val files by newTransferViewModel.filesDebounced.collectAsStateWithLifecycle()
val filesToImportCount by newTransferViewModel.filesToImportCount.collectAsStateWithLifecycle()
val currentSessionTotalUploadedFiles by newTransferViewModel.currentSessionTotalUploadedFiles.collectAsStateWithLifecycle()
ImportFilesScreen(
files = { files },
filesToImportCount = { filesToImportCount },
currentSessionTotalUploadedFiles = { currentSessionTotalUploadedFiles },
removeFileByUid = newTransferViewModel::removeFileByUid,
addFiles = newTransferViewModel::addFiles,
closeActivity = closeActivity
Expand All @@ -60,6 +62,7 @@ fun ImportFilesScreen(
private fun ImportFilesScreen(
files: () -> List<FileUi>,
filesToImportCount: () -> Int,
currentSessionTotalUploadedFiles: () -> Int,
removeFileByUid: (uid: String) -> Unit,
addFiles: (List<Uri>) -> Unit,
closeActivity: () -> Unit,
Expand All @@ -73,7 +76,9 @@ private fun ImportFilesScreen(
val spaceLeft = (TOTAL_FILE_SIZE - usedSpace).coerceAtLeast(0)
getHumanReadableSize(context, spaceLeft)
}
val isSendButtonEnabled by remember { derivedStateOf { importedFiles.isNotEmpty() && filesToImportCount() == 0 } }
val isSendButtonEnabled by remember { derivedStateOf { importedFiles.isNotEmpty() } }
val isImporting by remember { derivedStateOf { filesToImportCount() > 0 } }
val importProgress by remember { derivedStateOf { 1 - (filesToImportCount().toFloat() / currentSessionTotalUploadedFiles()) } }

val filePickerLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.OpenMultipleDocuments()
Expand All @@ -90,11 +95,17 @@ private fun ImportFilesScreen(
)
},
topButton = { modifier ->
val progress: (() -> Float)? = if (isImporting) {
{ importProgress }
} else {
null
}

LargeButton(
modifier = modifier,
titleRes = R.string.transferSendButton,
style = ButtonType.PRIMARY,
enabled = { isSendButtonEnabled },
progress = progress,
onClick = { /*TODO*/ },
)
},
Expand All @@ -120,6 +131,13 @@ private fun ImportFilesScreen(
@Composable
private fun ImportFilesScreenPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List<FileUi>) {
SwissTransferTheme {
ImportFilesScreen({ files }, { 0 }, {}, {}, closeActivity = {})
ImportFilesScreen(
files = { files },
filesToImportCount = { 0 },
currentSessionTotalUploadedFiles = { 0 },
removeFileByUid = {},
addFiles = {},
closeActivity = {}
)
}
}

0 comments on commit 7cdfb2c

Please sign in to comment.