Skip to content

Commit e70592c

Browse files
authored
Merge pull request #535 from synonymdev/fix/cjit-autoscroll
fix: cjit auto scroll and QR screen state update improvements
2 parents 9f7e7e6 + d3dcb3f commit e70592c

File tree

9 files changed

+28
-109
lines changed

9 files changed

+28
-109
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class BlocktankRepo @Inject constructor(
201201
description: String = Env.DEFAULT_INVOICE_MESSAGE,
202202
): Result<IcJitEntry> = withContext(bgDispatcher) {
203203
try {
204-
if (coreService.checkGeoBlock().first) throw ServiceError.GeoBlocked
204+
if (coreService.isGeoBlocked()) throw ServiceError.GeoBlocked
205205
val nodeId = lightningService.nodeId ?: throw ServiceError.NodeNotStarted
206206
val lspBalance = getDefaultLspBalance(clientBalance = amountSats)
207207
val channelSizeSat = amountSats + lspBalance
@@ -230,7 +230,7 @@ class BlocktankRepo @Inject constructor(
230230
channelExpiryWeeks: UInt = DEFAULT_CHANNEL_EXPIRY_WEEKS,
231231
): Result<IBtOrder> = withContext(bgDispatcher) {
232232
try {
233-
if (coreService.checkGeoBlock().first) throw ServiceError.GeoBlocked
233+
if (coreService.isGeoBlocked()) throw ServiceError.GeoBlocked
234234

235235
val options = defaultCreateOrderOptions(clientBalanceSat = spendingBalanceSats)
236236

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import org.lightningdevkit.ldknode.PaymentDetails
3737
import org.lightningdevkit.ldknode.PaymentId
3838
import org.lightningdevkit.ldknode.PeerDetails
3939
import org.lightningdevkit.ldknode.SpendableUtxo
40-
import org.lightningdevkit.ldknode.TransactionDetails
4140
import org.lightningdevkit.ldknode.Txid
4241
import to.bitkit.data.CacheStore
4342
import to.bitkit.data.SettingsStore
@@ -283,9 +282,8 @@ class LightningRepo @Inject constructor(
283282
}
284283

285284
suspend fun updateGeoBlockState() = withContext(bgDispatcher) {
286-
val (isGeoBlocked, shouldBlockLightning) = coreService.checkGeoBlock()
287285
_lightningState.update {
288-
it.copy(isGeoBlocked = isGeoBlocked, shouldBlockLightningReceive = shouldBlockLightning)
286+
it.copy(isGeoBlocked = coreService.isGeoBlocked())
289287
}
290288
}
291289

@@ -355,7 +353,8 @@ class LightningRepo @Inject constructor(
355353
private fun handleLdkEvent(event: Event) {
356354
when (event) {
357355
is Event.ChannelPending,
358-
is Event.ChannelReady -> scope.launch {
356+
is Event.ChannelReady,
357+
-> scope.launch {
359358
refreshChannelCache()
360359
}
361360

@@ -560,10 +559,6 @@ class LightningRepo @Inject constructor(
560559
expirySeconds: UInt = 86_400u,
561560
): Result<String> = executeWhenNodeRunning("Create invoice") {
562561
updateGeoBlockState()
563-
if (lightningState.value.shouldBlockLightningReceive) {
564-
return@executeWhenNodeRunning Result.failure(ServiceError.GeoBlocked)
565-
}
566-
567562
val invoice = lightningService.receive(amountSats, description, expirySeconds)
568563
Result.success(invoice)
569564
}
@@ -1016,7 +1011,6 @@ data class LightningState(
10161011
val channels: List<ChannelDetails> = emptyList(),
10171012
val balances: BalanceDetails? = null,
10181013
val isSyncingWallet: Boolean = false,
1019-
val shouldBlockLightningReceive: Boolean = false,
10201014
val isGeoBlocked: Boolean = false,
10211015
) {
10221016
fun block(): BestBlock? = nodeStatus?.currentBestBlock

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

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ class WalletRepo @Inject constructor(
121121
emptyList()
122122
}
123123

124-
val (_, shouldBlockLightningReceive) = coreService.checkGeoBlock()
125-
_walletState.update {
126-
it.copy(receiveOnSpendingBalance = !shouldBlockLightningReceive)
127-
}
128124
clearBip21State(clearTags = false)
129125
refreshAddressIfNeeded()
130126
updateBip21Invoice()
@@ -429,16 +425,6 @@ class WalletRepo @Inject constructor(
429425
}
430426
}
431427

432-
suspend fun toggleReceiveOnSpendingBalance(): Result<Unit> = withContext(bgDispatcher) {
433-
if (!_walletState.value.receiveOnSpendingBalance && coreService.checkGeoBlock().second) {
434-
return@withContext Result.failure(ServiceError.GeoBlocked)
435-
}
436-
437-
_walletState.update { it.copy(receiveOnSpendingBalance = !it.receiveOnSpendingBalance) }
438-
439-
return@withContext Result.success(Unit)
440-
}
441-
442428
// Payment ID management
443429
private suspend fun paymentHash(): String? = withContext(bgDispatcher) {
444430
val bolt11 = getBolt11()
@@ -553,7 +539,7 @@ class WalletRepo @Inject constructor(
553539
setBip21Description(description)
554540

555541
val canReceive = lightningRepo.canReceive()
556-
if (canReceive && _walletState.value.receiveOnSpendingBalance) {
542+
if (canReceive) {
557543
lightningRepo.createInvoice(amountSats, description).onSuccess {
558544
setBolt11(it)
559545
}
@@ -575,9 +561,7 @@ class WalletRepo @Inject constructor(
575561

576562
suspend fun shouldRequestAdditionalLiquidity(): Result<Boolean> = withContext(bgDispatcher) {
577563
return@withContext try {
578-
if (!_walletState.value.receiveOnSpendingBalance) return@withContext Result.success(false)
579-
580-
if (coreService.checkGeoBlock().first) return@withContext Result.success(false)
564+
if (coreService.isGeoBlocked()) return@withContext Result.success(false)
581565

582566
val channels = lightningRepo.lightningState.value.channels
583567
if (channels.filterOpen().isEmpty()) return@withContext Result.success(false)
@@ -617,7 +601,6 @@ data class WalletState(
617601
val bip21AmountSats: ULong? = null,
618602
val bip21Description: String = "",
619603
val selectedTags: List<String> = listOf(),
620-
val receiveOnSpendingBalance: Boolean = true,
621604
val walletExists: Boolean = false,
622605
)
623606

app/src/main/java/to/bitkit/services/CoreService.kt

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class CoreService @Inject constructor(
135135
}
136136

137137
@Suppress("KotlinConstantConditions")
138-
private suspend fun isGeoBlocked(): Boolean {
138+
suspend fun isGeoBlocked(): Boolean {
139139
if (!Env.isGeoblockingEnabled) {
140140
Logger.verbose("Geoblocking disabled via build config", context = "GeoCheck")
141141
return false
@@ -171,20 +171,6 @@ class CoreService @Inject constructor(
171171
}
172172
}
173173

174-
/**
175-
* This method checks if the device is in a is geo blocked region and if lightning features should be blocked
176-
* @return pair of `isGeoBlocked` to `shouldBlockLightningReceive`
177-
* */
178-
suspend fun checkGeoBlock(): Pair<Boolean, Boolean> {
179-
val geoBlocked = isGeoBlocked()
180-
val shouldBlockLightningReceive = when {
181-
lightningService.hasExternalPeers() -> !lightningService.canReceive()
182-
else -> geoBlocked
183-
}
184-
185-
return Pair(geoBlocked, shouldBlockLightningReceive)
186-
}
187-
188174
suspend fun wipeData(): Result<Unit> = ServiceQueue.CORE.background {
189175
runCatching {
190176
val result = wipeAllDatabases()
@@ -1182,6 +1168,7 @@ class ActivityService(
11821168
// a closed channel's funding UTXO
11831169
findClosedChannelForTransaction(txid, transactionDetails)
11841170
}
1171+
11851172
PaymentDirection.OUTBOUND -> {
11861173
// Check if this transaction is a channel open by checking if it's
11871174
// the funding transaction for an open channel

app/src/main/java/to/bitkit/services/LightningService.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ class LightningService @Inject constructor(
739739
/**
740740
* TODO remove, replace all usages with [FeeRate.fromSatPerVbUnchecked]
741741
* */
742+
@Deprecated("replace all usages with [FeeRate.fromSatPerVbUnchecked]")
742743
private fun convertVByteToKwu(satsPerVByte: UInt): FeeRate {
743744
// 1 vbyte = 4 weight units, so 1 sats/vbyte = 250 sats/kwu
744745
val satPerKwu = satsPerVByte.toULong() * 250u

app/src/main/java/to/bitkit/ui/screens/wallets/receive/ReceiveQrScreen.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fun ReceiveQrScreen(
9393
SetMaxBrightness()
9494

9595
val haptic = LocalHapticFeedback.current
96-
val hasUsableChannels = walletState.channels.any { it.isUsable }
96+
val hasUsableChannels = walletState.channels.any { it.isChannelReady }
9797

9898
var showDetails by remember { mutableStateOf(false) }
9999

@@ -163,6 +163,17 @@ fun ReceiveQrScreen(
163163
}
164164
}
165165

166+
// Auto-switch to Spending tab when CJIT is not null
167+
LaunchedEffect(cjitInvoice) {
168+
if (cjitInvoice != null) {
169+
val spendingIndex = visibleTabs.indexOf(ReceiveTab.SPENDING)
170+
if (spendingIndex != -1) {
171+
lazyListState.animateScrollToItem(spendingIndex)
172+
selectedTab = ReceiveTab.SPENDING
173+
}
174+
}
175+
}
176+
166177
val showingCjitOnboarding = remember(walletState, cjitInvoice, hasUsableChannels) {
167178
!hasUsableChannels &&
168179
walletState.nodeLifecycleState.isRunning() &&

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ class WalletViewModel @Inject constructor(
8585
bip21AmountSats = state.bip21AmountSats,
8686
bip21Description = state.bip21Description,
8787
selectedTags = state.selectedTags,
88-
receiveOnSpendingBalance = state.receiveOnSpendingBalance,
8988
)
9089
}
9190
if (state.walletExists && restoreState == RestoreState.InProgress.Wallet) {
@@ -306,7 +305,6 @@ data class MainUiState(
306305
val peers: List<PeerDetails> = emptyList(),
307306
val channels: List<ChannelDetails> = emptyList(),
308307
val isRefreshing: Boolean = false,
309-
val receiveOnSpendingBalance: Boolean = true,
310308
val bip21AmountSats: ULong? = null,
311309
val bip21Description: String = "",
312310
val selectedTags: List<String> = listOf(),

app/src/test/java/to/bitkit/repositories/LightningRepoTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class LightningRepoTest : BaseUnitTest() {
6363

6464
@Before
6565
fun setUp() {
66-
wheneverBlocking { coreService.checkGeoBlock() }.thenReturn(Pair(false, false))
66+
wheneverBlocking { coreService.isGeoBlocked() }.thenReturn(false)
6767
sut = LightningRepo(
6868
bgDispatcher = testDispatcher,
6969
lightningService = lightningService,

app/src/test/java/to/bitkit/repositories/WalletRepoTest.kt

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class WalletRepoTest : BaseUnitTest() {
7373

7474
@Before
7575
fun setUp() = runBlocking {
76-
whenever(coreService.checkGeoBlock()).thenReturn(Pair(false, false))
76+
whenever(coreService.isGeoBlocked()).thenReturn(false)
7777
whenever(cacheStore.data).thenReturn(flowOf(AppCacheData(bolt11 = "", onchainAddress = ADDRESS)))
7878
whenever(lightningRepo.lightningState).thenReturn(MutableStateFlow(LightningState()))
7979
whenever(lightningRepo.nodeEvents).thenReturn(MutableSharedFlow())
@@ -176,28 +176,6 @@ class WalletRepoTest : BaseUnitTest() {
176176
verify(lightningRepo).newAddress()
177177
}
178178

179-
@Test
180-
fun `refreshBip21 should set receiveOnSpendingBalance false when shouldBlockLightning is true`() = test {
181-
whenever(coreService.checkGeoBlock()).thenReturn(Pair(true, true))
182-
whenever(lightningRepo.newAddress()).thenReturn(Result.success(ADDRESS_NEW))
183-
184-
val result = sut.refreshBip21()
185-
186-
assertTrue(result.isSuccess)
187-
assertEquals(false, sut.walletState.value.receiveOnSpendingBalance)
188-
}
189-
190-
@Test
191-
fun `refreshBip21 should set receiveOnSpendingBalance true when shouldBlockLightning is false`() = test {
192-
whenever(coreService.checkGeoBlock()).thenReturn(Pair(true, false))
193-
whenever(lightningRepo.newAddress()).thenReturn(Result.success(ADDRESS_NEW))
194-
195-
val result = sut.refreshBip21()
196-
197-
assertTrue(result.isSuccess)
198-
assertEquals(true, sut.walletState.value.receiveOnSpendingBalance)
199-
}
200-
201179
@Test
202180
fun `refreshBip21 should generate new address when current has transactions`() = test {
203181
whenever(cacheStore.data).thenReturn(flowOf(AppCacheData(onchainAddress = ADDRESS)))
@@ -333,28 +311,6 @@ class WalletRepoTest : BaseUnitTest() {
333311
assertTrue(result.contains("lightning=testInvoice"))
334312
}
335313

336-
@Test
337-
fun `toggleReceiveOnSpendingBalance should toggle state`() = test {
338-
val initialValue = sut.walletState.value.receiveOnSpendingBalance
339-
340-
sut.toggleReceiveOnSpendingBalance()
341-
342-
assertEquals(!initialValue, sut.walletState.value.receiveOnSpendingBalance)
343-
}
344-
345-
@Test
346-
fun `toggleReceiveOnSpendingBalance should return failure if shouldBlockLightning is true`() = test {
347-
whenever(coreService.checkGeoBlock()).thenReturn(Pair(true, true))
348-
349-
if (sut.walletState.value.receiveOnSpendingBalance) {
350-
sut.toggleReceiveOnSpendingBalance()
351-
}
352-
353-
val result = sut.toggleReceiveOnSpendingBalance()
354-
355-
assert(result.isFailure)
356-
}
357-
358314
@Test
359315
fun `addTagToSelected should add tag and update lastUsedTags`() = test {
360316
// Set address in wallet state so paymentId() returns it
@@ -428,20 +384,9 @@ class WalletRepoTest : BaseUnitTest() {
428384
assertEquals(error, result.exceptionOrNull())
429385
}
430386

431-
@Test
432-
fun `shouldRequestAdditionalLiquidity should return false when geoBlocked is true`() = test {
433-
whenever(coreService.checkGeoBlock()).thenReturn(Pair(true, false))
434-
sut.toggleReceiveOnSpendingBalance() // Set to false (initial is true)
435-
436-
val result = sut.shouldRequestAdditionalLiquidity()
437-
438-
assertTrue(result.isSuccess)
439-
assertFalse(result.getOrThrow())
440-
}
441-
442387
@Test
443388
fun `shouldRequestAdditionalLiquidity should return false when geo status is true`() = test {
444-
whenever(coreService.checkGeoBlock()).thenReturn(Pair(true, false))
389+
whenever(coreService.isGeoBlocked()).thenReturn(true)
445390

446391
val result = sut.shouldRequestAdditionalLiquidity()
447392

@@ -451,7 +396,7 @@ class WalletRepoTest : BaseUnitTest() {
451396

452397
@Test
453398
fun `shouldRequestAdditionalLiquidity should return true when amount exceeds inbound capacity`() = test {
454-
whenever(coreService.checkGeoBlock()).thenReturn(Pair(false, false))
399+
whenever(coreService.isGeoBlocked()).thenReturn(false)
455400
whenever(lightningRepo.lightningState).thenReturn(MutableStateFlow(LightningState(channels = channels)))
456401
sut.updateBip21Invoice(amountSats = 1000uL)
457402

@@ -463,7 +408,7 @@ class WalletRepoTest : BaseUnitTest() {
463408

464409
@Test
465410
fun `should not request additional liquidity for 0 channels`() = test {
466-
whenever(coreService.checkGeoBlock()).thenReturn(Pair(false, false))
411+
whenever(coreService.isGeoBlocked()).thenReturn(false)
467412
whenever(lightningRepo.lightningState).thenReturn(MutableStateFlow(LightningState()))
468413
sut.updateBip21Invoice(amountSats = 1000uL)
469414

@@ -475,7 +420,7 @@ class WalletRepoTest : BaseUnitTest() {
475420

476421
@Test
477422
fun `shouldRequestAdditionalLiquidity should return false when amount is less than inbound capacity`() = test {
478-
whenever(coreService.checkGeoBlock()).thenReturn(Pair(false, false))
423+
whenever(coreService.isGeoBlocked()).thenReturn(false)
479424
whenever(lightningRepo.lightningState).thenReturn(MutableStateFlow(LightningState(channels = channels)))
480425
sut.updateBip21Invoice(amountSats = 900uL)
481426

0 commit comments

Comments
 (0)