From 5a85166f3f3a99f005d8e72578643ed2a0344098 Mon Sep 17 00:00:00 2001 From: Kevin Boulongne Date: Tue, 17 Dec 2024 11:44:08 +0100 Subject: [PATCH 1/3] feat: When cancelling the uploading transfer, we're going back to ImportFiles instead of closing Activity --- .../screen/newtransfer/NewTransferNavHost.kt | 2 +- .../importfiles/ImportFilesScreen.kt | 5 ++++- .../upload/UploadProgressScreen.kt | 21 ++++++++++++------- .../swisstransfer/workers/UploadWorker.kt | 6 +++++- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferNavHost.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferNavHost.kt index 6c04aad2e..cee05be8e 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferNavHost.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferNavHost.kt @@ -53,7 +53,7 @@ fun NewTransferNavHost(navController: NavHostController, closeActivity: () -> Un navController.navigate(UploadSuccessDestination(args.transferType, transferUrl)) }, navigateToUploadError = { navController.navigate(UploadErrorDestination) }, - closeActivity = closeActivity, + navigateBackToImportFiles = { navController.popBackStack() }, ) } composable { 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 0001d1354..09e6ab034 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 @@ -153,7 +153,10 @@ private fun HandleSendActionResult( LaunchedEffect(getSendActionResult()) { when (val actionResult = getSendActionResult()) { - is SendActionResult.Success -> navigateToUploadProgress(transferType(), actionResult.totalSize) + is SendActionResult.Success -> { + resetSendActionResult() + navigateToUploadProgress(transferType(), actionResult.totalSize) + } is SendActionResult.Failure -> { snackbarHostState.showSnackbar(context.getString(R.string.errorUnknown)) resetSendActionResult() diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/upload/UploadProgressScreen.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/upload/UploadProgressScreen.kt index 5c6a292db..69ad7570d 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/upload/UploadProgressScreen.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/upload/UploadProgressScreen.kt @@ -49,7 +49,7 @@ fun UploadProgressScreen( totalSizeInBytes: Long, navigateToUploadSuccess: (String) -> Unit, navigateToUploadError: () -> Unit, - closeActivity: () -> Unit, + navigateBackToImportFiles: () -> Unit, uploadProgressViewModel: UploadProgressViewModel = hiltViewModel(), ) { val uiState by uploadProgressViewModel.transferProgressUiState.collectAsStateWithLifecycle() @@ -66,7 +66,7 @@ fun UploadProgressScreen( uploadProgressViewModel.trackUploadProgress() } - HandleProgressState({ uiState }, navigateToUploadSuccess, navigateToUploadError) + HandleProgressState({ uiState }, navigateToUploadSuccess, navigateToUploadError, navigateBackToImportFiles) UploadProgressScreen( progressState = { uiState }, @@ -74,10 +74,7 @@ fun UploadProgressScreen( totalSizeInBytes = totalSizeInBytes, showBottomSheet = GetSetCallbacks(get = { showBottomSheet }, set = { showBottomSheet = it }), adScreenType = adScreenType, - onCancel = { - uploadProgressViewModel.cancelUpload() - closeActivity() - } + onCancel = { uploadProgressViewModel.cancelUpload() } ) } @@ -86,12 +83,14 @@ private fun HandleProgressState( uiState: () -> UploadProgressUiState, navigateToUploadSuccess: (String) -> Unit, navigateToUploadError: () -> Unit, + navigateBackToImportFiles: () -> Unit, ) { val currentUiState = uiState() LaunchedEffect(uiState()) { when (currentUiState) { is UploadProgressUiState.Success -> navigateToUploadSuccess(currentUiState.transferUrl) is UploadProgressUiState.Error -> navigateToUploadError() + is UploadProgressUiState.Cancel -> navigateBackToImportFiles() else -> Unit } } @@ -129,7 +128,15 @@ private fun UploadProgressScreen( Spacer(Modifier.height(Margin.Huge)) } - if (showBottomSheet.get()) CancelUploadBottomSheet(onCancel = onCancel, closeButtonSheet = { showBottomSheet.set(false) }) + if (showBottomSheet.get()) { + CancelUploadBottomSheet( + onCancel = { + onCancel() + showBottomSheet.set(false) + }, + closeButtonSheet = { showBottomSheet.set(false) }, + ) + } } } diff --git a/app/src/main/java/com/infomaniak/swisstransfer/workers/UploadWorker.kt b/app/src/main/java/com/infomaniak/swisstransfer/workers/UploadWorker.kt index 855c6236c..aa115d547 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/workers/UploadWorker.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/workers/UploadWorker.kt @@ -123,7 +123,8 @@ class UploadWorker @AssistedInject constructor( return@mapLatest when (workInfo.state) { State.RUNNING -> UploadProgressUiState.Progress(workInfo.progress).also { lastUploadedSize = it.uploadedSize } State.SUCCEEDED -> UploadProgressUiState.Success.create(workInfo.outputData, sharedApiUrlCreator) - State.FAILED, State.CANCELLED -> UploadProgressUiState.Error(lastUploadedSize) + State.FAILED -> UploadProgressUiState.Error(lastUploadedSize) + State.CANCELLED -> UploadProgressUiState.Cancel() else -> UploadProgressUiState.Default(lastUploadedSize) } ?: UploadProgressUiState.Error(lastUploadedSize) }.filterNotNull() @@ -159,6 +160,9 @@ class UploadWorker @AssistedInject constructor( @Immutable data class Error(override val uploadedSize: Long = 0) : UploadProgressUiState(uploadedSize) + + @Immutable + data class Cancel(override val uploadedSize: Long = 0) : UploadProgressUiState(uploadedSize) } companion object { From 334653f154aa17d5a0b71e521e3bcb14befa6e0b Mon Sep 17 00:00:00 2001 From: Kevin Boulongne Date: Tue, 17 Dec 2024 11:44:17 +0100 Subject: [PATCH 2/3] docs: Add explanatory comment --- .../ui/screen/newtransfer/importfiles/ImportFilesScreen.kt | 4 ++++ 1 file changed, 4 insertions(+) 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 09e6ab034..8960c916b 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 @@ -154,6 +154,10 @@ private fun HandleSendActionResult( LaunchedEffect(getSendActionResult()) { when (val actionResult = getSendActionResult()) { is SendActionResult.Success -> { + // If the user cancels the transfer while in UploadProgress, we're gonna popBackStack to ImportFiles. + // If we don't reset the ImportFiles state machine, we'll automatically navigate-back to UploadProgress again. + // So, before leaving ImportFiles to go to UploadProgress, we need to reset the ImportFiles state machine. + // TODO: Maybe merging the 2 screens state machines into 1 could help simplify this ? resetSendActionResult() navigateToUploadProgress(transferType(), actionResult.totalSize) } From 3d35b668807faf898541036af25ee8ed75931304 Mon Sep 17 00:00:00 2001 From: Kevin Boulongne Date: Tue, 17 Dec 2024 13:01:35 +0100 Subject: [PATCH 3/3] refactor: Int are now Long, as they should be --- .../com/infomaniak/swisstransfer/workers/UploadWorker.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/infomaniak/swisstransfer/workers/UploadWorker.kt b/app/src/main/java/com/infomaniak/swisstransfer/workers/UploadWorker.kt index aa115d547..ad0710b40 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/workers/UploadWorker.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/workers/UploadWorker.kt @@ -138,7 +138,7 @@ class UploadWorker @AssistedInject constructor( sealed class UploadProgressUiState(open val uploadedSize: Long) { @Immutable - data class Default(override val uploadedSize: Long = 0) : UploadProgressUiState(uploadedSize) + data class Default(override val uploadedSize: Long = 0L) : UploadProgressUiState(uploadedSize) @Immutable data class Progress(override val uploadedSize: Long) : UploadProgressUiState(uploadedSize) { @@ -159,10 +159,10 @@ class UploadWorker @AssistedInject constructor( } @Immutable - data class Error(override val uploadedSize: Long = 0) : UploadProgressUiState(uploadedSize) + data class Error(override val uploadedSize: Long = 0L) : UploadProgressUiState(uploadedSize) @Immutable - data class Cancel(override val uploadedSize: Long = 0) : UploadProgressUiState(uploadedSize) + data class Cancel(override val uploadedSize: Long = 0L) : UploadProgressUiState(uploadedSize) } companion object {