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 440
/
PortfolioView.swift
238 lines (224 loc) · 7.25 KB
/
PortfolioView.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
/* 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 UIKit
import SwiftUI
import BraveCore
import SnapKit
import Introspect
import Strings
import DesignSystem
import BraveUI
import Shared
import Preferences
struct PortfolioView: View {
var cryptoStore: CryptoStore
@ObservedObject var keyringStore: KeyringStore
@ObservedObject var networkStore: NetworkStore
@ObservedObject var portfolioStore: PortfolioStore
@Environment(\.buySendSwapDestination)
private var buySendSwapDestination: Binding<BuySendSwapDestination?>
@State private var selectedContent: PortfolioSegmentedControl.Item = .assets
@ObservedObject private var isShowingNFTsTab = Preferences.Wallet.isShowingNFTsTab
@State private var isPresentingEditUserAssets: Bool = false
@State private var isPresentingAssetsFilters: Bool = false
@State private var isPresentingAddCustomNFT: Bool = false
@State private var isPresentingNFTsFilters: Bool = false
var body: some View {
ScrollView {
VStack(spacing: 0) {
PortfolioHeaderView(
keyringStore: keyringStore,
buySendSwapDestination: buySendSwapDestination,
selectedDateRange: $portfolioStore.timeframe,
balance: portfolioStore.balance,
balanceDifference: portfolioStore.balanceDifference,
historicalBalances: portfolioStore.historicalBalances,
isLoading: portfolioStore.isLoadingBalances
)
contentDrawer
}
}
.background(
VStack(spacing: 0) {
Color(braveSystemName: .pageBackground) // top scroll rubberband area
Color(braveSystemName: .containerBackground) // bottom drawer scroll rubberband area
}.edgesIgnoringSafeArea(.all)
)
.background(Color.clear
.sheet(isPresented: $isPresentingEditUserAssets) {
EditUserAssetsView(
networkStore: networkStore,
keyringStore: keyringStore,
userAssetsStore: portfolioStore.userAssetsStore
) {
cryptoStore.updateAssets()
}
})
.background(Color.clear
.sheet(isPresented: $isPresentingAssetsFilters) {
FiltersDisplaySettingsView(
filters: portfolioStore.filters,
isNFTFilters: false,
networkStore: networkStore,
save: { filters in
portfolioStore.saveFilters(filters)
}
)
.osAvailabilityModifiers({ view in
if #available(iOS 16, *) {
view
.presentationDetents([
.fraction(0.7),
.large
])
} else {
view
}
})
})
.background(Color.clear
.sheet(isPresented: $isPresentingAddCustomNFT) {
AddCustomAssetView(
networkStore: networkStore,
networkSelectionStore: networkStore.openNetworkSelectionStore(mode: .formSelection),
keyringStore: keyringStore,
userAssetStore: cryptoStore.nftStore.userAssetsStore,
supportedTokenTypes: [.nft]
) {
cryptoStore.updateAssets()
}
})
.background(Color.clear
.sheet(isPresented: $isPresentingNFTsFilters) {
FiltersDisplaySettingsView(
filters: cryptoStore.nftStore.filters,
isNFTFilters: true,
networkStore: networkStore,
save: { filters in
cryptoStore.nftStore.saveFilters(filters)
}
)
.osAvailabilityModifiers({ view in
if #available(iOS 16, *) {
view
.presentationDetents([
.fraction(0.6),
.large
])
} else {
view
}
})
})
}
private var contentDrawer: some View {
VStack {
if isShowingNFTsTab.value {
PortfolioSegmentedControl(selected: $selectedContent)
.padding(.horizontal)
.padding(.bottom, 6)
}
Group {
if selectedContent == .assets || !isShowingNFTsTab.value {
PortfolioAssetsView(
cryptoStore: cryptoStore,
keyringStore: keyringStore,
networkStore: networkStore,
portfolioStore: portfolioStore,
isPresentingEditUserAssets: $isPresentingEditUserAssets,
isPresentingFilters: $isPresentingAssetsFilters
)
.padding(.horizontal, 8)
} else {
NFTView(
cryptoStore: cryptoStore,
keyringStore: keyringStore,
networkStore: cryptoStore.networkStore,
nftStore: cryptoStore.nftStore,
isPresentingFilters: $isPresentingNFTsFilters,
isPresentingAddCustomNFT: $isPresentingAddCustomNFT
)
.padding(.horizontal, 8)
}
}
}
.padding(.vertical)
.background(
ZStack {
Color(braveSystemName: .pageBackground) // bg behind rounded corners
.zIndex(0)
Color(braveSystemName: .containerBackground)
.roundedCorner(16, corners: [.topLeft, .topRight])
.shadow(color: .black.opacity(0.04), radius: 8, x: 0, y: -8)
.zIndex(1)
}
)
}
}
/// Builds the in-section header for `Assets`/`NFT` that is shown in expanded and non-expanded state. Not used for ungrouped assets.
struct PortfolioAssetGroupHeaderView: View {
let group: any WalletAssetGroupViewModel
var body: some View {
VStack(spacing: 0) {
HStack {
if case let .network(networkInfo) = group.groupType {
NetworkIconView(network: networkInfo, length: 32)
} else if case let .account(accountInfo) = group.groupType {
Blockie(address: accountInfo.address)
.frame(width: 32, height: 32)
.clipShape(RoundedRectangle(cornerRadius: 4))
}
VStack(alignment: .leading) {
Text(group.title)
.font(.callout.weight(.semibold))
.foregroundColor(Color(WalletV2Design.textPrimary))
if let description = group.description {
Text(description)
.font(.footnote)
.foregroundColor(Color(WalletV2Design.textSecondary))
}
}
.multilineTextAlignment(.leading)
}
.padding(.vertical, 4)
}
}
}
#if DEBUG
struct PortfolioViewController_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
PortfolioView(
cryptoStore: .previewStore,
keyringStore: .previewStore,
networkStore: .previewStore,
portfolioStore: CryptoStore.previewStore.portfolioStore
)
.navigationBarTitleDisplayMode(.inline)
}
.previewColorSchemes()
}
}
#endif
private struct RoundedRect: Shape {
var radius: CGFloat
var corners: UIRectCorner
func path(in rect: CGRect) -> Path {
let path = UIBezierPath(
roundedRect: rect,
byRoundingCorners: corners,
cornerRadii: CGSize(
width: radius,
height: radius
)
)
return Path(path.cgPath)
}
}
extension View {
func roundedCorner(_ radius: CGFloat, corners: UIRectCorner) -> some View {
clipShape(RoundedRect(radius: radius, corners: corners) )
}
}