80 lines
2.7 KiB
Swift
80 lines
2.7 KiB
Swift
//
|
|
// AccountServiceContacts.swift
|
|
// Wallet
|
|
//
|
|
// Created by Igor on 04.02.2021.
|
|
// Copyright © 2021 AM. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import RealmSwift
|
|
|
|
extension Account.Service {
|
|
|
|
final class Contacts: CommonMenuAutocompleteService {
|
|
|
|
private lazy var exclude: [String] = {
|
|
var exclude = [
|
|
"eosio.ram",
|
|
"eosio.ramfee",
|
|
"eosio.stake",
|
|
""
|
|
]
|
|
exclude.append(contentsOf: ApplicationEnvironment.contracts())
|
|
return exclude
|
|
}()
|
|
|
|
private var contactsList = [String: Int]()
|
|
private var tempCollection = [String: Int]()
|
|
var collection: [Common.Model.Menu] {
|
|
tempCollection.map { AccountModelContact(account: $0.key, rating: $0.value) }
|
|
.sorted(by: { $0.rating > $1.rating })
|
|
.map({ $0.toMenu() })
|
|
}
|
|
var didUpdate: (() -> Void)?
|
|
private var filter: String?
|
|
|
|
func apply(filter: String?) {
|
|
self.filter = filter
|
|
reload(filter: filter)
|
|
if let filter = filter?.lowercased(), !filter.isEmpty {
|
|
Network.accounts.exists(account: filter) { (exists) in
|
|
if exists, self.filter == filter {
|
|
self.tempCollection[filter.lowercased(), default: 0] += 100000
|
|
}
|
|
self.didUpdate?()
|
|
}
|
|
} else {
|
|
didUpdate?()
|
|
}
|
|
}
|
|
|
|
private func preloadContactsList() {
|
|
|
|
guard contactsList.isEmpty else { return }
|
|
guard let receiver = Accounts().current?.name else { return }
|
|
guard let actions = Accounts().messageShelf.activeBook?.actions else { return }
|
|
|
|
actions
|
|
.where {
|
|
$0.username == receiver
|
|
&& $0.receiver == receiver
|
|
&& $0.name.in(["transfer", "notify", "retire", Network.Model.Blockchain.Action.chatSendDm.rawValue])
|
|
&& !$0.withUsername.in(exclude)
|
|
}
|
|
.sectioned(by: \.withUsername)
|
|
.sorted(by: { $0.count > $1.count })
|
|
.forEach { self.contactsList[$0.key, default: 0] = $0.count }
|
|
}
|
|
|
|
private func reload(filter: String? = nil) {
|
|
preloadContactsList()
|
|
tempCollection = contactsList
|
|
if let filter = filter?.lowercased(), !filter.isEmpty {
|
|
tempCollection = tempCollection.filter({ $0.key.contains(filter) })
|
|
}
|
|
// collection.sort(by: { $0.rating > $1.rating })
|
|
}
|
|
}
|
|
}
|