Skip to content

Commit

Permalink
AND-8164 return Card dto on addBackupCard
Browse files Browse the repository at this point in the history
  • Loading branch information
kozarezvlad committed Sep 3, 2024
1 parent 5ea65bf commit d48b1db
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class BackupService(
updateState()
}

fun addBackupCard(callback: CompletionCallback<Unit>) {
fun addBackupCard(callback: CompletionCallback<Card>) {
val primaryCard = repo.data.primaryCard.guard {
callback(CompletionResult.Failure(TangemSdkError.MissingPrimaryCard()))
return
Expand Down Expand Up @@ -259,8 +259,12 @@ class BackupService(
return currentState
}

private fun addBackupCard(backupCard: BackupCard, callback: CompletionCallback<Unit>) {
private fun addBackupCard(
backupCardResponse: StartBackupCardLinkingTaskResponse,
callback: CompletionCallback<Card>,
) {
backupScope.launch {
val backupCard = backupCardResponse.backupCard
fetchCertificate(
cardId = backupCard.cardId,
cardPublicKey = backupCard.cardPublicKey,
Expand All @@ -273,12 +277,12 @@ class BackupService(
.plus(updatedBackupCard)
repo.data = repo.data.copy(backupCards = updatedList)
updateState()
callback(CompletionResult.Success(Unit))
callback(CompletionResult.Success(backupCardResponse.card))
}
}
}

private fun readBackupCard(primaryCard: PrimaryCard, callback: CompletionCallback<Unit>) {
private fun readBackupCard(primaryCard: PrimaryCard, callback: CompletionCallback<Card>) {
sdk.startSessionWithRunnable(
runnable = StartBackupCardLinkingTask(
primaryCard = primaryCard,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
package com.tangem.operations.backup

import com.squareup.moshi.JsonClass
import com.tangem.common.CompletionResult
import com.tangem.common.card.Card
import com.tangem.common.core.CardSession
import com.tangem.common.core.CardSessionRunnable
import com.tangem.common.core.CompletionCallback
import com.tangem.common.core.TangemSdkError
import com.tangem.common.extensions.guard

@JsonClass(generateAdapter = true)
data class StartBackupCardLinkingTaskResponse(
val backupCard: BackupCard,
val card: Card,
)

class StartBackupCardLinkingTask(
private val primaryCard: PrimaryCard,
private val addedBackupCards: List<String>,
private val skipCompatibilityChecks: Boolean = false,
) : CardSessionRunnable<BackupCard> {
) : CardSessionRunnable<StartBackupCardLinkingTaskResponse> {

override val allowsRequestAccessCodeFromRepository: Boolean
get() = false

@Suppress("CyclomaticComplexMethod")
override fun run(session: CardSession, callback: CompletionCallback<BackupCard>) {
override fun run(session: CardSession, callback: CompletionCallback<StartBackupCardLinkingTaskResponse>) {
val card = session.environment.card.guard {
callback(CompletionResult.Failure(TangemSdkError.MissingPreflightRead()))
return
Expand Down Expand Up @@ -79,7 +87,13 @@ class StartBackupCardLinkingTask(
.run(session) { result ->
when (result) {
is CompletionResult.Success -> {
callback(CompletionResult.Success(result.data))
session.environment.card?.let { updatedCard ->
val response = StartBackupCardLinkingTaskResponse(
backupCard = result.data,
card = updatedCard,
)
callback(CompletionResult.Success(response))
} ?: callback(CompletionResult.Failure(TangemSdkError.MissingPreflightRead()))
}

is CompletionResult.Failure -> callback(CompletionResult.Failure(result.error))
Expand Down

0 comments on commit d48b1db

Please # to comment.