@@ -37,10 +37,12 @@ import com.duckduckgo.sync.impl.QREncoder
37
37
import com.duckduckgo.sync.impl.R
38
38
import com.duckduckgo.sync.impl.R.dimen
39
39
import com.duckduckgo.sync.impl.R.string
40
+ import com.duckduckgo.sync.impl.Result
40
41
import com.duckduckgo.sync.impl.Result.Error
41
42
import com.duckduckgo.sync.impl.Result.Success
42
43
import com.duckduckgo.sync.impl.SyncAccountRepository
43
44
import com.duckduckgo.sync.impl.SyncFeature
45
+ import com.duckduckgo.sync.impl.getOrNull
44
46
import com.duckduckgo.sync.impl.onFailure
45
47
import com.duckduckgo.sync.impl.onSuccess
46
48
import com.duckduckgo.sync.impl.pixels.SyncPixels
@@ -52,6 +54,10 @@ import com.duckduckgo.sync.impl.ui.SyncWithAnotherActivityViewModel.Command.Read
52
54
import com.duckduckgo.sync.impl.ui.SyncWithAnotherActivityViewModel.Command.ShowError
53
55
import com.duckduckgo.sync.impl.ui.SyncWithAnotherActivityViewModel.Command.ShowMessage
54
56
import com.duckduckgo.sync.impl.ui.SyncWithAnotherActivityViewModel.Command.SwitchAccountSuccess
57
+ import com.duckduckgo.sync.impl.ui.qrcode.SyncBarcodeDecorator
58
+ import com.duckduckgo.sync.impl.ui.qrcode.SyncBarcodeDecorator.CodeType
59
+ import com.duckduckgo.sync.impl.ui.qrcode.SyncBarcodeDecorator.CodeType.Exchange
60
+ import com.duckduckgo.sync.impl.ui.qrcode.SyncBarcodeDecorator.CodeType.Recovery
55
61
import com.duckduckgo.sync.impl.ui.setup.EnterCodeContract.EnterCodeContractOutput
56
62
import javax.inject.Inject
57
63
import kotlinx.coroutines.channels.BufferOverflow.DROP_OLDEST
@@ -73,6 +79,7 @@ class SyncWithAnotherActivityViewModel @Inject constructor(
73
79
private val syncPixels : SyncPixels ,
74
80
private val dispatchers : DispatcherProvider ,
75
81
private val syncFeature : SyncFeature ,
82
+ private val urlDecorator : SyncBarcodeDecorator ,
76
83
) : ViewModel() {
77
84
private val command = Channel <Command >(1 , DROP_OLDEST )
78
85
fun commands (): Flow <Command > = command.receiveAsFlow()
@@ -107,18 +114,28 @@ class SyncWithAnotherActivityViewModel @Inject constructor(
107
114
}
108
115
}
109
116
117
+ // get the code as a Result, and pair it with the type of code we're dealing with
118
+ private fun getCode (): Pair <Result <String >, CodeType> {
119
+ return if (! syncFeature.exchangeKeysToSyncWithAnotherDevice().isEnabled()) {
120
+ Pair (syncAccountRepository.getRecoveryCode(), Recovery )
121
+ } else {
122
+ Pair (syncAccountRepository.generateExchangeInvitationCode(), Exchange )
123
+ }
124
+ }
125
+
110
126
private suspend fun showQRCode () {
111
- val shouldExchangeKeysToSyncAnotherDevice = syncFeature.exchangeKeysToSyncWithAnotherDevice().isEnabled()
127
+ val (result, codeType) = getCode()
128
+
129
+ result.onSuccess { code ->
130
+ // wrap the code inside a URL if feature flag allows it
131
+ val barcodeString = urlDecorator.decorateCode(code, codeType)
132
+
133
+ barcodeContents = barcodeString
112
134
113
- if (! shouldExchangeKeysToSyncAnotherDevice) {
114
- syncAccountRepository.getRecoveryCode()
115
- } else {
116
- syncAccountRepository.generateExchangeInvitationCode()
117
- }.onSuccess { connectQR ->
118
- barcodeContents = connectQR
119
135
val qrBitmap = withContext(dispatchers.io()) {
120
- qrEncoder.encodeAsBitmap(connectQR , dimen.qrSizeSmall, dimen.qrSizeSmall)
136
+ qrEncoder.encodeAsBitmap(barcodeString , dimen.qrSizeSmall, dimen.qrSizeSmall)
121
137
}
138
+
122
139
viewState.emit(viewState.value.copy(qrCodeBitmap = qrBitmap))
123
140
}.onFailure {
124
141
command.send(Command .FinishWithError )
@@ -133,8 +150,8 @@ class SyncWithAnotherActivityViewModel @Inject constructor(
133
150
134
151
fun onCopyCodeClicked () {
135
152
viewModelScope.launch(dispatchers.io()) {
136
- barcodeContents ?.let { code ->
137
- clipboard.copyToClipboard(code )
153
+ getCode().first.getOrNull() ?.let {
154
+ clipboard.copyToClipboard(it )
138
155
command.send(ShowMessage (string.sync_code_copied_message))
139
156
} ? : command.send(FinishWithError )
140
157
}
0 commit comments