-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPaymentNavigationHelper.swift
More file actions
220 lines (196 loc) · 7.08 KB
/
Copy pathPaymentNavigationHelper.swift
File metadata and controls
220 lines (196 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import BitkitCore
import Foundation
@MainActor
struct PaymentNavigationHelper {
/// Determines if quickpay should be used for the current app state
/// - Parameters:
/// - app: The app view model containing the current invoice state
/// - settings: The settings view model
/// - currency: The currency view model
/// - Returns: True if quickpay should be used, false otherwise
static func shouldUseQuickpay(
app: AppViewModel,
settings: SettingsViewModel,
currency: CurrencyViewModel,
spendStore: QuickPaySpendStore = .shared,
coordinator: QuickPayPaymentCoordinator? = nil
) -> Bool {
guard let amountSats = QuickPayLimits.paymentAmountSats(app: app), amountSats > 0 else {
return false
}
let coordinator = coordinator ?? .shared
if let hash = app.scannedLightningInvoice?.paymentHash.hex, coordinator.hasOpen(hash) {
return true
}
return spendStore.canApply(
amountSats: amountSats,
enabled: settings.enableQuickpay,
thresholdUsd: settings.quickpayAmount,
multiplier: settings.quickpayDailyLimitMultiplier,
rates: .live(currency)
)
}
/// Centralized method to open the appropriate sheet based on the current state
static func openPaymentSheet(
app: AppViewModel,
currency: CurrencyViewModel,
settings: SettingsViewModel,
sheetViewModel: SheetViewModel,
spendStore: QuickPaySpendStore = .shared,
coordinator: QuickPayPaymentCoordinator? = nil
) {
// Handle LNURL withdraw
if let lnurlWithdrawData = app.lnurlWithdrawData {
Logger.info("LNURL withdraw data: \(lnurlWithdrawData)")
if lnurlWithdrawData.isFixedAmount {
sheetViewModel.showSheet(.lnurlWithdraw, data: LnurlWithdrawConfig(view: .confirm))
} else {
sheetViewModel.showSheet(.lnurlWithdraw, data: LnurlWithdrawConfig(view: .amount))
}
return
}
let shouldUseQuickpay = shouldUseQuickpay(
app: app,
settings: settings,
currency: currency,
spendStore: spendStore,
coordinator: coordinator
)
// Handle Lightning address / LNURL pay
if let lnurlPayData = app.lnurlPayData {
if shouldUseQuickpay {
sheetViewModel.showSheet(.send, data: SendConfig(view: .quickpay))
} else if lnurlPayData.isFixedAmount {
sheetViewModel.showSheet(.send, data: SendConfig(view: .lnurlPayConfirm))
} else {
sheetViewModel.showSheet(.send, data: SendConfig(view: .lnurlPayAmount))
}
return
}
// Handle lightning invoice
if app.scannedLightningInvoice != nil {
let amount = app.scannedLightningInvoice!.amountSatoshis
if amount > 0 && shouldUseQuickpay {
sheetViewModel.showSheet(.send, data: SendConfig(view: .quickpay))
} else {
if amount == 0 {
sheetViewModel.showSheet(.send, data: SendConfig(view: .amount))
} else {
sheetViewModel.showSheet(.send, data: SendConfig(view: .confirm))
}
}
return
}
// Handle onchain invoice
if app.scannedOnchainInvoice != nil {
sheetViewModel.showSheet(.send, data: SendConfig(view: .amount))
return
}
}
/// Returns the appropriate send route for navigation-based views
/// This allows views using NavigationStack to get the correct route
/// - Returns: The appropriate send route, or nil if no route should be shown
static func appropriateSendRoute(
app: AppViewModel,
currency: CurrencyViewModel,
settings: SettingsViewModel,
spendStore: QuickPaySpendStore = .shared,
coordinator: QuickPayPaymentCoordinator? = nil
) -> SendRoute? {
if let lnurlWithdrawData = app.lnurlWithdrawData {
if lnurlWithdrawData.isFixedAmount {
return .lnurlWithdrawConfirm
} else {
return .lnurlWithdrawAmount
}
}
let shouldUseQuickpay = shouldUseQuickpay(
app: app,
settings: settings,
currency: currency,
spendStore: spendStore,
coordinator: coordinator
)
// Handle Lightning address / LNURL pay
if let lnurlPayData = app.lnurlPayData {
if shouldUseQuickpay {
return .quickpay
} else if lnurlPayData.isFixedAmount {
return .lnurlPayConfirm
} else {
return .lnurlPayAmount
}
}
// Handle lightning invoice
if let invoice = app.scannedLightningInvoice {
let amount = invoice.amountSatoshis
if amount > 0 && shouldUseQuickpay {
return .quickpay
} else {
if amount == 0 {
return .amount
} else {
return .confirm
}
}
}
// Handle onchain invoice
if let _ = app.scannedOnchainInvoice {
return .amount
}
// No valid invoice data
return nil
}
static func confirmRouteAfterQuickPayCap(app: AppViewModel) -> SendRoute {
if let lnurlPayData = app.lnurlPayData {
return lnurlPayData.isFixedAmount ? .lnurlPayConfirm : .lnurlPayAmount
}
if let invoice = app.scannedLightningInvoice, invoice.amountSatoshis == 0 {
return .amount
}
return .confirm
}
static func replacingQuickPay(
in path: [SendRoute],
root: SendRoute,
with route: SendRoute
) -> (root: SendRoute, path: [SendRoute]) {
if root == .quickpay {
return (route, [])
}
if let index = path.lastIndex(of: .quickpay) {
var nextPath = Array(path.prefix(index))
nextPath.append(route)
return (root, nextPath)
}
return (root, path + [route])
}
static func contactPaymentRoute(
app: AppViewModel,
currency: CurrencyViewModel,
settings: SettingsViewModel,
spendStore: QuickPaySpendStore = .shared,
coordinator: QuickPayPaymentCoordinator? = nil
) -> SendRoute? {
guard let route = appropriateSendRoute(
app: app,
currency: currency,
settings: settings,
spendStore: spendStore,
coordinator: coordinator
) else {
return nil
}
switch route {
case .quickpay:
return confirmRouteAfterQuickPayCap(app: app)
case .confirm:
if let invoice = app.scannedLightningInvoice {
return invoice.amountSatoshis == 0 ? .amount : .confirm
}
return route
default:
return route
}
}
}