ChatListViewModel.swift 705 B

123456789101112131415161718192021222324252627282930
  1. //
  2. // ChatListViewModel.swift
  3. // Chat
  4. //
  5. // Created by Sergey Tarasov on 25.07.2022.
  6. //
  7. import Foundation
  8. import XMPPFramework
  9. import XMPPFrameworkSwift
  10. final class ChatListViewModel: NSObject, ObservableObject {
  11. @Published var users: [User] = []
  12. var nicknames: [String] {
  13. return users.map({ $0.nickname.isEmpty ? $0.jidString : $0.nickname })
  14. }
  15. override init() {
  16. super.init()
  17. }
  18. public func fetchChatList() {
  19. let xmppUsers = XMPPController.shared.xmppRosterStorage.sortedUsersByAvailabilityName() as? [XMPPUser]
  20. guard let users = xmppUsers?.compactMap({ User(jidString: $0.jid.bare, nickname: $0.nickname) }) else {
  21. return
  22. }
  23. self.users = users
  24. }
  25. }