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 441
/
AssetIconView.swift
326 lines (299 loc) · 9.67 KB
/
AssetIconView.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// 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 SwiftUI
import BraveCore
import BraveUI
import DesignSystem
/// Displays an asset's icon from the token registry or logo.
struct AssetIcon: View {
let token: BraveWallet.BlockchainToken
let network: BraveWallet.NetworkInfo?
var body: some View {
Group {
if let uiImage = token.localImage(network: network) {
Image(uiImage: uiImage)
.resizable()
.aspectRatio(contentMode: .fit)
} else if let url = URL(string: token.logo) {
WebImageReader(url: url) { image in
if let image = image {
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fit)
} else {
fallbackMonogram
}
}
} else {
fallbackMonogram
}
}
}
@State private var monogramSize: CGSize = .zero
private var fallbackMonogram: some View {
BlockieMaterial(address: token.contractAddress)
.blur(radius: 8, opaque: true)
.clipShape(Circle())
.readSize(onChange: { newSize in
monogramSize = newSize
})
.overlay(
Text(token.symbol.first?.uppercased() ?? "")
.font(.system(size: max(monogramSize.width, monogramSize.height) / 2, weight: .bold, design: .rounded))
.foregroundColor(.white)
.shadow(color: .black.opacity(0.3), radius: 2, x: 0, y: 1)
)
}
}
/// Displays an asset's icon from the token registry or logo.
///
/// By default, creating an `AssetIconView` will result in a dynamically sized icon based
/// on the users size category.
struct AssetIconView: View {
var token: BraveWallet.BlockchainToken
var network: BraveWallet.NetworkInfo?
/// If we should show the network logo on non-native assets. NetworkInfo is required.
var shouldShowNetworkIcon: Bool = false
@ScaledMetric var length: CGFloat = 40
var maxLength: CGFloat?
@ScaledMetric var networkSymbolLength: CGFloat = 15
var maxNetworkSymbolLength: CGFloat?
var body: some View {
AssetIcon(token: token, network: network)
.frame(width: min(length, maxLength ?? length), height: min(length, maxLength ?? length))
.overlay(tokenNetworkLogo, alignment: .bottomTrailing)
.accessibilityHidden(true)
}
@ViewBuilder private var tokenNetworkLogo: some View {
if let network,
shouldShowNetworkIcon, // explicitly show/not show network logo
(!network.isNativeAsset(token) || network.nativeTokenLogoName != network.networkLogoName), // non-native asset OR if the network is not the official Ethereum network, but uses ETH as gas
let image = network.networkLogoImage {
Image(uiImage: image)
.resizable()
.overlay(
Circle()
.stroke(lineWidth: 2)
.foregroundColor(Color(braveSystemName: .containerBackground))
)
.frame(width: min(networkSymbolLength, maxNetworkSymbolLength ?? networkSymbolLength), height: min(networkSymbolLength, maxNetworkSymbolLength ?? networkSymbolLength))
}
}
}
#if DEBUG
struct AssetIconView_Previews: PreviewProvider {
static var previews: some View {
AssetIconView(token: .previewToken, network: .mockMainnet)
.previewLayout(.sizeThatFits)
.padding()
.previewSizeCategories()
AssetIconView(
token: .init(
contractAddress: "0x55296f69f40ea6d20e478533c15a6b08b654e758",
name: "XY Oracle",
logo: "",
isErc20: true,
isErc721: false,
isErc1155: false,
isNft: false,
isSpam: false,
symbol: "XYO",
decimals: 18,
visible: false,
tokenId: "",
coingeckoId: "",
chainId: "",
coin: .eth
),
network: .mockMainnet
)
.previewLayout(.sizeThatFits)
.padding()
.previewSizeCategories()
}
}
#endif
struct NFTIconView: View {
/// NFT token
var token: BraveWallet.BlockchainToken
/// Network for token
var network: BraveWallet.NetworkInfo
/// NFT image url from metadata
var url: URL?
/// If we should show the network logo on non-native assets
var shouldShowNetworkIcon: Bool = false
/// View is loading NFT metadata
var isLoadingMetadata: Bool = false
@ScaledMetric var length: CGFloat = 40
var maxLength: CGFloat?
@ScaledMetric var tokenLogoLength: CGFloat = 15
var maxTokenLogoLength: CGFloat?
@ViewBuilder private var tokenLogo: some View {
if shouldShowNetworkIcon, let image = network.nativeTokenLogoImage {
Image(uiImage: image)
.resizable()
.overlay(
Circle()
.stroke(lineWidth: 2)
.foregroundColor(Color(braveSystemName: .containerBackground))
)
.frame(
width: min(tokenLogoLength, maxTokenLogoLength ?? tokenLogoLength),
height: min(tokenLogoLength, maxTokenLogoLength ?? tokenLogoLength)
)
}
}
var body: some View {
NFTImageView( // logo populated for auto-discovered NFTs
urlString: url?.absoluteString ?? token.logo,
isLoading: isLoadingMetadata
) {
LoadingNFTView(shimmer: false)
}
.cornerRadius(5)
.frame(
width: min(length, maxLength ?? length),
height: min(length, maxLength ?? length)
)
.overlay(tokenLogo, alignment: .bottomTrailing)
.allowsHitTesting(false)
.accessibilityHidden(true)
}
}
/// Displays 2 asset icons stacked on top of one another, with the token's network logo on top.
/// `bottomToken` is displayed in the top-right, `topToken` is displayed in the bottom-left,
/// and the network logo is displayed in the bottom-right.
struct StackedAssetIconsView: View {
var bottomToken: BraveWallet.BlockchainToken?
var topToken: BraveWallet.BlockchainToken?
var network: BraveWallet.NetworkInfo
/// Length of entire view
@ScaledMetric var length: CGFloat = 40
/// Max length of entire view
var maxLength: CGFloat?
@ScaledMetric var networkSymbolLength: CGFloat = 15
var maxNetworkSymbolLength: CGFloat?
/// Size of padding applied to bottom/top icon so they can overlap
private var iconPadding: CGFloat {
length / 5
}
/// Size of asset icon
private var assetIconLength: CGFloat {
length - iconPadding
}
/// Max size of asset icon
private var maxAssetIconLength: CGFloat? {
guard let maxLength else { return nil }
return maxLength - iconPadding
}
var body: some View {
ZStack {
Group {
if let bottomToken {
AssetIconView(
token: bottomToken,
network: network,
shouldShowNetworkIcon: false,
length: assetIconLength,
maxLength: maxAssetIconLength
)
} else { // nil token possible for Solana Swaps
GenericAssetIconView(
backgroundColor: Color(braveSystemName: .gray40),
iconColor: Color.white,
length: assetIconLength,
maxLength: maxAssetIconLength
)
}
}
.padding(.leading, iconPadding)
.padding(.bottom, iconPadding)
.zIndex(0)
Group {
if let topToken {
AssetIconView(
token: topToken,
network: network,
shouldShowNetworkIcon: false,
length: assetIconLength,
maxLength: maxAssetIconLength
)
} else { // nil token possible for Solana Swaps
GenericAssetIconView(
backgroundColor: Color(braveSystemName: .gray20),
iconColor: Color.black,
length: assetIconLength,
maxLength: maxAssetIconLength
)
}
}
.padding(.top, iconPadding)
.padding(.trailing, iconPadding)
.zIndex(1)
if let networkLogoImage = network.networkLogoImage {
Group {
Image(uiImage: networkLogoImage)
.resizable()
.overlay(
Circle()
.stroke(lineWidth: 2)
.foregroundColor(Color(braveSystemName: .containerBackground))
)
.frame(
width: min(networkSymbolLength, maxNetworkSymbolLength ?? networkSymbolLength),
height: min(networkSymbolLength, maxNetworkSymbolLength ?? networkSymbolLength)
)
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottomTrailing)
.zIndex(2)
}
}
.frame(
width: min(length, maxLength ?? length),
height: min(length, maxLength ?? length)
)
}
}
#if DEBUG
struct StackedAssetIconsView_Previews: PreviewProvider {
static var previews: some View {
StackedAssetIconsView(
bottomToken: nil,
topToken: nil,
network: .mockSolana
)
.previewLayout(.sizeThatFits)
}
}
#endif
struct GenericAssetIconView: View {
let backgroundColor: Color
let iconColor: Color
@ScaledMetric var length: CGFloat
let maxLength: CGFloat?
init(
backgroundColor: Color = Color(braveSystemName: .gray20),
iconColor: Color = Color.black,
length: CGFloat = 40,
maxLength: CGFloat? = nil
) {
self.backgroundColor = backgroundColor
self.iconColor = iconColor
self._length = .init(wrappedValue: length)
self.maxLength = maxLength
}
var body: some View {
Circle()
.fill(backgroundColor)
.frame(width: min(length, maxLength ?? length), height: min(length, maxLength ?? length))
.overlay {
Image(braveSystemName: "leo.crypto.wallets")
.resizable()
.aspectRatio(contentMode: .fit)
.padding(6)
.foregroundColor(iconColor)
}
}
}