Skip to content

Commit

Permalink
feat: Add resendLastTransfer() function in TransferSend manager
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne authored and LunarX committed Dec 20, 2024
1 parent 38e9ff2 commit 0de9e51
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class ImportFilesViewModel @Inject constructor(

fun sendTransfer() {
viewModelScope.launch(ioDispatcher) {
transferSendManager.sendTransfer(generateNewUploadSession())
transferSendManager.sendNewTransfer(generateNewUploadSession())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,49 +46,57 @@ class TransferSendManager @Inject constructor(
private val uploadWorkerScheduler: UploadWorker.Scheduler,
) {

// TODO: Merge these two ui states in a single one for the whole flow of logic
// TODO: Merge these two UI states in a single one for the whole flow of logic
private val _sendActionResult = MutableStateFlow<SendActionResult?>(SendActionResult.NotStarted)
val sendActionResult = _sendActionResult.asStateFlow()

private val _integrityCheckResult = MutableStateFlow(AppIntegrityResult.Idle)
val integrityCheckResult = _integrityCheckResult.asStateFlow()

suspend fun sendTransfer(newUploadSession: NewUploadSession) {
_integrityCheckResult.value = AppIntegrityResult.Ongoing

withIntegrityToken(
onSuccess = { attestationToken ->
sendTransfer(newUploadSession, attestationToken)
},
onRefused = {
_integrityCheckResult.value = AppIntegrityResult.Fail
},
onFailure = { exception ->
if (exception !is CancellationException) {
SentryLog.e(TAG, "Integrity token received an exception", exception)
} else {
SentryLog.i(TAG, "Integrity token received an exception", exception)
}
_sendActionResult.update { SendActionResult.Failure }
}
)
suspend fun sendNewTransfer(newUploadSession: NewUploadSession) {
val uploadSession = uploadManager.createAndGetUpload(newUploadSession)
sendTransfer(uploadSession.uuid)
}

private suspend fun sendTransfer(newUploadSession: NewUploadSession, attestationToken: String) {
_integrityCheckResult.value = AppIntegrityResult.Success
_sendActionResult.update { SendActionResult.Pending }
suspend fun resendLastTransfer() {
val uploadSessionUuid = uploadManager.getLastUpload()?.uuid ?: run {
SentryLog.e(TAG, "No last upload found")
return
}
sendTransfer(uploadSessionUuid)
}

private suspend fun sendTransfer(uploadSessionUuid: String) {
runCatching {
val uuid = uploadManager.createAndGetUpload(newUploadSession).uuid
uploadManager.initUploadSession(
attestationHeaderName = AppIntegrityManager.ATTESTATION_TOKEN_HEADER,
attestationToken = attestationToken,
)!! // TODO: Handle ContainerErrorsException here
uploadWorkerScheduler.scheduleWork(uuid)
_sendActionResult.update {
val totalSize = importationFilesManager.importedFiles.value.sumOf { it.fileSize }
SendActionResult.Success(totalSize)
}
_integrityCheckResult.value = AppIntegrityResult.Ongoing

withIntegrityToken(
onSuccess = { attestationToken ->
_integrityCheckResult.value = AppIntegrityResult.Success
_sendActionResult.update { SendActionResult.Pending }

uploadManager.initUploadSession(
attestationHeaderName = AppIntegrityManager.ATTESTATION_TOKEN_HEADER,
attestationToken = attestationToken,
)!! // TODO: Handle ContainerErrorsException here
uploadWorkerScheduler.scheduleWork(uploadSessionUuid)
_sendActionResult.update {
val totalSize = importationFilesManager.importedFiles.value.sumOf { it.fileSize }
SendActionResult.Success(totalSize)
}
},
onRefused = {
_integrityCheckResult.value = AppIntegrityResult.Fail
},
onFailure = { exception ->
if (exception !is CancellationException) {
SentryLog.e(TAG, "Integrity token received an exception", exception)
} else {
SentryLog.i(TAG, "Integrity token received an exception", exception)
}
_sendActionResult.update { SendActionResult.Failure }
},
)
}.onFailure { exception ->
SentryLog.e(TAG, "Failed to start the upload", exception)
_sendActionResult.update { SendActionResult.Failure }
Expand Down

0 comments on commit 0de9e51

Please sign in to comment.