// // InheritanceService.swift // Wallet // // Created by Igor on 11.03.2021. // Copyright © 2021 AM. All rights reserved. // import Foundation import Combine import WalletFoundation import WalletKit extension Inheritance { final class Service: Common.Service.Provider { var didUpdate: ((NSError?, Bool) -> Void )? let information = Information() let isSwapActive = AppSettingsModel.shared().isSwapActive var menus: [Contract] { let basicMenus: [Contract] = [.cash, .list] return self.isSwapActive ? basicMenus.appending(.swap) : basicMenus } let records = Records() var activeWalletPublisher: AnyPublisher { Accounts().bank.activePublisher } } } extension Inheritance.Service { func fetch(username: String? = nil) { self.didUpdate?(nil, true) self.information.fetch(username: username ?? Accounts().current?.name) { [weak self] in self?.records.fetch { err in self?.didUpdate?(err, false) } } } func refresh(privateKey: String, completion: @escaping Completion.Network) { guard self.canRefresh else { completion(nil) return } let data = [ "owner": self.information.object?.user_name ?? "", "inactive_period": "\(self.information.object?.inactive_period ?? 0)" ] Network.Service.Blockchain.execute(contract: self.information.contract, action: .updinhdate, data: data, privateKeys: [privateKey]) { if case .success = $0 { self.fetch() } completion(.error($0)) } } } extension Inheritance.Service { var canRefresh: Bool { self.information.object != nil } var menu: Contract { get { self.information.contract } set { self.information.contract = newValue self.records.contract = newValue self.fetch() } } } extension Inheritance.Service { typealias Contract = EOSContract }