Skip to content

Commit 7e48141

Browse files
committed
chore: replace direct decode calls with CoreService decode method
1 parent f1a704a commit 7e48141

4 files changed

Lines changed: 19 additions & 18 deletions

File tree

app/src/main/java/to/bitkit/repositories/LightningRepo.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import com.synonym.bitkitcore.PreActivityMetadata
88
import com.synonym.bitkitcore.Scanner
99
import com.synonym.bitkitcore.createChannelRequestUrl
1010
import com.synonym.bitkitcore.createWithdrawCallbackUrl
11-
import com.synonym.bitkitcore.decode
1211
import com.synonym.bitkitcore.lnurlAuth
1312
import kotlinx.coroutines.CoroutineDispatcher
1413
import kotlinx.coroutines.CoroutineScope
@@ -552,7 +551,7 @@ class LightningRepo @Inject constructor(
552551
return runCatching {
553552
// TODO use bitkit-core getLnurlInvoice if it works with callbackUrl
554553
val bolt11 = lnurlService.fetchLnurlInvoice(callbackUrl, amountSats, comment).getOrThrow().pr
555-
val decoded = (decode(bolt11) as Scanner.Lightning).invoice
554+
val decoded = (coreService.decode(bolt11) as Scanner.Lightning).invoice
556555
return@runCatching decoded
557556
}.onFailure {
558557
Logger.error(

app/src/main/java/to/bitkit/repositories/WalletRepo.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package to.bitkit.repositories
33
import com.synonym.bitkitcore.AddressType
44
import com.synonym.bitkitcore.PreActivityMetadata
55
import com.synonym.bitkitcore.Scanner
6-
import com.synonym.bitkitcore.decode
76
import kotlinx.coroutines.CoroutineDispatcher
87
import kotlinx.coroutines.CoroutineScope
98
import kotlinx.coroutines.Job
@@ -137,7 +136,7 @@ class WalletRepo @Inject constructor(
137136
) {
138137
val onChainAddress = getOnchainAddress()
139138
val paymentHash = runCatching {
140-
when (val decoded = decode(bip21Url)) {
139+
when (val decoded = coreService.decode(bip21Url)) {
141140
is Scanner.Lightning -> decoded.invoice.paymentHash.toHex()
142141
is Scanner.OnChain -> decoded.extractLightningHash()
143142
else -> null
@@ -418,7 +417,7 @@ class WalletRepo @Inject constructor(
418417
if (bolt11.isEmpty()) return@withContext null
419418

420419
runCatching {
421-
when (val decoded = decode(bolt11)) {
420+
when (val decoded = coreService.decode(bolt11)) {
422421
is Scanner.Lightning -> decoded.invoice.paymentHash.toHex()
423422
else -> null
424423
}
@@ -544,7 +543,7 @@ class WalletRepo @Inject constructor(
544543
private suspend fun Scanner.OnChain.extractLightningHash(): String? {
545544
val lightningInvoice: String = this.invoice.params?.get("lightning") ?: return null
546545

547-
return when (val decoded = decode(lightningInvoice)) {
546+
return when (val decoded = coreService.decode(lightningInvoice)) {
548547
is Scanner.Lightning -> decoded.invoice.paymentHash.toHex()
549548
else -> null
550549
}

app/src/main/java/to/bitkit/ui/utils/AutoReadClipboardHandler.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import androidx.lifecycle.Lifecycle
1616
import androidx.lifecycle.LifecycleEventObserver
1717
import androidx.lifecycle.compose.LocalLifecycleOwner
1818
import androidx.lifecycle.compose.collectAsStateWithLifecycle
19-
import com.synonym.bitkitcore.decode
2019
import kotlinx.coroutines.delay
2120
import kotlinx.coroutines.launch
2221
import to.bitkit.R
2322
import to.bitkit.ext.getClipboardText
2423
import to.bitkit.ui.appViewModel
2524
import to.bitkit.ui.scaffold.AppAlertDialog
2625
import to.bitkit.ui.settingsViewModel
26+
import to.bitkit.viewmodels.AppViewModel
2727

2828
@Composable
2929
fun AutoReadClipboardHandler() {
@@ -43,7 +43,7 @@ fun AutoReadClipboardHandler() {
4343
// Check clipboard on app startup - only after authentication
4444
LaunchedEffect(isAuthenticated, isAutoReadClipboardEnabled) {
4545
if (isAuthenticated && isAutoReadClipboardEnabled && !hasCheckedOnStartup) {
46-
showClipboardDialog = context.hasScanDataInClipboard()
46+
showClipboardDialog = hasScanDataInClipboard(context, appViewModel)
4747
hasCheckedOnStartup = true
4848
}
4949
}
@@ -60,7 +60,7 @@ fun AutoReadClipboardHandler() {
6060
if (event == Lifecycle.Event.ON_RESUME && isAuthenticated) {
6161
if (hasCheckedOnStartup && isAutoReadClipboardEnabled) {
6262
scope.launch {
63-
showClipboardDialog = context.hasScanDataInClipboard()
63+
showClipboardDialog = hasScanDataInClipboard(context, appViewModel)
6464
}
6565
}
6666
}
@@ -86,12 +86,11 @@ fun AutoReadClipboardHandler() {
8686
}
8787
}
8888

89-
private suspend fun Context.hasScanDataInClipboard(): Boolean {
89+
private suspend fun hasScanDataInClipboard(context: Context, appViewModel: AppViewModel): Boolean {
9090
delay(1000) // delay needed for Android clipboard accessibility on start
9191

92-
val clipText = this.getClipboardText()
92+
val clipText = context.getClipboardText()
9393
if (clipText.isNullOrBlank()) return false
9494

95-
val scanResult = runCatching { decode(clipText) }
96-
return scanResult.isSuccess
95+
return appViewModel.canDecodeClipboard(clipText)
9796
}

app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import com.synonym.bitkitcore.LnurlWithdrawData
2222
import com.synonym.bitkitcore.OnChainInvoice
2323
import com.synonym.bitkitcore.PaymentType
2424
import com.synonym.bitkitcore.Scanner
25-
import com.synonym.bitkitcore.decode
2625
import com.synonym.bitkitcore.validateBitcoinAddress
2726
import dagger.hilt.android.lifecycle.HiltViewModel
2827
import dagger.hilt.android.qualifiers.ApplicationContext
@@ -101,6 +100,7 @@ import to.bitkit.repositories.SweepRepo
101100
import to.bitkit.repositories.TransferRepo
102101
import to.bitkit.repositories.WalletRepo
103102
import to.bitkit.services.AppUpdaterService
103+
import to.bitkit.services.CoreService
104104
import to.bitkit.services.MigrationService
105105
import to.bitkit.ui.Routes
106106
import to.bitkit.ui.components.Sheet
@@ -149,6 +149,7 @@ class AppViewModel @Inject constructor(
149149
private val transferRepo: TransferRepo,
150150
private val migrationService: MigrationService,
151151
private val sweepRepo: SweepRepo,
152+
private val coreService: CoreService,
152153
private val appUpdateSheet: AppUpdateTimedSheet,
153154
private val backupSheet: BackupTimedSheet,
154155
private val notificationsSheet: NotificationsTimedSheet,
@@ -715,7 +716,7 @@ class AppViewModel @Inject constructor(
715716
return@withContext
716717
}
717718

718-
val scanResult = runCatching { decode(input) }
719+
val scanResult = runCatching { coreService.decode(input) }
719720

720721
if (scanResult.isFailure) {
721722
showAddressValidationError(
@@ -819,7 +820,7 @@ class AppViewModel @Inject constructor(
819820

820821
private suspend fun extractViableLightningInvoice(params: Map<String, String>?): LightningInvoice? =
821822
params?.get("lightning")?.let { bolt11 ->
822-
runCatching { decode(bolt11) }.getOrNull()
823+
runCatching { coreService.decode(bolt11) }.getOrNull()
823824
?.let { it as? Scanner.Lightning }
824825
?.invoice
825826
?.takeIf { lnInv ->
@@ -1043,8 +1044,7 @@ class AppViewModel @Inject constructor(
10431044
return@withContext
10441045
}
10451046

1046-
@Suppress("ForbiddenComment") // TODO: wrap `decode` from bindings in a `CoreService` method and call that one
1047-
val scan = runCatching { decode(result) }
1047+
val scan = runCatching { coreService.decode(result) }
10481048
.onFailure { Logger.error("Failed to decode scan data: '$result'", it, context = TAG) }
10491049
.onSuccess { Logger.info("Handling decoded scan data: $it", context = TAG) }
10501050
.getOrNull()
@@ -2111,6 +2111,10 @@ class AppViewModel @Inject constructor(
21112111
}
21122112
// endregion
21132113

2114+
suspend fun canDecodeClipboard(text: String): Boolean = withContext(bgDispatcher) {
2115+
runCatching { coreService.decode(text) }.isSuccess
2116+
}
2117+
21142118
fun onClipboardAutoRead(data: String) {
21152119
viewModelScope.launch {
21162120
mainScreenEffect(MainScreenEffect.ProcessClipboardAutoRead(data))

0 commit comments

Comments
 (0)