Skip to content

Commit

Permalink
Improved EthWalletService initialisation.
Browse files Browse the repository at this point in the history
Fixed dependency cycle.
  • Loading branch information
RealBonus committed Nov 19, 2018
1 parent f39a3fd commit fe0d586
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
51 changes: 49 additions & 2 deletions Adamant/Services/AdamantAccountService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class AdamantAccountService: AccountService {

var apiService: ApiService!
var adamantCore: AdamantCore!
var notificationsService: NotificationsService!
weak var notificationsService: NotificationsService!
var dialogService: DialogService!
var securedStore: SecuredStore! {
didSet {
securedStoreSemaphore.wait()
Expand Down Expand Up @@ -86,9 +87,55 @@ class AdamantAccountService: AccountService {
// MARK: Wallets
var wallets: [WalletService] = [
AdmWalletService(),
try! EthWalletService(apiUrl: AdamantResources.ethServers.first!), // TODO: Move to background thread
EthWalletService(), // TODO: Move to background thread
// LskWalletService()
]

init() {
guard let ethWallet = wallets[1] as? EthWalletService else {
fatalError("Failed to get EthWalletService")
}

ethWallet.initiateNetwork(apiUrl: AdamantResources.ethServers.first!) { result in
switch result {
case .success:
break

case .failure(let error):
switch error {
case .networkError:
NotificationCenter.default.addObserver(forName: Notification.Name.AdamantReachabilityMonitor.reachabilityChanged, object: nil, queue: nil) { notification in
guard let connection = notification.userInfo?[AdamantUserInfoKey.ReachabilityMonitor.connection] as? AdamantConnection else {
return
}

switch connection {
case .none:
break

case .wifi, .cellular:
ethWallet.initiateNetwork(apiUrl: AdamantResources.ethServers.first!) { result in
switch result {
case .success:
NotificationCenter.default.removeObserver(self, name: Notification.Name.AdamantReachabilityMonitor.reachabilityChanged, object: nil)

case .failure(let error):
self.dialogService.showRichError(error: error)
}
}
}
}

case .notLogged, .transactionNotFound, .notEnoughtMoney, .accountNotFound, .walletNotInitiated, .invalidAmount:
break

case .remoteServiceError, .apiError, .internalError:
self.dialogService.showRichError(error: error)
self.wallets.remove(at: 1)
}
}
}
}
}

// MARK: - Saved data
Expand Down
14 changes: 7 additions & 7 deletions Adamant/SwinjectDependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ extension Container {
func registerAdamantServices() {
// MARK: - Standalone services
// MARK: AdamantCore
self.register(AdamantCore.self) { _ in
let core = NativeAdamantCore()
return core
}.inObjectScope(.container)
self.register(AdamantCore.self) { _ in NativeAdamantCore() }.inObjectScope(.container)

// MARK: Router
self.register(Router.self) { _ in
Expand Down Expand Up @@ -81,9 +78,12 @@ extension Container {
service.apiService = r.resolve(ApiService.self)!
service.adamantCore = r.resolve(AdamantCore.self)!
service.securedStore = r.resolve(SecuredStore.self)!
service.notificationsService = r.resolve(NotificationsService.self)!
return service
}.inObjectScope(.container).initCompleted { (r, service) in
service.dialogService = r.resolve(DialogService.self)!
return service
}.inObjectScope(.container).initCompleted { (r, c) in
let service = c as! AdamantAccountService
service.notificationsService = r.resolve(NotificationsService.self)!

for case let wallet as SwinjectDependentService in service.wallets {
wallet.injectDependencies(from: self)
}
Expand Down
26 changes: 15 additions & 11 deletions Adamant/Wallets/Ethereum/EthWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ class EthWalletService: WalletService {

private static let transactionsListApiSubpath = "ethtxs"

let web3: web3
private let baseUrl: String
private(set) var web3: web3!
private var baseUrl: String!
let defaultDispatchQueue = DispatchQueue(label: "im.adamant.ethWalletService", qos: .utility, attributes: [.concurrent])
private (set) var enabled = true

Expand Down Expand Up @@ -121,15 +121,7 @@ class EthWalletService: WalletService {
private var balanceObserver: NSObjectProtocol? = nil

// MARK: - Logic
init(apiUrl: String) throws {
// Init network
guard let url = URL(string: apiUrl), let web3 = Web3.new(url) else {
throw WalletServiceError.networkError
}

self.web3 = web3
self.baseUrl = EthWalletService.buildBaseUrl(for: web3.provider.network)

init() {
// Notifications
NotificationCenter.default.addObserver(forName: Notification.Name.AdamantAccountService.userLoggedIn, object: nil, queue: nil) { [weak self] _ in
self?.update()
Expand All @@ -148,6 +140,18 @@ class EthWalletService: WalletService {
}
}
}

func initiateNetwork(apiUrl: String, completion: @escaping (WalletServiceSimpleResult) -> Void) {
DispatchQueue.global(qos: .userInitiated).async {
guard let url = URL(string: apiUrl), let web3 = Web3.new(url) else {
completion(.failure(error: WalletServiceError.networkError))
return
}

self.web3 = web3
self.baseUrl = EthWalletService.buildBaseUrl(for: web3.provider.network)
}
}

func update() {
guard let wallet = ethWallet else {
Expand Down

0 comments on commit fe0d586

Please sign in to comment.