// // ChatListViewModel.swift // Chat // // Created by Sergey Tarasov on 25.07.2022. // import Foundation import XMPPFramework import XMPPFrameworkSwift class ChatListViewModel: ObservableObject { let manager = RosterManager() @Published var users: [User] = [] var nicknames: [String] { return users.map({ $0.nickname.isEmpty ? $0.jidString : $0.nickname }) } var timer: Timer? init() { self.timer = Timer.scheduledTimer( timeInterval: 2, target: self, selector: #selector(fetchChatList), userInfo: nil, repeats: true ) } @objc public func fetchChatList() { if users.isEmpty { let xmppUsers = manager.storage.sortedUsersByAvailabilityName() as? [XMPPUser] guard let users = xmppUsers?.compactMap({ User(jidString: $0.jid.bare, nickname: $0.nickname) }) else { return } self.users = users } else { self.timer?.invalidate() } } }