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

feat: When cancelling the uploading transfer, we're going back to ImportFiles instead of closing Activity #257

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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 @@ -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<UploadSuccessDestination> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,14 @@ private fun HandleSendActionResult(

LaunchedEffect(getSendActionResult()) {
when (val actionResult = getSendActionResult()) {
is SendActionResult.Success -> navigateToUploadProgress(transferType(), actionResult.totalSize)
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)
}
is SendActionResult.Failure -> {
snackbarHostState.showSnackbar(context.getString(R.string.errorUnknown))
resetSendActionResult()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fun UploadProgressScreen(
totalSizeInBytes: Long,
navigateToUploadSuccess: (String) -> Unit,
navigateToUploadError: () -> Unit,
closeActivity: () -> Unit,
navigateBackToImportFiles: () -> Unit,
uploadProgressViewModel: UploadProgressViewModel = hiltViewModel<UploadProgressViewModel>(),
) {
val uiState by uploadProgressViewModel.transferProgressUiState.collectAsStateWithLifecycle()
Expand All @@ -66,18 +66,15 @@ fun UploadProgressScreen(
uploadProgressViewModel.trackUploadProgress()
}

HandleProgressState({ uiState }, navigateToUploadSuccess, navigateToUploadError)
HandleProgressState({ uiState }, navigateToUploadSuccess, navigateToUploadError, navigateBackToImportFiles)

UploadProgressScreen(
progressState = { uiState },
isNetworkAvailable = { isNetworkAvailable },
totalSizeInBytes = totalSizeInBytes,
showBottomSheet = GetSetCallbacks(get = { showBottomSheet }, set = { showBottomSheet = it }),
adScreenType = adScreenType,
onCancel = {
uploadProgressViewModel.cancelUpload()
closeActivity()
}
onCancel = { uploadProgressViewModel.cancelUpload() }
)
}

Expand All @@ -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
}
}
Expand Down Expand Up @@ -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) },
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -137,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) {
Expand All @@ -158,7 +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 = 0L) : UploadProgressUiState(uploadedSize)
}

companion object {
Expand Down
Loading