// // ChatListViewModel.swift // Chat // // Created by Sergey Tarasov on 25.07.2022. // import Foundation import XMPPFramework import XMPPFrameworkSwift final class ChatListViewModel: NSObject, ObservableObject { let manager = XMPPController.shared @Published var users: [User] = [] var nicknames: [String] { return users.map({ $0.nickname.isEmpty ? $0.jidString : $0.nickname }) } var timer: Timer? override init() { super.init() self.timer = Timer(timeInterval: 2, repeats: true, block: { [weak self] timer in self?.fetchChatList() if !(self?.users.isEmpty ?? true) { timer.invalidate() } }) } public func fetchChatList() { let xmppUsers = manager.xmppRosterStorage.sortedUsersByAvailabilityName() as? [XMPPUser] guard let users = xmppUsers?.compactMap({ User(jidString: $0.jid.bare, nickname: $0.nickname) }) else { return } self.users = users } }