ChatListView.swift 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // ChatListView.swift
  3. // Chat
  4. //
  5. // Created by Sergey Tarasov on 24.07.2022.
  6. //
  7. import SwiftUI
  8. import XMPPFramework
  9. import XMPPFrameworkSwift
  10. struct ChatListView: View {
  11. @Environment(\.colorScheme) var colorScheme
  12. @ObservedObject var viewModel: ChatListViewModel
  13. var body: some View {
  14. Group {
  15. if viewModel.users.isEmpty {
  16. ProgressView()
  17. } else {
  18. List {
  19. ForEach(viewModel.users.indices, id: \.self) { index in
  20. ZStack {
  21. HStack {
  22. Image(systemName: "person.crop.circle")
  23. .resizable()
  24. .scaledToFit()
  25. .foregroundColor(.secondary.opacity(0.5))
  26. .padding(.vertical, 8)
  27. VStack(alignment: .leading, spacing: 0) {
  28. HStack {
  29. Text(viewModel.nicknames[index])
  30. Spacer()
  31. Text("23:04")
  32. .font(.callout)
  33. .foregroundColor(.secondary)
  34. }
  35. .padding(.vertical, 4)
  36. HStack {
  37. VStack {
  38. Text("Сообщение \(index)")
  39. .foregroundColor(.secondary)
  40. Spacer()
  41. }
  42. Spacer()
  43. Text("\(index)")
  44. .font(.callout)
  45. .foregroundColor(colorScheme == .light ? .white : .black)
  46. .padding(.horizontal, 6)
  47. .background(
  48. Capsule().foregroundColor(.secondary)
  49. )
  50. }
  51. .padding(.vertical, 6)
  52. }
  53. }
  54. .foregroundColor(.primary)
  55. .frame(height: 60)
  56. NavigationLink {
  57. ChatView(viewModel: ChatViewModel(with: viewModel.users[index].jidString))
  58. } label: {
  59. EmptyView()
  60. }
  61. .opacity(0.0)
  62. }
  63. }
  64. }
  65. .listStyle(.plain)
  66. }
  67. }
  68. .navigationTitle("Контакты")
  69. .navigationBarTitleDisplayMode(.inline)
  70. .toolbar {
  71. ToolbarItem(placement: .navigationBarTrailing) {
  72. Button {
  73. viewModel.fetchChatList()
  74. } label: {
  75. Image(systemName: "arrow.counterclockwise")
  76. }
  77. }
  78. }
  79. }
  80. }
  81. struct ChatListView_Previews: PreviewProvider {
  82. static var viewModel: ChatListViewModel = ChatListViewModel()
  83. static var previews: some View {
  84. Group {
  85. NavigationView {
  86. ChatListView(viewModel: viewModel)
  87. }
  88. NavigationView {
  89. ChatListView(viewModel: viewModel)
  90. }
  91. .preferredColorScheme(.dark)
  92. }
  93. }
  94. }