Files
2023-01-23 23:15:06 +03:00

53 lines
1.4 KiB
Swift

//
// MessageBookShelfService.swift
// Malinka
//
// Created by Juraldinio on 1/19/23.
// Copyright © 2023 NUT.Tech. All rights reserved.
//
import Foundation
import WalletKit
import Combine
protocol MessageBookShelf {
}
final class MessageBookShelfService {
private let bank: Bank
private var cancellables = Set<AnyCancellable>()
private var shelves = [String: Account.Service.AccountHistory]()
private var wallet: WalletKit.Wallet?
var activeBook: Account.Service.AccountHistory? {
guard let wallet = self.wallet else { return nil }
return self.book(for: wallet)
}
init(with bank: Bank) {
self.bank = bank
bank.activePublisher
.sink { [weak self] wallet in
self?.wallet = wallet
// try? self.load(username: Accounts().current?.name)
}
.store(in: &self.cancellables)
}
func book(for wallet: WalletKit.Wallet) -> Account.Service.AccountHistory? {
let name = wallet.name
guard !name.isEmpty else { return nil }
if let book = self.shelves[name] { return book }
guard let book = try? Account.Service.AccountHistory(username: name) else { return nil }
self.shelves[name] = book
return self.shelves[name]
}
func fetchAll() { self.shelves.values.forEach { $0.fetch() } }
}