61 lines
1.9 KiB
Swift
61 lines
1.9 KiB
Swift
//
|
|
// CCCPServicePriority.swift
|
|
// PayCash
|
|
//
|
|
// Created by Saveliy Stavitsky on 9/21/20.
|
|
// Copyright © 2020 List. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension CCCP.Service {
|
|
class Priority {
|
|
|
|
init() { }
|
|
|
|
private let serialQueue = DispatchQueue(label: "paycash.serial.queue.CCCP.Service.Priority.requests")
|
|
var allPriority = [String: Bool]() {
|
|
didSet {
|
|
if !isLoading {
|
|
didUpdate?()
|
|
}
|
|
}
|
|
}
|
|
|
|
private var isLoadingCounter = 0
|
|
private var isLoading: Bool { isLoadingCounter != 0 }
|
|
|
|
var didUpdate: (() -> Void)?
|
|
|
|
func fetch() {
|
|
guard !isLoading, let username = Accounts().current?.username else { return }
|
|
|
|
isLoadingCounter += 1
|
|
|
|
self.isLoadingCounter = Wallet.Service.Tokens.shared.cashTokens.count
|
|
self.allPriority = [:]
|
|
|
|
for cashToken in Wallet.Service.Tokens.shared.cashTokens {
|
|
Network.table.fetch(code: Network.servers.current.contract(.cash),
|
|
table: "income",
|
|
scope: cashToken.symbol,
|
|
limit: 1,
|
|
keyType: "string",
|
|
lowerBound: username,
|
|
upperBound: username,
|
|
type: PriorityValue.self) { rows in
|
|
print("success: \(self.isLoadingCounter), \(rows.count)")
|
|
self.serialQueue.async {
|
|
self.isLoadingCounter -= 1
|
|
self.allPriority[cashToken.symbol] = rows.first?.is_priority == 1
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct PriorityValue: Codable {
|
|
let is_priority: Int //swiftlint:disable:this identifier_name
|
|
}
|
|
}
|
|
}
|