Skip to content

Commit

Permalink
Added logic to use an internal SpamToken image to TokenImageFetcherIm…
Browse files Browse the repository at this point in the history
…pl instead of fetching it over the network
  • Loading branch information
eviltofu committed Apr 11, 2023
1 parent 2a0769c commit f8235ff
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion AlphaWallet/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class AppCoordinator: NSObject, Coordinator {
private let addressStorage: FileAddressStorage
private let tokenScriptOverridesFileManager = TokenScriptOverridesFileManager()

private let tokenImageFetcher: TokenImageFetcher = TokenImageFetcherImpl(networking: KingfisherImageFetcher())
private lazy var tokenImageFetcher: TokenImageFetcher = TokenImageFetcherImpl(networking: KingfisherImageFetcher(), tokenGroupIdentifier: tokenGroupIdentifier, spamImage: R.image.spamSmall()!)

private let tokenGroupIdentifier: TokenGroupIdentifierProtocol = TokenGroupIdentifier.identifier(fromFileName: "tokens")!

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "spam-small.png",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,20 @@ public protocol TokenImageFetcher {

public class TokenImageFetcherImpl: TokenImageFetcher {
private let networking: ImageFetcher
private let tokenGroupsIdentifier: TokenGroupIdentifierProtocol
private let spamImage: UIImage
private let subscribables: AtomicDictionary<String, CurrentValueSubject<TokenImage?, Never>> = .init()

enum ImageAvailabilityError: LocalizedError {
case notAvailable
}

public init(networking: ImageFetcher) {
public init(networking: ImageFetcher,
tokenGroupIdentifier: TokenGroupIdentifierProtocol,
spamImage: UIImage) {
self.networking = networking
self.tokenGroupsIdentifier = tokenGroupIdentifier
self.spamImage = spamImage
}

private static func programmaticallyGenerateIcon(for contractAddress: AlphaWallet.Address,
Expand Down Expand Up @@ -132,14 +138,18 @@ public class TokenImageFetcherImpl: TokenImageFetcher {

switch type {
case .nativeCryptocurrency:
if let img = serverIconImage {
return .init(image: .image(.loaded(image: img)), isFinal: true, overlayServerIcon: nil)
if let img = iconImageForContractAndChainID(image: serverIconImage, address: contractAddress.eip55String, chainID: server.chainID) {
return TokenImage(image: .image(.loaded(image: img)), isFinal: true, overlayServerIcon: nil)
}
case .erc20, .erc875, .erc721, .erc721ForTickets, .erc1155:
if let img = tokenImage {
return .init(image: .image(.loaded(image: img)), isFinal: true, overlayServerIcon: staticOverlayIcon)
if let img = iconImageForContractAndChainID(image: tokenImage, address: contractAddress.eip55String, chainID: server.chainID) {
return TokenImage(image: .image(.loaded(image: img)), isFinal: true, overlayServerIcon: staticOverlayIcon)
}
}

if tokenGroupsIdentifier.isSpam(address: contractAddress.eip55String, chainID: server.chainID) {
return TokenImage(image: .image(.loaded(image: spamImage)), isFinal: true, overlayServerIcon: nil)
}

return TokenImageFetcherImpl.programmaticallyGenerateIcon(
for: contractAddress,
Expand All @@ -151,6 +161,13 @@ public class TokenImageFetcherImpl: TokenImageFetcher {
blockChainNameColor: blockChainNameColor)
}

private func iconImageForContractAndChainID(image iconImage: UIImage?, address: String, chainID: Int) -> UIImage? {
if tokenGroupsIdentifier.isSpam(address: address, chainID: chainID) {
return spamImage
}
return iconImage
}

public func image(contractAddress: AlphaWallet.Address,
server: RPCServer,
name: String,
Expand Down

0 comments on commit f8235ff

Please sign in to comment.