This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 442
/
CryptoTabsView.swift
268 lines (256 loc) · 8.81 KB
/
CryptoTabsView.swift
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* Copyright 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import UIKit
import SwiftUI
import BraveUI
import Strings
enum CryptoTab: Equatable, Hashable, CaseIterable {
case portfolio
case activity
case accounts
case market
@ViewBuilder var tabLabel: some View {
switch self {
case .portfolio:
Label(Strings.Wallet.portfolioPageTitle, braveSystemImage: "leo.coins")
case .activity:
Label(Strings.Wallet.activityPageTitle, braveSystemImage: "leo.activity")
case .accounts:
Label(Strings.Wallet.accountsPageTitle, braveSystemImage: "leo.user.accounts")
case .market:
Label(Strings.Wallet.marketPageTitle, braveSystemImage: "leo.discover")
}
}
}
struct CryptoTabsView<DismissContent: ToolbarContent>: View {
@ObservedObject var cryptoStore: CryptoStore
@ObservedObject var keyringStore: KeyringStore
var toolbarDismissContent: DismissContent
@State private var isShowingMainMenu: Bool = false
@State private var isTabShowingSettings: [CryptoTab: Bool] = CryptoTab.allCases.reduce(into: [CryptoTab: Bool]()) { $0[$1] = false }
@State private var isShowingSearch: Bool = false
@State private var isShowingBackup: Bool = false
@State private var isShowingAddAccount: Bool = false
@State private var fetchedPendingRequestsThisSession: Bool = false
@State private var selectedTab: CryptoTab = .portfolio
private var isConfirmationButtonVisible: Bool {
if case .transactions(let txs) = cryptoStore.pendingRequest {
return !txs.isEmpty
}
return cryptoStore.pendingRequest != nil
}
var body: some View {
TabView(selection: $selectedTab) {
NavigationView {
PortfolioView(
cryptoStore: cryptoStore,
keyringStore: keyringStore,
networkStore: cryptoStore.networkStore,
portfolioStore: cryptoStore.portfolioStore
)
.navigationTitle(Strings.Wallet.wallet)
.navigationBarTitleDisplayMode(.inline)
.transparentUnlessScrolledNavigationAppearance()
.toolbar { sharedToolbarItems }
.background(settingsNavigationLink(for: .portfolio))
}
.navigationViewStyle(.stack)
.tabItem {
CryptoTab.portfolio.tabLabel
}
.tag(CryptoTab.portfolio)
NavigationView {
TransactionsActivityView(
store: cryptoStore.transactionsActivityStore,
networkStore: cryptoStore.networkStore
)
.navigationTitle(Strings.Wallet.wallet)
.navigationBarTitleDisplayMode(.inline)
.applyRegularNavigationAppearance()
.toolbar { sharedToolbarItems }
.background(settingsNavigationLink(for: .activity))
}
.navigationViewStyle(.stack)
.tabItem {
CryptoTab.activity.tabLabel
}
.tag(CryptoTab.activity)
NavigationView {
AccountsView(
store: cryptoStore.accountsStore,
cryptoStore: cryptoStore,
keyringStore: keyringStore
)
.navigationTitle(Strings.Wallet.wallet)
.navigationBarTitleDisplayMode(.inline)
.applyRegularNavigationAppearance()
.toolbar { sharedToolbarItems }
.background(settingsNavigationLink(for: .accounts))
}
.navigationViewStyle(.stack)
.tabItem {
CryptoTab.accounts.tabLabel
}
.tag(CryptoTab.accounts)
NavigationView {
MarketView(
cryptoStore: cryptoStore,
keyringStore: keyringStore
)
.navigationTitle(Strings.Wallet.wallet)
.navigationBarTitleDisplayMode(.inline)
.applyRegularNavigationAppearance()
.toolbar { sharedToolbarItems }
.background(settingsNavigationLink(for: .market))
}
.navigationViewStyle(.stack)
.tabItem {
CryptoTab.market.tabLabel
}
.tag(CryptoTab.market)
}
.introspectTabBarController(customize: { tabBarController in
let appearance = UITabBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor(braveSystemName: .containerBackground)
tabBarController.tabBar.standardAppearance = appearance
tabBarController.tabBar.scrollEdgeAppearance = appearance
})
.overlay(alignment: .bottomTrailing, content: {
if isConfirmationButtonVisible {
Button(action: {
cryptoStore.isPresentingPendingRequest = true
}) {
Image(braveSystemName: "leo.notification.dot")
.font(.system(size: 18))
.foregroundColor(.white)
.frame(width: 36, height: 36)
.background(
Color(uiColor: .braveBlurpleTint)
.clipShape(Circle())
)
}
.accessibilityLabel(Text(Strings.Wallet.confirmTransactionsTitle))
.padding(.trailing, 16)
.padding(.bottom, 100)
}
})
.onAppear {
// If a user chooses not to confirm/reject their requests we shouldn't
// do it again until they close and re-open wallet
if !fetchedPendingRequestsThisSession {
// Give the animation time
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.fetchedPendingRequestsThisSession = true
self.cryptoStore.prepare(isInitialOpen: true)
}
}
}
.ignoresSafeArea()
.background(
Color.clear
.sheet(isPresented: $cryptoStore.isPresentingAssetSearch) {
AssetSearchView(
keyringStore: keyringStore,
cryptoStore: cryptoStore,
userAssetsStore: cryptoStore.portfolioStore.userAssetsStore
)
}
)
.sheet(isPresented: $isShowingMainMenu) {
MainMenuView(
selectedTab: selectedTab,
isShowingSettings: Binding(get: {
self.isTabShowingSettings[selectedTab, default: false]
}, set: { isActive, _ in
self.isTabShowingSettings[selectedTab] = isActive
}),
isShowingBackup: $isShowingBackup,
isShowingAddAccount: $isShowingAddAccount,
keyringStore: keyringStore
)
.background(
Color.clear
.sheet(isPresented: Binding(get: {
isShowingBackup
}, set: { newValue in
if !newValue {
// dismiss menu if we're dismissing backup from menu
isShowingMainMenu = false
}
isShowingBackup = newValue
})) {
NavigationView {
BackupWalletView(
password: nil,
keyringStore: keyringStore
)
}
.navigationViewStyle(.stack)
.environment(\.modalPresentationMode, $isShowingBackup)
.accentColor(Color(.braveBlurpleTint))
}
)
.background(
Color.clear
.sheet(isPresented: Binding(get: {
isShowingAddAccount
}, set: { newValue in
if !newValue {
// dismiss menu if we're dismissing add account from menu
isShowingMainMenu = false
}
isShowingAddAccount = newValue
})) {
NavigationView {
AddAccountView(
keyringStore: keyringStore,
networkStore: cryptoStore.networkStore
)
}
.navigationViewStyle(StackNavigationViewStyle())
}
)
}
}
@ToolbarContentBuilder private var sharedToolbarItems: some ToolbarContent {
ToolbarItemGroup(placement: .navigationBarTrailing) {
if selectedTab == .portfolio {
Button(action: {
cryptoStore.isPresentingAssetSearch = true
}) {
Label(Strings.Wallet.searchTitle, systemImage: "magnifyingglass")
.labelStyle(.iconOnly)
.foregroundColor(Color(.braveBlurpleTint))
}
}
Button(action: { self.isShowingMainMenu = true }) {
Label(Strings.Wallet.otherWalletActionsAccessibilityTitle, braveSystemImage: "leo.more.horizontal")
.labelStyle(.iconOnly)
.foregroundColor(Color(.braveBlurpleTint))
}
.accessibilityLabel(Strings.Wallet.otherWalletActionsAccessibilityTitle)
}
toolbarDismissContent
}
private func settingsNavigationLink(for tab: CryptoTab) -> some View {
NavigationLink(
destination: Web3SettingsView(
settingsStore: cryptoStore.settingsStore,
networkStore: cryptoStore.networkStore,
keyringStore: keyringStore
),
isActive: Binding(get: {
self.isTabShowingSettings[tab, default: false]
}, set: { isActive, _ in
self.isTabShowingSettings[tab] = isActive
})
) {
Text(Strings.Wallet.settings)
}
.hidden()
}
}