Skip to content

Commit

Permalink
fix balance info type for balance info module
Browse files Browse the repository at this point in the history
  • Loading branch information
Viscount88 committed Oct 9, 2023
1 parent 1bbd882 commit ca7a722
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ protocol WalletBalanceSubscriptionAdapterProtocol {
deliverOn queue: DispatchQueue?,
listener: WalletBalanceSubscriptionListener
)

func unsubscribe(listener: WalletBalanceSubscriptionListener)
}

enum WalletBalanceError: Error {
Expand Down Expand Up @@ -117,9 +119,11 @@ final class WalletBalanceSubscriptionAdapter: WalletBalanceSubscriptionAdapterPr
deliverQueue = queue
let weakListener = WeakWrapper(target: listener)
listeners.append(weakListener)

if let balances = buildBalance(for: [wallet], chainAssets: chainAssets.map { $0.value }) {
notify(listener: listener, result: .success(balances))
Task {
let cas = await getChainAssets()
if let balances = buildBalance(for: [wallet], chainAssets: cas) {
notify(listener: listener, result: .success(balances))
}
}
}

Expand All @@ -130,9 +134,11 @@ final class WalletBalanceSubscriptionAdapter: WalletBalanceSubscriptionAdapterPr
deliverQueue = queue
let weakListener = WeakWrapper(target: listener)
listeners.append(weakListener)

if let balances = buildBalance(for: metaAccounts, chainAssets: chainAssets.map { $0.value }) {
notify(listener: listener, result: .success(balances))
Task {
let cas = await getChainAssets()
if let balances = buildBalance(for: metaAccounts, chainAssets: cas) {
notify(listener: listener, result: .success(balances))
}
}
}

Expand All @@ -145,12 +151,20 @@ final class WalletBalanceSubscriptionAdapter: WalletBalanceSubscriptionAdapterPr
deliverQueue = queue
let weakListener = WeakWrapper(target: listener)
listeners.append(weakListener)

if let balances = buildBalance(for: [wallet], chainAssets: [chainAsset]) {
notify(listener: listener, result: .success(balances))
}
}

func unsubscribe(listener: WalletBalanceSubscriptionListener) {
listeners = listeners.filter {
if let target = $0.target as? WalletBalanceSubscriptionListener {
return target !== listener
}
return true
}
}

// MARK: - Private methods

private func buildBalance(for wallets: [MetaAccountModel], chainAssets: [ChainAsset]) -> WalletBalanceInfos? {
Expand All @@ -160,7 +174,6 @@ final class WalletBalanceSubscriptionAdapter: WalletBalanceSubscriptionAdapterPr
chainAssets,
prices
)

return walletBalances
}

