Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Bitkit/Components/TabBar/TabBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import SwiftUI
struct TabBar: View {
@EnvironmentObject var navigation: NavigationViewModel
@EnvironmentObject var sheets: SheetViewModel
@EnvironmentObject var wallet: WalletViewModel

var shouldShow: Bool {
if navigation.activeDrawerMenuItem == .wallet || navigation.activeDrawerMenuItem == .activity {
Expand Down Expand Up @@ -54,7 +55,11 @@ struct TabBar: View {
}

private func onReceivePress() {
if navigation.currentRoute == .spendingWallet {
let hasInboundCapacity = (wallet.totalInboundLightningSats ?? 0) > 0
let hasPendingTransfersToSpending = wallet.balanceInTransferToSpending > 0

if navigation.currentRoute == .spendingWallet && !hasInboundCapacity && !hasPendingTransfersToSpending {
// On spending wallet screen, show CJIT flow when user can't receive normally
sheets.showSheet(.receive, data: ReceiveConfig(view: .cjitAmount))
} else {
sheets.showSheet(.receive)
Expand Down
10 changes: 10 additions & 0 deletions Bitkit/Services/LightningService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@
}

func closeChannel(_ channel: ChannelDetails, force: Bool = false, forceCloseReason: String? = nil) async throws {
guard let node else {

Check warning on line 524 in Bitkit/Services/LightningService.swift

View workflow job for this annotation

GitHub Actions / Run Tests

value 'node' was defined but never used; consider replacing with boolean test

Check warning on line 524 in Bitkit/Services/LightningService.swift

View workflow job for this annotation

GitHub Actions / Run Integration Tests

value 'node' was defined but never used; consider replacing with boolean test
throw AppError(serviceError: .nodeNotStarted)
}

Expand Down Expand Up @@ -728,6 +728,16 @@
}
return (trusted: trusted, nonTrusted: nonTrusted)
}

/// Get a channel by ID from the channel cache
/// This is more reliable than using the cachedChannels array when handling events,
/// as the channelCache is updated immediately when channel events occur
func getChannelFromCache(channelId: ChannelId) async -> ChannelDetails? {
let channelIdString = channelId.description
return await MainActor.run {
channelCache[channelIdString]
}
}
}

// MARK: Events
Expand All @@ -752,7 +762,7 @@
onEvent?(event)

switch event {
case let .paymentSuccessful(paymentId, paymentHash, paymentPreimage, feePaidMsat):

Check warning on line 765 in Bitkit/Services/LightningService.swift

View workflow job for this annotation

GitHub Actions / Run Integration Tests

immutable value 'paymentPreimage' was never used; consider replacing with '_' or removing it
Logger.info("✅ Payment successful: paymentId: \(paymentId ?? "?") paymentHash: \(paymentHash) feePaidMsat: \(feePaidMsat ?? 0)")
Task {
let hash = paymentId ?? paymentHash
Expand Down Expand Up @@ -816,7 +826,7 @@

if let channel {
await registerClosedChannel(channel: channel, reason: reasonString)
await MainActor.run {

Check warning on line 829 in Bitkit/Services/LightningService.swift

View workflow job for this annotation

GitHub Actions / Run Tests

result of call to 'run(resultType:body:)' is unused

Check warning on line 829 in Bitkit/Services/LightningService.swift

View workflow job for this annotation

GitHub Actions / Run Integration Tests

result of call to 'run(resultType:body:)' is unused
channelCache.removeValue(forKey: channelIdString)
}
} else {
Expand Down
36 changes: 21 additions & 15 deletions Bitkit/ViewModels/AppViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -684,9 +684,10 @@ extension AppViewModel {
// Only relevant for channels to external nodes
break
case .channelReady(let channelId, userChannelId: _, counterpartyNodeId: _, fundingTxo: _):
if let channel = lightningService.channels?.first(where: { $0.channelId == channelId }) {
Task {
let cjitOrder = try await CoreService.shared.blocktank.getCjit(channel: channel)
Task {
// Use channelCache instead of cachedChannels array, as it's updated immediately
if let channel = await lightningService.getChannelFromCache(channelId: channelId) {
let cjitOrder = await CoreService.shared.blocktank.getCjit(channel: channel)
if cjitOrder != nil {
let amount = channel.balanceOnCloseSats
let now = UInt64(Date().timeIntervalSince1970)
Expand All @@ -713,7 +714,9 @@ extension AppViewModel {
sheetViewModel.showSheet(.receivedTx, data: ReceivedTxSheetDetails(type: .lightning, sats: amount))
}
} else {
let channelCount = lightningService.channels?.count ?? 0
let channelCount = await MainActor.run {
lightningService.channels?.count ?? 0
}
if channelCount == 1 {
toast(
type: .lightning,
Expand All @@ -724,17 +727,20 @@ extension AppViewModel {
)
}
}
}
} else {
let channelCount = lightningService.channels?.count ?? 0
if channelCount == 1 {
toast(
type: .lightning,
title: t("lightning__channel_opened_title"),
description: t("lightning__channel_opened_msg"),
visibilityTime: 5.0,
accessibilityIdentifier: "SpendingBalanceReadyToast"
)
} else {
Logger.warn("Channel not found in cache: \(channelId)")
let channelCount = await MainActor.run {
lightningService.channels?.count ?? 0
}
if channelCount == 1 {
toast(
type: .lightning,
title: t("lightning__channel_opened_title"),
description: t("lightning__channel_opened_msg"),
visibilityTime: 5.0,
accessibilityIdentifier: "SpendingBalanceReadyToast"
)
}
}
}
case .channelClosed(channelId: _, userChannelId: _, counterpartyNodeId: _, reason: _):
Expand Down
Loading