RoomsListViewModel.swift 751 B

12345678910111213141516171819202122232425262728293031323334353637
  1. //
  2. // RoomsListViewModel.swift
  3. // Chat
  4. //
  5. // Created by Sergey Tarasov on 10.08.2022.
  6. //
  7. import Foundation
  8. import XMPPFramework
  9. import XMPPFrameworkSwift
  10. class RoomsListViewModel: ObservableObject {
  11. let manager = MUCManager()
  12. @Published var rooms: [Room] = []
  13. var timer: Timer?
  14. init() {
  15. self.timer = Timer.scheduledTimer(
  16. timeInterval: 2,
  17. target: self,
  18. selector: #selector(fetchChatList),
  19. userInfo: nil,
  20. repeats: true
  21. )
  22. }
  23. @objc
  24. public func fetchChatList() {
  25. if rooms.isEmpty {
  26. self.manager.fetchRooms()
  27. self.rooms = self.manager.rooms
  28. } else {
  29. self.timer?.invalidate()
  30. }
  31. }
  32. }