Skip to content

Commit

Permalink
fix: Call init of UploadSession outside of worker to handle email ver…
Browse files Browse the repository at this point in the history
…ification easily (#246)
  • Loading branch information
tevincent authored Dec 16, 2024
2 parents 568a771 + 9eb92b6 commit 291901f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ImportFilesViewModel @Inject constructor(
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : ViewModel() {

private val _sendActionResult = MutableStateFlow<SendActionResult?>(null)
private val _sendActionResult = MutableStateFlow<SendActionResult?>(SendActionResult.NotStarted)
val sendActionResult = _sendActionResult.asStateFlow()

@OptIn(FlowPreview::class)
Expand Down Expand Up @@ -129,9 +129,11 @@ class ImportFilesViewModel @Inject constructor(
}

fun sendTransfer() {
_sendActionResult.update { SendActionResult.Pending }
viewModelScope.launch(ioDispatcher) {
runCatching {
val uuid = uploadManager.createAndGetUpload(generateNewUploadSession()).uuid
uploadManager.initUploadSession(recaptcha = "Recaptcha")!! // TODO: Handle ContainerErrorsException here
uploadWorkerScheduler.scheduleWork(uuid)
_sendActionResult.update {
val totalSize = importationFilesManager.importedFiles.value.sumOf { it.fileSize }
Expand Down Expand Up @@ -256,6 +258,8 @@ class ImportFilesViewModel @Inject constructor(
//endregion

sealed class SendActionResult {
data object NotStarted : SendActionResult()
data object Pending : SendActionResult()
data class Success(val totalSize: Long) : SendActionResult()
data object Failure : SendActionResult()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ fun ImportFilesScreen(
closeActivity = closeActivity,
shouldStartByPromptingUserForFiles = true,
sendTransfer = importFilesViewModel::sendTransfer,
isTransferStarted = { sendActionResult != SendActionResult.NotStarted },
)
}

Expand Down Expand Up @@ -157,6 +158,7 @@ private fun ImportFilesScreen(
closeActivity: () -> Unit,
shouldStartByPromptingUserForFiles: Boolean,
sendTransfer: () -> Unit,
isTransferStarted: () -> Boolean,
) {

val shouldShowEmailAddressesFields by remember { derivedStateOf { selectedTransferType.get() == TransferTypeUi.MAIL } }
Expand All @@ -177,6 +179,7 @@ private fun ImportFilesScreen(
importedFiles = files,
shouldShowEmailAddressesFields = { shouldShowEmailAddressesFields },
transferAuthorEmail = transferAuthorEmail,
isTransferStarted = isTransferStarted,
navigateToUploadProgress = sendTransfer,
)
},
Expand Down Expand Up @@ -354,6 +357,7 @@ private fun SendButton(
importedFiles: () -> List<FileUi>,
shouldShowEmailAddressesFields: () -> Boolean,
transferAuthorEmail: GetSetCallbacks<String>,
isTransferStarted: () -> Boolean,
navigateToUploadProgress: () -> Unit,
) {
val remainingFilesCount = filesToImportCount()
Expand All @@ -375,7 +379,8 @@ private fun SendButton(
modifier = modifier,
title = stringResource(R.string.transferSendButton),
style = ButtonType.PRIMARY,
enabled = { importedFiles().isNotEmpty() && !isImporting && isSenderEmailCorrect },
enabled = { importedFiles().isNotEmpty() && !isImporting && isSenderEmailCorrect && !isTransferStarted() },
showIndeterminateProgress = { isTransferStarted() },
progress = progress,
onClick = navigateToUploadProgress,
)
Expand Down Expand Up @@ -443,6 +448,7 @@ private fun Preview(@PreviewParameter(FileUiListPreviewParameter::class) files:
addFiles = {},
closeActivity = {},
shouldStartByPromptingUserForFiles = false,
isTransferStarted = { false },
sendTransfer = {},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class UploadWorker @AssistedInject constructor(
return Result.failure()
}

val uploadSession = uploadManager.initUploadSession(recaptcha = "Recaptcha")!!
val uploadSession = uploadManager.getLastUpload() ?: return Result.failure()

val totalSize = uploadSession.files.sumOf { it.size }

SentryLog.d(TAG, "Work started with ${uploadSession.files.count()} files of $totalSize bytes")
Expand Down

0 comments on commit 291901f

Please sign in to comment.