RoomsListView.swift 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. .navigationTitle("Чаты")
  67. .navigationBarTitleDisplayMode(.inline)
  68. .toolbar {
  69. ToolbarItem(placement: .navigationBarTrailing) {
  70. Button {
  71. viewModel.fetchChatList()
  72. } label: {
  73. Image(systemName: "arrow.counterclockwise")
  74. }
  75. }
  76. }
  77. }
  78. }
  79. struct RoomsListView_Previews: PreviewProvider {
  80. static var viewModel: RoomsListViewModel = RoomsListViewModel()
  81. static var previews: some View {
  82. Group {
  83. NavigationView {
  84. RoomsListView(viewModel: viewModel)
  85. }
  86. NavigationView {
  87. RoomsListView(viewModel: viewModel)
  88. }
  89. .preferredColorScheme(.dark)
  90. }
  91. }
  92. }