RoomsListView.swift 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. @ObservedObject var viewModel: RoomsListViewModel
  11. var body: some View {
  12. Group {
  13. if viewModel.rooms.isEmpty {
  14. ProgressView()
  15. } else {
  16. List {
  17. ForEach(viewModel.rooms.indices, id: \.self) { index in
  18. ZStack {
  19. HStack {
  20. Image(systemName: "person.crop.circle")
  21. .resizable()
  22. .scaledToFit()
  23. .foregroundColor(.secondary.opacity(0.5))
  24. .padding(.vertical, 8)
  25. VStack(alignment: .leading, spacing: 0) {
  26. HStack {
  27. Text(viewModel.rooms[index].name)
  28. Spacer()
  29. Text("23:04")
  30. .font(.callout)
  31. .foregroundColor(.secondary)
  32. }
  33. .padding(.vertical, 4)
  34. HStack {
  35. VStack {
  36. Text("Сообщение \(index)")
  37. .foregroundColor(.secondary)
  38. Spacer()
  39. }
  40. Spacer()
  41. Text("\(index)")
  42. .font(.callout)
  43. .foregroundColor(colorScheme == .light ? .white : .black)
  44. .padding(.horizontal, 6)
  45. .background(
  46. Capsule().foregroundColor(.secondary)
  47. )
  48. }
  49. .padding(.vertical, 6)
  50. }
  51. }
  52. .foregroundColor(.primary)
  53. .frame(height: 60)
  54. NavigationLink {
  55. ChatView(viewModel: ChatViewModel(with: viewModel.rooms[index].jidString))
  56. } label: {
  57. EmptyView()
  58. }
  59. .opacity(0.0)
  60. }
  61. }
  62. }
  63. .listStyle(.plain)
  64. }
  65. }
  66. .navigationBarTitleDisplayMode(.inline)
  67. .toolbar {
  68. ToolbarItem(placement: .navigationBarTrailing) {
  69. Button {
  70. viewModel.fetchChatList()
  71. } label: {
  72. Image(systemName: "arrow.counterclockwise")
  73. }
  74. }
  75. }
  76. }
  77. }
  78. struct RoomsListView_Previews: PreviewProvider {
  79. static var viewModel: RoomsListViewModel = RoomsListViewModel()
  80. static var previews: some View {
  81. Group {
  82. NavigationView {
  83. RoomsListView(viewModel: viewModel)
  84. }
  85. NavigationView {
  86. RoomsListView(viewModel: viewModel)
  87. }
  88. .preferredColorScheme(.dark)
  89. }
  90. }
  91. }