ModalInAppMessageView.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2021 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 SwiftUI
  15. import FirebaseInAppMessaging
  16. import FirebaseInAppMessagingSwift
  17. struct ModalInAppMessageView: View {
  18. var modalMessage: InAppMessagingModalDisplay
  19. var delegate: InAppMessagingDisplayDelegate
  20. var body: some View {
  21. VStack {
  22. if let imageData = modalMessage.imageData?.imageRawData,
  23. let image = UIImage(data: imageData) {
  24. Image(uiImage: image)
  25. }
  26. Text(modalMessage.title).padding(4)
  27. if let bodyText = modalMessage.bodyText {
  28. Text(bodyText).padding(4)
  29. }
  30. actionButton(modalMessage: modalMessage, delegate: delegate).padding(4)
  31. dismissButton(modalMessage: modalMessage, delegate: delegate).padding(4)
  32. }
  33. .background(Color.white)
  34. .border(Color.black)
  35. .cornerRadius(4)
  36. }
  37. @ViewBuilder
  38. func actionButton(modalMessage: InAppMessagingModalDisplay,
  39. delegate: InAppMessagingDisplayDelegate) -> some View {
  40. if let button = modalMessage.actionButton {
  41. Button(action: {
  42. if let actionURL = modalMessage.actionURL {
  43. let action = InAppMessagingAction(actionText: button.buttonText,
  44. actionURL: actionURL)
  45. delegate.messageClicked?(modalMessage, with: action)
  46. } else {
  47. delegate.messageDismissed?(modalMessage, dismissType: .typeUserTapClose)
  48. }
  49. }) {
  50. Text(button.buttonText).bold()
  51. }
  52. }
  53. EmptyView()
  54. }
  55. // Need a dismiss button for the case where there's an action button with an action URL. Otherwise
  56. // user would be forced into a clickthrough.
  57. @ViewBuilder
  58. func dismissButton(modalMessage: InAppMessagingModalDisplay,
  59. delegate: InAppMessagingDisplayDelegate) -> some View {
  60. if let _ = modalMessage.actionButton, modalMessage.actionURL != nil {
  61. Button(action: {
  62. delegate.messageDismissed?(modalMessage, dismissType: .typeUserTapClose)
  63. }) {
  64. Text("Dismiss").bold()
  65. }
  66. }
  67. EmptyView()
  68. }
  69. struct ModalInAppMessageView_Previews: PreviewProvider {
  70. static var previews: some View {
  71. let modalMessage = InAppMessagingPreviewHelpers.modalMessage()
  72. return ModalInAppMessageView(modalMessage: modalMessage,
  73. delegate: InAppMessagingPreviewHelpers.Delegate())
  74. }
  75. }
  76. }