Files
raspberry/iOS/Wallet/Sources/Inheritance/Service/InheritanceServiceSetup.swift
2022-05-27 20:21:01 +03:00

131 lines
4.5 KiB
Swift

//
// InheritanceServiceSetup.swift
// PayCash
//
// Created by Igor on 15.03.2021.
// Copyright © 2021 AM. All rights reserved.
//
import Foundation
extension Common.Service.Provider.Key {
static let period = Self("period")
static let percent = Self("percent")
}
extension Inheritance.Service.Setup {
typealias Contract = Network.Model.Contract
typealias Model = Inheritance.Model.Share
typealias Token = Network.Model.Token
typealias Information = Inheritance.Model.Information
}
extension Inheritance.Service {
class Setup: Common.Service.Provider {
var didUpdate: (() -> Void)?
var didReload: (() -> Void)?
let contract : Contract
var period : Int = 0 { didSet { didUpdate?() } }
private(set) var collection = [Model]()
init(object: Information?, contract: Contract) {
self.contract = contract
if let object = object {
period = object.inactive_period/(3600*24)
collection = object.inheritors
.compactMap({ $0 })
.map({ .init(
account: $0.inheritor,
didChangeAccount: didChangeAccount(),
value: $0.share.amount.toDouble(),
didChangeValue: didChangeValue()
)})
} else { add() }
}
}
}
extension Inheritance.Service.Setup {
var totalPercent: Double { collection.map({ $0.value ?? 0 }).reduce(0, +) }
var errors: [Key:String] {
[
.period : period > 3650 ? L10n.Inheritance.Error.Setup.period : nil,
.percent : totalPercent > 100 ? L10n.Inheritance.Error.Setup.sum : nil
].compactMapValues({ $0 })
}
var isValid: Bool {
Int(totalPercent) == 100
&& collection.filter({ !$0.account.isEmpty }).count == collection.count
&& (period > 0 && period <= 3650)
}
}
extension Inheritance.Service.Setup {
var canAdd: Bool { collection.count < 3 }
func add() {
guard canAdd else { return }
let value = totalPercent >= 100 ? nil : 100 - totalPercent
collection.append(.init(didChangeAccount: didChangeAccount(), value: value, didChangeValue: didChangeValue()))
didReload?()
}
func delete(share: Model) {
collection.removeAll(where: { $0.uuid == share.uuid })
didReload?()
}
}
extension Inheritance.Service.Setup {
func submit(privateKey: String, completion: @escaping Completion.Network) {
Network.Service.Blockchain.execute(actions: [
(contract, .updinhdate, [
"owner" : Accounts().current?.username ?? "",
"inactive_period" : "\(period*3600*24)"
]),
(contract, .updtokeninhs, Submit(inheritors: collection.map({ .init(share: $0) })))
], privateKeys: [privateKey]) { result in
switch result {
case .success(let txnId):
Account.Service.History.shared.current?.fetchUntil(trxId: txnId) { _ in
completion(nil)
}
case .failure:
completion(.error(result))
}
}
}
}
extension Inheritance.Service.Setup {
private func didChangeAccount() -> (Model) -> Void { { _ in self.didUpdate?() } }
private func didChangeValue() -> (Model) -> Void { { [unowned self](share) in
if collection.count == 2, var index = collection.lastIndex(where: { $0.uuid == share.uuid }) {
index += (index == 0 ? 1 : -1)
let value = 100 - (share.value ?? 0)
let other = collection[index]
collection.remove(at: index)
collection.insert(.init(
account: other.account,
didChangeAccount: didChangeAccount(),
value: value == 0 ? nil : value,
didChangeValue: didChangeValue()
), at: index)
didReload?()
} else { didUpdate?() }
} }
private struct Submit: Encodable {
struct Inheritor: Encodable {
let inheritor: String
let share: String
init(share: Inheritance.Model.Share) {
self.inheritor = share.account
self.share = (share.value ?? 0).toString(precision: 1, symbol: "PERCENT")
}
}
let owner = Accounts().current?.username ?? ""
let inheritors: [Inheritor]
}
}