ModalInAppMessageView.swift 2.8 KB

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