ChatView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // ChatView.swift
  3. // Chat
  4. //
  5. // Created by Sergey Tarasov on 24.07.2022.
  6. //
  7. import SwiftUI
  8. import XMPPFramework
  9. import XMPPFrameworkSwift
  10. extension CGFloat {
  11. static let sidePadding: CGFloat = 14
  12. }
  13. struct ChatView: View, KeyboardReadable {
  14. @ObservedObject var viewModel: ChatViewModel
  15. @State private var isKeyboardVisible = false
  16. var body: some View {
  17. VStack(alignment: .center, spacing: 0) {
  18. ScrollView {
  19. ScrollViewReader { value in
  20. VStack {
  21. withAnimation {
  22. ForEach(viewModel.messages, id: \.self) { message in
  23. HStack {
  24. if message.from?.bare == StreamManager.shared.stream.myJID?.bare {
  25. Spacer(minLength: 40)
  26. MessageView(text: message.body ?? "-", time: "", isSelf: true)
  27. } else {
  28. MessageView(text: message.body ?? "-", time: "", isSelf: false)
  29. Spacer(minLength: 40)
  30. }
  31. }
  32. }
  33. .onChange(of: viewModel.messages) { _ in
  34. withAnimation {
  35. value.scrollTo(viewModel.messages.count - 1, anchor: .bottomTrailing)
  36. }
  37. }
  38. .onReceive(keyboardPublisher) { newIsKeyboardVisible in
  39. isKeyboardVisible = newIsKeyboardVisible
  40. withAnimation {
  41. value.scrollTo(viewModel.messages.count - 2, anchor: .topTrailing)
  42. }
  43. }
  44. .onAppear {
  45. value.scrollTo(viewModel.messages.count - 1, anchor: .top)
  46. }
  47. }
  48. }
  49. .padding(.horizontal, .sidePadding)
  50. .padding(.vertical, 12)
  51. .background(Color.indigo.opacity(0.0))
  52. }
  53. }
  54. Divider()
  55. HStack {
  56. TextField("Сообщение", text: $viewModel.messageText)
  57. .textFieldStyle(.automatic)
  58. .padding(.vertical, 6)
  59. .padding(.horizontal, 12)
  60. .background(.background)
  61. Button {
  62. viewModel.messageText = ""
  63. } label: {
  64. Image(systemName: "arrow.up.circle.fill")
  65. .font(.title)
  66. .foregroundColor(viewModel.messageText.isEmpty ? .gray : .accentColor)
  67. }
  68. .disabled(viewModel.messageText.isEmpty)
  69. }
  70. .padding(.horizontal, .sidePadding)
  71. .padding(.vertical, 6)
  72. .background()
  73. }
  74. .navigationTitle(viewModel.manager.room.roomJID.user ?? "Room")
  75. .navigationBarTitleDisplayMode(.inline)
  76. .toolbar {
  77. ToolbarItem(placement: .navigationBarTrailing) {
  78. Button {
  79. viewModel.fetchMessages()
  80. } label: {
  81. Image(systemName: "arrow.counterclockwise")
  82. }
  83. }
  84. }
  85. .onAppear {
  86. viewModel.fetchMessages()
  87. }
  88. }
  89. }
  90. struct ChatView_Previews: PreviewProvider {
  91. static var viewModel: ChatViewModel = ChatViewModel(with: "")
  92. static var previews: some View {
  93. Group {
  94. NavigationView {
  95. ChatView(viewModel: viewModel)
  96. }
  97. NavigationView {
  98. ChatView(viewModel: viewModel)
  99. .preferredColorScheme(.dark)
  100. }
  101. }
  102. }
  103. }