Эх сурвалжийг харах

Move view models into own views

Sergey 2 жил өмнө
parent
commit
0c72a672f8

+ 8 - 6
Chat/UI/Chat/ChatView.swift

@@ -15,7 +15,7 @@ extension CGFloat {
 
 struct ChatView: View, KeyboardReadable {
 
-    @ObservedObject var viewModel: ChatViewModel
+    @StateObject var viewModel: ChatViewModel
 
     @State private var isKeyboardVisible = false
 
@@ -29,17 +29,19 @@ struct ChatView: View, KeyboardReadable {
                                 HStack {
                                     if message.fromID == XMPPController.shared.xmppStream.myJID?.bare {
                                         Spacer(minLength: 40)
-                                        MessageView(text: message.body ?? "-", time: "", isSelf: true)
+										MessageView(text: message.body, time: "", isSelf: true)
                                     } else {
-                                        MessageView(text: message.body ?? "-", time: "", isSelf: false)
+										MessageView(text: message.body, time: "", isSelf: false)
                                         Spacer(minLength: 40)
                                     }
                                 }
                             }
                             .onChange(of: viewModel.messages) { _ in
-                                withAnimation {
-                                    value.scrollTo(viewModel.messages.count - 1, anchor: .bottomTrailing)
-                                }
+								DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
+									withAnimation {
+										value.scrollTo(viewModel.messages.count - 1, anchor: .bottomTrailing)
+									}
+								}
                             }
                             .onReceive(keyboardPublisher) { newIsKeyboardVisible in
                                 isKeyboardVisible = newIsKeyboardVisible

+ 10 - 15
Chat/UI/ChatList/ChatListView.swift

@@ -11,7 +11,8 @@ import XMPPFrameworkSwift
 
 struct ChatListView: View {
     @Environment(\.colorScheme) var colorScheme
-    @ObservedObject var viewModel: ChatListViewModel
+
+	@StateObject var viewModel: ChatListViewModel = ChatListViewModel()
 
     var body: some View {
         Group {
@@ -40,19 +41,13 @@ struct ChatListView: View {
                         }
                     }
                 }
-                .listStyle(.plain)
-            }
-        }
+				.listStyle(.plain)
+			}
+		}
+		.onAppear {
+			viewModel.fetchChatList()
+		}
         .navigationBarTitleDisplayMode(.inline)
-        .toolbar {
-            ToolbarItem(placement: .navigationBarTrailing) {
-                Button {
-                    viewModel.fetchChatList()
-                } label: {
-                    Image(systemName: "arrow.counterclockwise")
-                }
-            }
-        }
     }
 }
 
@@ -61,10 +56,10 @@ struct ChatListView_Previews: PreviewProvider {
     static var previews: some View {
         Group {
             NavigationView {
-                ChatListView(viewModel: viewModel)
+                ChatListView()
             }
             NavigationView {
-                ChatListView(viewModel: viewModel)
+                ChatListView()
             }
             .preferredColorScheme(.dark)
         }

+ 1 - 11
Chat/UI/ChatList/ChatListViewModel.swift

@@ -10,28 +10,18 @@ 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]
+		let xmppUsers = XMPPController.shared.xmppRosterStorage.sortedUsersByAvailabilityName() as? [XMPPUser]
 		guard let users = xmppUsers?.compactMap({ User(jidString: $0.jid.bare, nickname: $0.nickname) }) else {
 			return
 		}

+ 2 - 4
Chat/UI/ContentView.swift

@@ -8,8 +8,6 @@
 import SwiftUI
 
 struct ContentView: View {
-    @ObservedObject var contactsViewModel: ChatListViewModel = ChatListViewModel()
-    @ObservedObject var roomsViewModel: RoomsListViewModel = RoomsListViewModel()
     @ObservedObject var loginViewModel: LoginViewModel = LoginViewModel()
 
     @State var authStatus: Bool = false
@@ -19,14 +17,14 @@ struct ContentView: View {
             if authStatus {
                 NavigationView {
                     TabView(selection: $selectedTab) {
-                        ChatListView(viewModel: contactsViewModel)
+                        ChatListView()
                             .navigationViewStyle(StackNavigationViewStyle())
                             .tabItem {
 								Label(Tab.contacts.string, systemImage: "person.crop.circle")
                             }
                             .tag(Tab.contacts)
 
-                        RoomsListView(viewModel: roomsViewModel)
+                        RoomsListView()
                             .navigationViewStyle(StackNavigationViewStyle())
                             .tabItem {
 								Label(Tab.rooms.string, systemImage: "bubble.left.and.bubble.right")

+ 5 - 10
Chat/UI/Rooms/RoomsListView.swift

@@ -9,7 +9,8 @@ import SwiftUI
 
 struct RoomsListView: View {
     @Environment(\.colorScheme) var colorScheme
-    @ObservedObject var viewModel: RoomsListViewModel
+
+	@StateObject var viewModel: RoomsListViewModel = RoomsListViewModel()
     var body: some View {
         Group {
             if viewModel.rooms.isEmpty {
@@ -65,16 +66,10 @@ struct RoomsListView: View {
                 .listStyle(.plain)
             }
         }
+		.onAppear {
+			viewModel.fetchRooms()
+		}
         .navigationBarTitleDisplayMode(.inline)
-        .toolbar {
-            ToolbarItem(placement: .navigationBarTrailing) {
-                Button {
-                    viewModel.fetchRooms()
-                } label: {
-                    Image(systemName: "arrow.counterclockwise")
-                }
-            }
-        }
     }
 }
 

+ 0 - 8
Chat/UI/Rooms/RoomsListViewModel.swift

@@ -13,19 +13,11 @@ final class RoomsListViewModel: NSObject, ObservableObject {
 	let muc: XMPPMUCLight = XMPPMUCLight()
 
 	@Published var rooms: [Room] = []
-	var timer: Timer?
 
 	override init() {
 		super.init()
 		self.muc.activate(XMPPController.shared.xmppStream)
 		self.muc.addDelegate(self, delegateQueue: .main)
-
-		self.timer = Timer(timeInterval: 2, repeats: true, block: { [weak self] timer in
-			self?.fetchRooms()
-			if !(self?.rooms.isEmpty ?? false) {
-				timer.invalidate()
-			}
-		})
 	}
 
 	func fetchRooms() {