ChatView.swift 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // ChatView.swift
  3. // Chat
  4. //
  5. // Created by Sergey Tarasov on 24.07.2022.
  6. //
  7. import SwiftUI
  8. extension CGFloat {
  9. static let sidePadding: CGFloat = 14
  10. }
  11. struct ChatView: View, KeyboardReadable {
  12. @State private var isKeyboardVisible = false
  13. @State var message: String = ""
  14. @State var messages: [String] = ["Display a short text message, called an alert, that draws attention to something new in your app.", "Play a notification sound.", "Set a badge number on the app’s icon to let the user know there are new items.", "Provide actions the user can take without opening the app.", "Show a media attachment.", "Be silent, allowing the app to perform a task in the background.", "No", "Group notifications into threads.", "Yes", "Edit or remove delivered notifications.", "Run code to change your notification before displaying it.", "Display a custom, interactive UI for your notification.", "And probably more.", "Yes", "Edit or remove delivered notifications.", "Run code to change your notification before displaying it.", "Display a custom, interactive UI for your notification.", "And probably more."]
  15. var body: some View {
  16. VStack(alignment: .center, spacing: 0) {
  17. ScrollView {
  18. ScrollViewReader { value in
  19. VStack {
  20. withAnimation {
  21. ForEach(messages.indices, id: \.self) { i in
  22. HStack {
  23. if messages[i] == messages[3] || messages[i] == messages[6] || messages[i] == messages[8] || i > 17{
  24. Spacer()
  25. MessageView(text: messages[i], time: "23:03", isSelf: true)
  26. } else {
  27. MessageView(text: messages[i], time: "23:03", isSelf: false)
  28. Spacer()
  29. }
  30. }
  31. }
  32. .onChange(of: messages) { _ in
  33. withAnimation {
  34. value.scrollTo(messages.count - 1, anchor: .bottomTrailing)
  35. }
  36. }
  37. .onReceive(keyboardPublisher) { newIsKeyboardVisible in
  38. isKeyboardVisible = newIsKeyboardVisible
  39. withAnimation {
  40. value.scrollTo(messages.count - 2, anchor: .topTrailing)
  41. }
  42. }
  43. .onAppear {
  44. value.scrollTo(messages.count - 1, anchor: .top)
  45. }
  46. }
  47. }
  48. .padding(.horizontal, .sidePadding)
  49. .padding(.vertical, 12)
  50. .background(Color.indigo.opacity(0.0))
  51. }
  52. }
  53. Divider()
  54. HStack {
  55. TextField("Сообщение", text: $message)
  56. .textFieldStyle(.automatic)
  57. .padding(.vertical, 6)
  58. .padding(.horizontal, 12)
  59. .background(.background)
  60. Button {
  61. messages.append(message)
  62. message = ""
  63. } label: {
  64. Image(systemName: "arrow.up.circle.fill")
  65. .font(.title)
  66. .foregroundColor(message.isEmpty ? .gray : .accentColor)
  67. }
  68. .disabled(message.isEmpty)
  69. }
  70. .padding(.horizontal, .sidePadding)
  71. .padding(.vertical, 6)
  72. .background()
  73. }
  74. .navigationBarTitleDisplayMode(.inline)
  75. }
  76. }
  77. struct ChatView_Previews: PreviewProvider {
  78. static var previews: some View {
  79. Group {
  80. NavigationView {
  81. ChatView()
  82. }
  83. NavigationView {
  84. ChatView()
  85. .preferredColorScheme(.dark)
  86. }
  87. }
  88. }
  89. }