FIAMSwiftUIApp.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 FirebaseCore
  16. import FirebaseInAppMessaging
  17. import FirebaseInAppMessagingSwift
  18. @main
  19. struct FIAMSwiftUIApp: App {
  20. init() {
  21. FirebaseApp.configure()
  22. }
  23. var body: some Scene {
  24. WindowGroup {
  25. ContentView()
  26. .modalInAppMessage { modalMessage, delegate in
  27. ModalInAppMessageView(modalMessage: modalMessage, delegate: delegate)
  28. }
  29. }
  30. }
  31. // This can be in another file, for cleaner organization.
  32. struct ModalInAppMessageView: View {
  33. var modalMessage: InAppMessagingModalDisplay
  34. var delegate: InAppMessagingDisplayDelegate
  35. var body: some View {
  36. VStack {
  37. if let imageData = modalMessage.imageData?.imageRawData,
  38. let image = UIImage(data: imageData) {
  39. Image(uiImage: image)
  40. }
  41. Text(modalMessage.title).padding(4)
  42. if let bodyText = modalMessage.bodyText {
  43. Text(bodyText).padding(4)
  44. }
  45. actionButton(modalMessage: modalMessage, delegate: delegate).padding(4)
  46. dismissButton(modalMessage: modalMessage, delegate: delegate).padding(4)
  47. }
  48. .background(Color.white)
  49. .border(Color.black)
  50. .cornerRadius(4)
  51. }
  52. @ViewBuilder
  53. func actionButton(modalMessage: InAppMessagingModalDisplay,
  54. delegate: InAppMessagingDisplayDelegate) -> some View {
  55. if let button = modalMessage.actionButton {
  56. Button(action: {
  57. if let actionURL = modalMessage.actionURL {
  58. let action = InAppMessagingAction(actionText: button.buttonText,
  59. actionURL: actionURL)
  60. delegate.messageClicked?(modalMessage, with: action)
  61. } else {
  62. delegate.messageDismissed?(modalMessage, dismissType: .typeUserTapClose)
  63. }
  64. }) {
  65. Text(button.buttonText)
  66. }
  67. }
  68. EmptyView()
  69. }
  70. // Need a dismiss button for the case where there's an action button with an action URL. Otherwise
  71. // user would be forced into a clickthrough.
  72. @ViewBuilder
  73. func dismissButton(modalMessage: InAppMessagingModalDisplay,
  74. delegate: InAppMessagingDisplayDelegate) -> some View {
  75. if let _ = modalMessage.actionButton, modalMessage.actionURL != nil {
  76. Button(action: {
  77. delegate.messageDismissed?(modalMessage, dismissType: .typeUserTapClose)
  78. }) {
  79. Text("Dismiss")
  80. }
  81. }
  82. EmptyView()
  83. }
  84. }
  85. }