RoomsListView.swift 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //
  2. // RoomsListView.swift
  3. // Chat
  4. //
  5. // Created by Sergey Tarasov on 10.08.2022.
  6. //
  7. import SwiftUI
  8. struct RoomsListView: View {
  9. @Environment(\.colorScheme) var colorScheme
  10. @StateObject var viewModel: RoomsListViewModel = RoomsListViewModel()
  11. var body: some View {
  12. Group {
  13. if viewModel.rooms.isEmpty {
  14. ProgressView()
  15. } else {
  16. List(viewModel.rooms.indices, id: \.self) { index in
  17. RoomsListCellView(
  18. roomID: viewModel.rooms[index].jidString,
  19. title: viewModel.rooms[index].name,
  20. lastMessage: "Сообщение \(index)",
  21. lastMessageTime: "14:\(Int.random(in: 10...50))",
  22. unreadMessagesCount: Int.random(in: 1...100)
  23. )
  24. }
  25. .listStyle(.plain)
  26. }
  27. }
  28. .onAppear {
  29. viewModel.fetchRooms()
  30. }
  31. .navigationBarTitleDisplayMode(.inline)
  32. }
  33. }
  34. struct RoomsListView_Previews: PreviewProvider {
  35. static var viewModel: RoomsListViewModel = RoomsListViewModel()
  36. static var previews: some View {
  37. NavigationView {
  38. RoomsListView(viewModel: viewModel)
  39. .onAppear {
  40. viewModel.rooms = [
  41. .init(jidString: "Jid", name: "Room title 1"),
  42. .init(jidString: "Jid", name: "Room title 2"),
  43. .init(jidString: "Jid", name: "Room title 3"),
  44. .init(jidString: "Jid", name: "Room title 4"),
  45. .init(jidString: "Jid", name: "Room title 5"),
  46. .init(jidString: "Jid", name: "Room title 6"),
  47. .init(jidString: "Jid", name: "Room title 7")
  48. ]
  49. }
  50. }
  51. }
  52. }