Track the list of deleted contracts so we don't refresh contract data for them upon launch

pull/419/head
Hwee-Boon Yar 6 years ago
parent 8020627ab5
commit 78d5e0c14b
  1. 4
      AlphaWallet.xcodeproj/project.pbxproj
  2. 2
      AlphaWallet/Core/Initializers/MirgrationInitializer.swift
  3. 30
      AlphaWallet/Tokens/Coordinators/TokensCoordinator.swift
  4. 13
      AlphaWallet/Tokens/Types/DeletedContract.swift
  5. 10
      AlphaWallet/Tokens/Types/TokensDataStore.swift

@ -303,6 +303,7 @@
5E7C7376B566E5A59CC8F463 /* ImportTicketViewControllerViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C72D0E7CA03ADE5CFAE7A /* ImportTicketViewControllerViewModel.swift */; };
5E7C73FC3990D110C474C3D6 /* WalletFilterViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C75CC640BAFFE0E789F44 /* WalletFilterViewModel.swift */; };
5E7C73FD5BD75D90C8D0EF3C /* WalletFilterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C7C58586099F082973073 /* WalletFilterView.swift */; };
5E7C741353DDF87133054FCC /* DeletedContract.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C72BEB789700C49FF64A6 /* DeletedContract.swift */; };
5E7C745A423BD10CFDED9A81 /* ServersViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C7CBEBF984CFCA29D6866 /* ServersViewModel.swift */; };
5E7C745C725F3F34037DCC68 /* SetTransferTicketsExpiryDateViewControllerViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C754C0E2E57F32A61F9A3 /* SetTransferTicketsExpiryDateViewControllerViewModel.swift */; };
5E7C745DACB5FCCEBCEB49CA /* WelcomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C793E23E2364B73C4D813 /* WelcomeViewController.swift */; };
@ -809,6 +810,7 @@
5E7C71EBD4C95AD4E11F3352 /* AlphaWalletSettingsButtonRow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AlphaWalletSettingsButtonRow.swift; path = Views/AlphaWalletSettingsButtonRow.swift; sourceTree = "<group>"; };
5E7C72142D5817EF8FA8CADA /* PrivacyPolicyViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrivacyPolicyViewController.swift; sourceTree = "<group>"; };
5E7C727433F7B8E322B3C68A /* SetTransferTicketsExpiryDateViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SetTransferTicketsExpiryDateViewController.swift; sourceTree = "<group>"; };
5E7C72BEB789700C49FF64A6 /* DeletedContract.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeletedContract.swift; sourceTree = "<group>"; };
5E7C72D0E7CA03ADE5CFAE7A /* ImportTicketViewControllerViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImportTicketViewControllerViewModel.swift; sourceTree = "<group>"; };
5E7C731B6F01534683227123 /* TicketTokenViewCellViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TicketTokenViewCellViewModel.swift; sourceTree = "<group>"; };
5E7C73495E0C0A207152EC25 /* LockEnterPasscodeViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = LockEnterPasscodeViewController.swift; path = AlphaWallet/AlphaWalletLock/ViewControllers/LockEnterPasscodeViewController.swift; sourceTree = SOURCE_ROOT; };
@ -1708,6 +1710,7 @@
442FC90F0768C43EF903465B /* Ticket.swift */,
442FCFEB2D7443C4E0B889B0 /* TicketHolder.swift */,
5E7C74B9EB81C51E956566E7 /* TokensDataStore.swift */,
5E7C72BEB789700C49FF64A6 /* DeletedContract.swift */,
);
path = Types;
sourceTree = "<group>";
@ -3645,6 +3648,7 @@
5E7C74BD08801CABF9695853 /* LocaleViewModel.swift in Sources */,
5E7C7B348A4DB36FC2D39DDF /* AssetField.swift in Sources */,
76F1D74912F5D8CDA72363BD /* GetContractInteractions.swift in Sources */,
5E7C741353DDF87133054FCC /* DeletedContract.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

@ -20,7 +20,7 @@ class MigrationInitializer: Initializer {
}
func perform() {
config.schemaVersion = 41
config.schemaVersion = 42
config.migrationBlock = { migration, oldSchemaVersion in
switch oldSchemaVersion {
case 0...32:

@ -3,6 +3,7 @@
import Foundation
import UIKit
import TrustKeystore
import Alamofire
protocol TokensCoordinatorDelegate: class {
func didPress(for type: PaymentFlow, in coordinator: TokensCoordinator)
@ -17,6 +18,7 @@ private enum ContractData {
case decimals(UInt8)
case stormBirdComplete(name: String, symbol: String, balance: [String])
case nonStormBirdComplete(name: String, symbol: String, decimals: UInt8)
case failed(networkReachable: Bool?)
}
class TokensCoordinator: Coordinator {
@ -79,7 +81,8 @@ class TokensCoordinator: Coordinator {
GetContractInteractions(web3: web3).getContractList(address: address.eip55String, chainId: config.chainID) { contracts in
let detectedContracts = contracts.map { $0.lowercased() }
let alreadyAddedContracts = self.storage.enabledObject.map { $0.address.eip55String.lowercased() }
let contractsToAdd = detectedContracts - alreadyAddedContracts
let deletedContracts = self.storage.deletedContracts.map { $0.contract.lowercased() }
let contractsToAdd = detectedContracts - alreadyAddedContracts - deletedContracts
for eachContract in contractsToAdd {
self.addToken(for: eachContract)
}
@ -114,6 +117,10 @@ class TokensCoordinator: Coordinator {
)
self.storage.add(tokens: [token])
self.tokensViewController.fetch()
case .failed(let networkReachable):
if let networkReachable = networkReachable, networkReachable {
self.storage.add(deadContracts: [DeletedContract(contract: contract)])
}
}
}
}
@ -153,11 +160,13 @@ class TokensCoordinator: Coordinator {
tokensViewController.fetch()
}
/// Failure to obtain contract data may be due to no-connectivity. So we should check .failed(networkReachable: Bool)
private func fetchContractData(for address: String, completion: @escaping (ContractData) -> Void) {
var completedName: String?
var completedSymbol: String?
var completedBalance: [String]?
var completedDecimals: UInt8?
var failed = false
func callCompletionOnAllData() {
if let completedName = completedName, let completedSymbol = completedSymbol, let completedBalance = completedBalance {
@ -167,6 +176,13 @@ class TokensCoordinator: Coordinator {
}
}
func callCompletionFailed() {
guard !failed else { return }
failed = true
//TODO maybe better to share an instance of the reachability manager
completion(.failed(networkReachable: NetworkReachabilityManager()?.isReachable))
}
self.storage.getContractName(for: address) { result in
switch result {
case .success(let name):
@ -174,7 +190,7 @@ class TokensCoordinator: Coordinator {
completion(.name(name))
callCompletionOnAllData()
case .failure:
break
callCompletionFailed()
}
}
@ -185,7 +201,7 @@ class TokensCoordinator: Coordinator {
completion(.symbol(symbol))
callCompletionOnAllData()
case .failure:
break
callCompletionFailed()
}
}
@ -200,7 +216,7 @@ class TokensCoordinator: Coordinator {
completion(.balance(balance))
callCompletionOnAllData()
case .failure:
break
callCompletionFailed()
}
}
} else {
@ -211,7 +227,7 @@ class TokensCoordinator: Coordinator {
completion(.decimals(decimal))
callCompletionOnAllData()
case .failure:
break
callCompletionFailed()
}
}
}
@ -223,7 +239,7 @@ class TokensCoordinator: Coordinator {
completion(.decimals(decimal))
callCompletionOnAllData()
case .failure:
break
callCompletionFailed()
}
}
}
@ -287,6 +303,8 @@ extension TokensCoordinator: NewTokenViewControllerDelegate {
break
case .nonStormBirdComplete:
break
case .failed:
break
}
}
}

@ -0,0 +1,13 @@
// Copyright © 2018 Stormbird PTE. LTD.
import Foundation
import RealmSwift
class DeletedContract: Object {
@objc dynamic var contract: String = ""
convenience init(contract: String) {
self.init()
self.contract = contract
}
}

@ -109,6 +109,10 @@ class TokensDataStore {
.filter { !$0.isDisabled }
}
var deletedContracts: [DeletedContract] {
return Array(realm.objects(DeletedContract.self))
}
static func update(in realm: Realm, tokens: [Token]) {
realm.beginWrite()
for token in tokens {
@ -282,6 +286,12 @@ class TokensDataStore {
}
}
func add(deadContracts: [DeletedContract]) {
try! realm.write {
realm.add(deadContracts, update: false)
}
}
@discardableResult
func add(tokens: [TokenObject]) -> [TokenObject] {
realm.beginWrite()

Loading…
Cancel
Save