MessageView.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import MarkdownUI
  15. import SwiftUI
  16. struct RoundedCorner: Shape {
  17. var radius: CGFloat = .infinity
  18. var corners: UIRectCorner = .allCorners
  19. func path(in rect: CGRect) -> Path {
  20. let path = UIBezierPath(
  21. roundedRect: rect,
  22. byRoundingCorners: corners,
  23. cornerRadii: CGSize(width: radius, height: radius)
  24. )
  25. return Path(path.cgPath)
  26. }
  27. }
  28. extension View {
  29. func roundedCorner(_ radius: CGFloat, corners: UIRectCorner) -> some View {
  30. clipShape(RoundedCorner(radius: radius, corners: corners))
  31. }
  32. }
  33. struct MessageContentView: View {
  34. var message: ChatMessage
  35. var body: some View {
  36. if message.pending {
  37. BouncingDots()
  38. } else {
  39. Markdown(message.message)
  40. .markdownTextStyle {
  41. FontFamilyVariant(.normal)
  42. FontSize(.em(0.85))
  43. ForegroundColor(message.participant == .system ? Color(UIColor.label) : .white)
  44. }
  45. .markdownBlockStyle(\.codeBlock) { configuration in
  46. configuration.label
  47. .relativeLineSpacing(.em(0.25))
  48. .markdownTextStyle {
  49. FontFamilyVariant(.monospaced)
  50. FontSize(.em(0.85))
  51. ForegroundColor(Color(.label))
  52. }
  53. .padding()
  54. .background(Color(.secondarySystemBackground))
  55. .clipShape(RoundedRectangle(cornerRadius: 8))
  56. .markdownMargin(top: .zero, bottom: .em(0.8))
  57. }
  58. }
  59. }
  60. }
  61. struct MessageView: View {
  62. var message: ChatMessage
  63. var body: some View {
  64. HStack {
  65. if message.participant == .user {
  66. Spacer()
  67. }
  68. MessageContentView(message: message)
  69. .padding(10)
  70. .background(message.participant == .system
  71. ? Color(UIColor.systemFill)
  72. : Color(UIColor.systemBlue))
  73. .roundedCorner(10,
  74. corners: [
  75. .topLeft,
  76. .topRight,
  77. message.participant == .system ? .bottomRight : .bottomLeft,
  78. ])
  79. if message.participant == .system {
  80. Spacer()
  81. }
  82. }
  83. .listRowSeparator(.hidden)
  84. }
  85. }
  86. struct MessageView_Previews: PreviewProvider {
  87. static var previews: some View {
  88. NavigationView {
  89. List {
  90. MessageView(message: ChatMessage.samples[0])
  91. MessageView(message: ChatMessage.samples[1])
  92. MessageView(message: ChatMessage.samples[2])
  93. MessageView(message: ChatMessage(message: "Hello!", participant: .system, pending: true))
  94. }
  95. .listStyle(.plain)
  96. .navigationTitle("Chat sample")
  97. }
  98. }
  99. }