Skip to content

Commit 9da79b2

Browse files
committed
Improve Realm access and replacing many publishers with async/await calls to block less when app launches.
1 parent 30c17de commit 9da79b2

File tree

135 files changed

+2631
-2586
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+2631
-2586
lines changed

AlphaWallet/Accounts/ViewModels/AccountsViewModel.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ final class AccountsViewModel {
147147

148148
private func buildAccountRowViewModel(wallet: Wallet) -> AnyPublisher<AccountRowViewModel, Never> {
149149
let balance = walletBalanceService.walletBalance(for: wallet)
150-
let blockieImage = blockiesGenerator.getBlockieOrEnsAvatarImage(address: wallet.address, fallbackImage: BlockiesImage.defaulBlockieImage)
150+
let blockieImage = asFuture { await self.blockiesGenerator.getBlockieOrEnsAvatarImage(address: wallet.address, fallbackImage: BlockiesImage.defaulBlockieImage) }
151151
.handleEvents(receiveOutput: { [analytics] value in
152152
guard value.isEnsAvatar else { return }
153153
analytics.setUser(property: Analytics.UserProperties.hasEnsAvatar, value: true)

AlphaWallet/ActiveWalletCoordinator.swift

+34-30
Original file line numberDiff line numberDiff line change
@@ -775,41 +775,45 @@ extension ActiveWalletCoordinator: ActivityViewControllerDelegate {
775775
}
776776

777777
func speedupTransaction(transactionId: String, server: RPCServer, viewController: ActivityViewController) {
778-
guard let transaction = transactionsDataStore.transaction(withTransactionId: transactionId, forServer: server) else { return }
779-
guard let session = sessionsProvider.session(for: transaction.server) else { return }
780-
guard let coordinator = ReplaceTransactionCoordinator(
781-
analytics: analytics,
782-
domainResolutionService: domainResolutionService,
783-
keystore: keystore,
784-
presentingViewController: viewController,
785-
session: session,
786-
transaction: transaction,
787-
mode: .speedup,
788-
tokensService: tokensPipeline,
789-
networkService: networkService) else { return }
778+
Task { @MainActor in
779+
guard let transaction = await transactionsDataStore.transaction(withTransactionId: transactionId, forServer: server) else { return }
780+
guard let session = sessionsProvider.session(for: transaction.server) else { return }
781+
guard let coordinator = ReplaceTransactionCoordinator(
782+
analytics: analytics,
783+
domainResolutionService: domainResolutionService,
784+
keystore: keystore,
785+
presentingViewController: viewController,
786+
session: session,
787+
transaction: transaction,
788+
mode: .speedup,
789+
tokensService: tokensPipeline,
790+
networkService: networkService) else { return }
790791

791-
coordinator.delegate = self
792-
coordinator.start()
793-
addCoordinator(coordinator)
792+
coordinator.delegate = self
793+
coordinator.start()
794+
addCoordinator(coordinator)
795+
}
794796
}
795797

796798
func cancelTransaction(transactionId: String, server: RPCServer, viewController: ActivityViewController) {
797-
guard let transaction = transactionsDataStore.transaction(withTransactionId: transactionId, forServer: server) else { return }
798-
guard let session = sessionsProvider.session(for: transaction.server) else { return }
799-
guard let coordinator = ReplaceTransactionCoordinator(
800-
analytics: analytics,
801-
domainResolutionService: domainResolutionService,
802-
keystore: keystore,
803-
presentingViewController: viewController,
804-
session: session,
805-
transaction: transaction,
806-
mode: .cancel,
807-
tokensService: tokensPipeline,
808-
networkService: networkService) else { return }
799+
Task { @MainActor in
800+
guard let transaction = await transactionsDataStore.transaction(withTransactionId: transactionId, forServer: server) else { return }
801+
guard let session = sessionsProvider.session(for: transaction.server) else { return }
802+
guard let coordinator = ReplaceTransactionCoordinator(
803+
analytics: analytics,
804+
domainResolutionService: domainResolutionService,
805+
keystore: keystore,
806+
presentingViewController: viewController,
807+
session: session,
808+
transaction: transaction,
809+
mode: .cancel,
810+
tokensService: tokensPipeline,
811+
networkService: networkService) else { return }
809812

810-
coordinator.delegate = self
811-
coordinator.start()
812-
addCoordinator(coordinator)
813+
coordinator.delegate = self
814+
coordinator.start()
815+
addCoordinator(coordinator)
816+
}
813817
}
814818

815819
func goToTransaction(viewController: ActivityViewController) {

AlphaWallet/Activities/Coordinators/ActivitiesCoordinator.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class ActivitiesCoordinator: NSObject, Coordinator {
5656
let viewModel = ActivitiesViewModel(collection: .init())
5757
let controller = ActivitiesViewController(analytics: analytics, keystore: keystore, wallet: wallet, viewModel: viewModel, sessionsProvider: sessionsProvider, assetDefinitionStore: assetDefinitionStore, tokenImageFetcher: tokenImageFetcher)
5858
controller.delegate = self
59-
59+
6060
return controller
6161
}
6262

AlphaWallet/Activities/ViewControllers/ActivitiesViewController.swift

+2-9
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,7 @@ class ActivitiesViewController: UIViewController {
2121
private var activitiesView: ActivitiesView
2222
weak var delegate: ActivitiesViewControllerDelegate?
2323

24-
init(analytics: AnalyticsLogger,
25-
keystore: Keystore,
26-
wallet: Wallet,
27-
viewModel: ActivitiesViewModel,
28-
sessionsProvider: SessionsProvider,
29-
assetDefinitionStore: AssetDefinitionStore,
30-
tokenImageFetcher: TokenImageFetcher) {
31-
24+
init(analytics: AnalyticsLogger, keystore: Keystore, wallet: Wallet, viewModel: ActivitiesViewModel, sessionsProvider: SessionsProvider, assetDefinitionStore: AssetDefinitionStore, tokenImageFetcher: TokenImageFetcher) {
3225
self.viewModel = viewModel
3326
searchController = UISearchController(searchResultsController: nil)
3427
activitiesView = ActivitiesView(
@@ -159,7 +152,7 @@ extension ActivitiesViewController {
159152
private func configureSearchBarOnce() {
160153
guard !isSearchBarConfigured else { return }
161154
isSearchBarConfigured = true
162-
155+
163156
UISearchBar.configure(searchBar: searchController.searchBar)
164157
}
165158
}

AlphaWallet/Browser/Coordinators/QRCodeResolutionCoordinator.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ extension QRCodeResolutionCoordinator: ScanQRCodeCoordinatorDelegate {
7979
resolveScanResult(result, decodedValue: decodedValue)
8080
}
8181

82-
private func availableActions(forContract contract: AlphaWallet.Address) -> [ScanQRCodeAction] {
82+
private func availableActions(forContract contract: AlphaWallet.Address) async -> [ScanQRCodeAction] {
8383
switch usage {
8484
case .all(let tokensDataStore, _):
85-
let isTokenFound = tokensDataStore.token(for: contract, server: .main) != nil
85+
let isTokenFound = await tokensDataStore.token(for: contract, server: .main) != nil
8686
if isTokenFound {
8787
return [.sendToAddress, .watchWallet, .openInEtherscan]
8888
} else {
@@ -104,7 +104,7 @@ extension QRCodeResolutionCoordinator: ScanQRCodeCoordinatorDelegate {
104104
switch value {
105105
case .address(let contract):
106106
guard supportedResolutions.contains(.address) else { return }
107-
let actions = availableActions(forContract: contract)
107+
let actions = await availableActions(forContract: contract)
108108
if actions.count == 1 {
109109
delegate.coordinator(self, didResolve: .address(address: contract, action: actions[0]))
110110
} else {

AlphaWallet/Common/Initializers/ConfigureApp.swift

+1-9
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,7 @@ public class ConfigureApp: Initializer {
1616

1717
Attestation.isLoggingEnabled = true
1818
Attestation.callSmartContract = { chainId, contract, functionName, abiString, parameters in
19-
return try await withCheckedThrowingContinuation { continuation in
20-
firstly {
21-
callSmartContract(withServer: RPCServer(chainID: chainId), contract: contract, functionName: functionName, abiString: abiString, parameters: parameters)
22-
}.done { result in
23-
continuation.resume(returning: result)
24-
}.catch {
25-
continuation.resume(throwing: $0)
26-
}
27-
}
19+
return try await callSmartContractAsync(withServer: RPCServer(chainID: chainId), contract: contract, functionName: functionName, abiString: abiString, parameters: parameters)
2820
}
2921
}
3022
}

AlphaWallet/Common/Services/MediaContentNetworking.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
//
77

88
import Foundation
9-
import Alamofire
10-
import AlphaWalletFoundation
119
import Combine
10+
import AlphaWalletCore
11+
import AlphaWalletFoundation
1212
import AlphaWalletOpenSea
13+
import Alamofire
1314

1415
protocol MediaContentNetworking {
1516
func dataTaskPublisher(_ request: URLRequestConvertible) -> AnyPublisher<URLRequest.Response, SessionTaskError>

AlphaWallet/Common/Views/AddressOrEnsNameLabel.swift

+14-14
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,23 @@ class AddressOrEnsNameLabel: UILabel {
136136

137137
let promise = Promise<BlockieAndAddressOrEnsResolution> { seal in
138138
if let address = AlphaWallet.Address(string: value) {
139-
cancelable?.cancel()
140-
cancelable = domainResolutionService.resolveEnsAndBlockie(address: address, server: server)
141-
.sink(receiveCompletion: { _ in
142-
seal.fulfill((nil, .resolved(.none)))
143-
}, receiveValue: { value in
139+
Task { @MainActor in
140+
do {
141+
let blockieAndResolution = try await domainResolutionService.resolveEnsAndBlockie(address: address, server: server)
142+
seal.fulfill(blockieAndResolution)
143+
} catch {
144144
self.clearCurrentlyResolvingIf(value: valueArg)
145-
seal.fulfill(value)
146-
})
145+
}
146+
}
147147
} else if value.contains(".") {
148-
cancelable?.cancel()
149-
cancelable = domainResolutionService.resolveAddressAndBlockie(string: value)
150-
.sink(receiveCompletion: { _ in
151-
seal.fulfill((nil, .resolved(.none)))
152-
}, receiveValue: { value in
148+
Task { @MainActor in
149+
do {
150+
let blockieAndResolution = try await domainResolutionService.resolveAddressAndBlockie(string: value)
151+
seal.fulfill(blockieAndResolution)
152+
} catch {
153153
self.clearCurrentlyResolvingIf(value: valueArg)
154-
seal.fulfill(value)
155-
})
154+
}
155+
}
156156
} else {
157157
seal.fulfill((nil, .resolved(.none)))
158158
}

AlphaWallet/Market/ImportMagicLinkController.swift

+18-13
Original file line numberDiff line numberDiff line change
@@ -305,20 +305,19 @@ final class ImportMagicLinkController {
305305
}
306306

307307
private func makeTokenHolder(_ bytes32Tokens: [String], _ contractAddress: AlphaWallet.Address) {
308-
assetDefinitionStore.fetchXML(forContract: contractAddress, server: server, useCacheAndFetch: true) { [weak self, session] _ in
309-
guard let strongSelf = self else { return }
310-
311-
func makeTokenHolder(name: String, symbol: String, type: TokenType? = nil) {
312-
strongSelf.makeTokenHolderImpl(name: name, symbol: symbol, type: type, bytes32Tokens: bytes32Tokens, contractAddress: contractAddress)
313-
strongSelf.updateTokenFields()
308+
Task { @MainActor in
309+
_ = await assetDefinitionStore.fetchXMLAsync(forContract: contractAddress, server: server, useCacheAndFetch: true)
310+
func makeTokenHolder(name: String, symbol: String, type: TokenType? = nil) async {
311+
await makeTokenHolderImpl(name: name, symbol: symbol, type: type, bytes32Tokens: bytes32Tokens, contractAddress: contractAddress)
312+
updateTokenFields()
314313
}
315314

316-
if let existingToken = strongSelf.tokensService.tokenViewModel(for: contractAddress, server: strongSelf.server) {
317-
let name = XMLHandler(token: existingToken, assetDefinitionStore: strongSelf.assetDefinitionStore).getLabel(fallback: existingToken.name)
318-
makeTokenHolder(name: name, symbol: existingToken.symbol)
315+
if let existingToken = await tokensService.tokenViewModel(for: contractAddress, server: server) {
316+
let name = XMLHandler(token: existingToken, assetDefinitionStore: assetDefinitionStore).getLabel(fallback: existingToken.name)
317+
await makeTokenHolder(name: name, symbol: existingToken.symbol)
319318
} else {
320319
let localizedTokenTypeName = R.string.localizable.tokensTitlecase()
321-
makeTokenHolder(name: localizedTokenTypeName, symbol: "")
320+
await makeTokenHolder(name: localizedTokenTypeName, symbol: "")
322321

323322
let getContractName = session.tokenProvider.getContractName(for: contractAddress)
324323
let getContractSymbol = session.tokenProvider.getContractSymbol(for: contractAddress)
@@ -328,15 +327,21 @@ final class ImportMagicLinkController {
328327
.sinkAsync(receiveCompletion: { _ in
329328
//no-op
330329
}, receiveValue: { name, symbol, type in
331-
makeTokenHolder(name: name, symbol: symbol, type: type)
330+
Task { @MainActor in
331+
await makeTokenHolder(name: name, symbol: symbol, type: type)
332+
}
332333
})
333334
}
334335
}
335336
}
336337

337-
private func makeTokenHolderImpl(name: String, symbol: String, type: TokenType? = nil, bytes32Tokens: [String], contractAddress: AlphaWallet.Address) {
338+
private func makeTokenHolderImpl(name: String, symbol: String, type: TokenType? = nil, bytes32Tokens: [String], contractAddress: AlphaWallet.Address) async {
338339
//TODO pass in the wallet instead
339-
guard let tokenType = type ?? (tokensService.tokenViewModel(for: contractAddress, server: server)?.type) else { return }
340+
var tokenType1: TokenType? = type
341+
if tokenType1 == nil {
342+
tokenType1 = await tokensService.tokenViewModel(for: contractAddress, server: server)?.type
343+
}
344+
guard let tokenType = tokenType1 else { return }
340345
var tokens = [TokenScript.Token]()
341346
let xmlHandler = XMLHandler(contract: contractAddress, tokenType: tokenType, assetDefinitionStore: assetDefinitionStore)
342347
for i in 0..<bytes32Tokens.count {

0 commit comments

Comments
 (0)