-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNavigationViewModel.swift
More file actions
173 lines (149 loc) · 4.01 KB
/
Copy pathNavigationViewModel.swift
File metadata and controls
173 lines (149 loc) · 4.01 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
import BitkitCore
import LDKNode
import SwiftUI
enum Route: Hashable {
case savingsWallet
case spendingWallet
case activityList
case activityDetail(Activity)
case activityExplorer(Activity)
case buyBitcoin
case contacts
case contactsIntro
case contactDetail(publicKey: String)
case contactActivity(publicKey: String)
case contactImportOverview
case contactImportSelect
case addContact(publicKey: String)
case editContact(publicKey: String)
case profile
case profileIntro
case pubkyChoice
case createProfile
case editProfile
case payContacts
case transferIntro
case fundingOptions
case spendingIntro
case spendingAmount
case spendingConfirm(order: IBtOrder)
case spendingAdvanced(order: IBtOrder)
case transferLearnMore(order: IBtOrder)
case settingUp
case fundingAdvanced
case fundManual(nodeUri: String?)
case fundManualAmount(lnPeer: LnPeer)
case fundManualConfirm(lnPeer: LnPeer, amountSats: UInt64)
case fundManualSuccess
case lnurlChannel(channelData: LnurlChannelData)
case savingsIntro
case savingsAvailability
case savingsConfirm
case savingsAdvanced
case savingsProgress
case scanner
case support
// Shop
case shopIntro
case shopDiscover
case shopMain(page: String)
// Widgets
case widgetsIntro
case widgetsList
case widgetDetail(WidgetType)
case widgetEdit(WidgetType)
// Support
case reportIssue
case appStatus
// Settings
// General/Interface
case settings
case languageSettings
case currencySettings
case unitSettings
case tagSettings
case widgetsSettings
// General/Payments
case transactionSpeedSettings
case customSpeedSettings
case quickpay
case quickpayIntro
case notifications
case notificationsIntro
// Security
case dataBackups
case reset
case changePin
// Advanced/Payments
case coinSelection
case addressTypePreference
case connections
case connectionDetail(channelId: String)
case closeConnection(channel: ChannelDetails)
case node
case electrumSettings
case rgsSettings
case addressViewer
case devSettings
// Dev settings
case blocktankRegtest
case ldkDebug
case vssDebug
case probingTool
case orders
case logs
}
extension Route {
var isContactImportRoute: Bool {
switch self {
case .contactImportOverview, .contactImportSelect:
true
default:
false
}
}
}
func shouldDiscardPendingImport(currentRoute: Route?, destination: Route?) -> Bool {
guard currentRoute?.isContactImportRoute == true else {
return false
}
return destination?.isContactImportRoute != true
}
func fallbackRouteForMissingPendingImport(hasPendingImport: Bool) -> Route? {
hasPendingImport ? nil : .payContacts
}
func resolvePubkyRoute(input: String, ownPublicKey: String?, contacts: [PubkyContact]) -> Route? {
guard let normalizedKey = PubkyPublicKeyFormat.normalized(input) else {
return nil
}
if PubkyPublicKeyFormat.matches(normalizedKey, ownPublicKey) {
return .profile
}
if contacts.contains(where: { PubkyPublicKeyFormat.matches($0.publicKey, normalizedKey) }) {
return .contactDetail(publicKey: normalizedKey)
}
return .addContact(publicKey: normalizedKey)
}
func resolvePastedPubkyRoute(input: String, ownPublicKey: String?, contacts: [PubkyContact]) -> Route? {
resolvePubkyRoute(input: input, ownPublicKey: ownPublicKey, contacts: contacts)
}
@MainActor
class NavigationViewModel: ObservableObject {
@Published var path: [Route] = []
var currentRoute: Route? {
path.last
}
var canGoBack: Bool {
return !path.isEmpty
}
func navigate(_ route: Route) {
path.append(route)
}
func navigateBack() {
guard !path.isEmpty else { return }
path.removeLast()
}
func reset() {
path.removeAll()
}
}