Expand Down Expand Up @@ -284,22 +297,25 @@ final class WalletBalanceSubscriptionAdapter: WalletBalanceSubscriptionAdapterPr
}
return nil
}
unwrappedListeners.forEach { listener in
switch listener.type {
case .wallets:
if let balances = buildBalance(for: self.metaAccounts, chainAssets: self.chainAssets.map { $0.value }) {
notify(listener: listener, result: .success(balances))
}
case let .wallet(wallet):
if updatedWallets.contains(wallet) {
if let balances = buildBalance(for: [wallet], chainAssets: self.chainAssets.map { $0.value }) {
Task {
let cas = await getChainAssets()
unwrappedListeners.forEach { listener in
switch listener.type {
case .wallets:
if let balances = buildBalance(for: metaAccounts, chainAssets: cas) {
notify(listener: listener, result: .success(balances))
}
}
case let .chainAsset(wallet, chainAsset):
if updatedWallets.contains(wallet), updatedChainAssets.contains(chainAsset) {
if let balances = buildBalance(for: [wallet], chainAssets: [chainAsset]) {
notify(listener: listener, result: .success(balances))
case let .wallet(wallet):
if updatedWallets.contains(wallet) {
if let balances = buildBalance(for: [wallet], chainAssets: cas) {
notify(listener: listener, result: .success(balances))
}
}
case let .chainAsset(wallet, chainAsset):
if updatedWallets.contains(wallet), updatedChainAssets.contains(chainAsset) {
if let balances = buildBalance(for: [wallet], chainAssets: [chainAsset]) {
notify(listener: listener, result: .success(balances))
}
}
}
}
Expand All @@ -322,35 +338,12 @@ extension WalletBalanceSubscriptionAdapter: EventVisitorProtocol {
}

func processSelectedAccountChanged(event: SelectedAccountChanged) {
if chainAssets.isEmpty {
let unwrappedListeners = listeners.compactMap {
if let target = $0.target as? WalletBalanceSubscriptionListener {
return target
}
return nil
}
let chainsOperation = fetchChainsOperation()
chainsOperation.completionBlock = { [weak self] in
guard let result = chainsOperation.result else { return }
switch result {
case let .success(cas):
if self?.metaAccounts.contains(event.account) == true {
self?.notifyIfNeeded(with: [event.account], updatedChainAssets: cas)
} else {
self?.handle([event.account], cas)
}
case let .failure(error):
unwrappedListeners.forEach { [weak self] in
self?.notify(listener: $0, result: .failure(error))
}
}
}
operationQueue.addOperation(chainsOperation)
} else {
if metaAccounts.contains(event.account) {
notifyIfNeeded(with: [event.account], updatedChainAssets: chainAssets.map { $0.value })
Task {
let cas = await getChainAssets()
if metaAccounts.contains(event.account) == true {
notifyIfNeeded(with: [event.account], updatedChainAssets: cas)
} else {
handle([event.account], chainAssets.map { $0.value })
handle([event.account], cas)
}
}
}
Expand Down Expand Up @@ -393,6 +386,30 @@ extension WalletBalanceSubscriptionAdapter: AccountInfoSubscriptionAdapterHandle
)
}
}

private func getChainAssets() async -> [ChainAsset] {
let cas = chainAssets.map { $0.value }
if cas.isNotEmpty {
return cas
} else {
return await withCheckedContinuation { continuation in
let chainsOperation = fetchChainsOperation()
chainsOperation.completionBlock = {
if let result = chainsOperation.result {
switch result {
case let .success(cas):
continuation.resume(returning: cas)
case .failure:
continuation.resume(returning: [])
}
} else {
continuation.resume(returning: [])
}
}
operationQueue.addOperation(chainsOperation)
}
}
}
}

// MARK: - PriceLocalSubscriptionHandler
Expand All @@ -407,8 +424,11 @@ extension WalletBalanceSubscriptionAdapter: PriceLocalSubscriptionHandler {
}
switch result {
case let .success(prices):
self.prices = prices
notifyIfNeeded(with: metaAccounts, updatedChainAssets: chainAssets.map { $0.value })
Task {
self.prices = prices
let cas = await getChainAssets()
notifyIfNeeded(with: metaAccounts, updatedChainAssets: cas)
}
case let .failure(error):
unwrappedListeners.forEach { listener in
notify(listener: listener, result: .failure(error))
Expand Down
1 change: 0 additions & 1 deletion fearless/Modules/BalanceInfo/BalanceInfoAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ enum BalanceInfoAssembly {
)

let presenter = BalanceInfoPresenter(
balanceInfoType: type,
balanceInfoViewModelFactoryProtocol: balanceInfoViewModelFactory,
interactor: interactor,
router: router,
Expand Down
18 changes: 9 additions & 9 deletions fearless/Modules/BalanceInfo/BalanceInfoInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class BalanceInfoInteractor {

private weak var output: BalanceInfoInteractorOutput?

private let balanceInfoType: BalanceInfoType
var balanceInfoType: BalanceInfoType
private let walletBalanceSubscriptionAdapter: WalletBalanceSubscriptionAdapterProtocol
private let operationManager: OperationManagerProtocol
private let storageRequestFactory: StorageRequestFactoryProtocol
Expand All @@ -31,16 +31,15 @@ final class BalanceInfoInteractor {

extension BalanceInfoInteractor: BalanceInfoInteractorInput {
func setup(
with output: BalanceInfoInteractorOutput,
for type: BalanceInfoType
with output: BalanceInfoInteractorOutput
) {
self.output = output
fetchBalanceInfo(for: type)
fetchBalanceInfo()
}

func fetchBalanceInfo(for type: BalanceInfoType) {
fetchBalance(for: type)
if case let .chainAsset(wallet, chainAsset) = type {
func fetchBalanceInfo() {
subscribeOnBalance()
if case let .chainAsset(wallet, chainAsset) = balanceInfoType {
guard let dependencies = dependencyContainer.prepareDepencies(chainAsset: chainAsset) else {
return
}
Expand All @@ -60,8 +59,9 @@ extension BalanceInfoInteractor: BalanceInfoInteractorInput {
}

private extension BalanceInfoInteractor {
func fetchBalance(for type: BalanceInfoType) {
switch type {
func subscribeOnBalance() {
walletBalanceSubscriptionAdapter.unsubscribe(listener: self)
switch balanceInfoType {
case let .wallet(metaAccount):
walletBalanceSubscriptionAdapter.subscribeWalletBalance(
wallet: metaAccount,
Expand Down
27 changes: 12 additions & 15 deletions fearless/Modules/BalanceInfo/BalanceInfoPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ final class BalanceInfoPresenter {
private let interactor: BalanceInfoInteractorInput
private let eventCenter: EventCenterProtocol

private var balanceInfoType: BalanceInfoType
private let balanceInfoViewModelFactoryProtocol: BalanceInfoViewModelFactoryProtocol
private let logger: LoggerProtocol

Expand All @@ -21,15 +20,13 @@ final class BalanceInfoPresenter {
// MARK: - Constructors

init(
balanceInfoType: BalanceInfoType,
balanceInfoViewModelFactoryProtocol: BalanceInfoViewModelFactoryProtocol,
interactor: BalanceInfoInteractorInput,
router: BalanceInfoRouterInput,
logger: LoggerProtocol,
localizationManager: LocalizationManagerProtocol,
eventCenter: EventCenterProtocol
) {
self.balanceInfoType = balanceInfoType
self.balanceInfoViewModelFactoryProtocol = balanceInfoViewModelFactoryProtocol
self.interactor = interactor
self.router = router
Expand All @@ -44,7 +41,7 @@ final class BalanceInfoPresenter {

private func buildBalance() {
let viewModel = balanceInfoViewModelFactoryProtocol.buildBalanceInfo(
with: balanceInfoType,
with: interactor.balanceInfoType,
balances: balances,
infoButtonEnabled: createBalanceContext() != nil,
locale: selectedLocale
Expand All @@ -54,7 +51,7 @@ final class BalanceInfoPresenter {
}

private func createBalanceContext() -> BalanceContext? {
guard case let .chainAsset(wallet, chainAsset) = balanceInfoType,
guard case let .chainAsset(wallet, chainAsset) = interactor.balanceInfoType,
let balance = balances[wallet.metaId] else {
return nil
}
Expand Down Expand Up @@ -91,7 +88,7 @@ final class BalanceInfoPresenter {
extension BalanceInfoPresenter: BalanceInfoViewOutput {
func didLoad(view: BalanceInfoViewInput) {
self.view = view
interactor.setup(with: self, for: balanceInfoType)
interactor.setup(with: self)
view.didReceiveViewModel(nil)
}
}
Expand Down Expand Up @@ -136,23 +133,23 @@ extension BalanceInfoPresenter: Localizable {

extension BalanceInfoPresenter: BalanceInfoModuleInput {
func replace(infoType: BalanceInfoType) {
balanceInfoType = infoType
interactor.fetchBalanceInfo(for: infoType)
interactor.balanceInfoType = infoType
interactor.fetchBalanceInfo()
}
}

extension BalanceInfoPresenter: EventVisitorProtocol {
func processSelectedAccountChanged(event: SelectedAccountChanged) {
switch balanceInfoType {
case let .chainAsset(wallet, chainAsset):
switch interactor.balanceInfoType {
case let .chainAsset(_, chainAsset):
let newType = BalanceInfoType.chainAsset(wallet: event.account, chainAsset: chainAsset)
balanceInfoType = newType
interactor.fetchBalanceInfo(for: newType)
interactor.balanceInfoType = newType
interactor.fetchBalanceInfo()

case let .wallet(wallet):
case .wallet:
let newType = BalanceInfoType.wallet(wallet: event.account)
balanceInfoType = newType
interactor.fetchBalanceInfo(for: newType)
interactor.balanceInfoType = newType
interactor.fetchBalanceInfo()
}
}
}
6 changes: 4 additions & 2 deletions fearless/Modules/BalanceInfo/BalanceInfoProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ protocol BalanceInfoViewOutput: AnyObject {
}

protocol BalanceInfoInteractorInput: AnyObject {
func setup(with output: BalanceInfoInteractorOutput, for type: BalanceInfoType)
func fetchBalanceInfo(for type: BalanceInfoType)
var balanceInfoType: BalanceInfoType { get set }

func setup(with output: BalanceInfoInteractorOutput)
func fetchBalanceInfo()
}

protocol BalanceInfoInteractorOutput: AnyObject {
Expand Down

0 comments on commit ca7a722

Please sign in to comment.