SwiftUIPreviewHelpers.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 UIKit
  15. #if SWIFT_PACKAGE
  16. @_exported import FirebaseInAppMessagingInternal
  17. #endif // SWIFT_PACKAGE
  18. @available(iOS 13.0, tvOS 13.0, *)
  19. @available(iOSApplicationExtension, unavailable)
  20. @available(tvOSApplicationExtension, unavailable)
  21. public enum InAppMessagingPreviewHelpers {
  22. public static func cardMessage(campaignName: String = "Card message campaign",
  23. title: String = "Title for modal message",
  24. body: String? = "Body for modal message",
  25. textColor: UIColor = UIColor.label,
  26. backgroundColor: UIColor = UIColor.black,
  27. portraitImage: UIImage = UIImage(systemName: "rectangle")!,
  28. landscapeImage: UIImage? = UIImage(systemName: "square"),
  29. primaryButtonText: String = "Click me!",
  30. primaryButtonTextColor: UIColor = UIColor.systemBlue,
  31. primaryButtonBackgroundColor: UIColor = UIColor.systemGray,
  32. primaryActionURL: URL? = nil,
  33. secondaryButtonText: String? = "Dismiss",
  34. secondaryButtonTextColor: UIColor? = UIColor.secondaryLabel,
  35. secondaryButtonBackgroundColor: UIColor? = UIColor.systemYellow,
  36. secondaryActionURL: URL? = nil,
  37. appData: [String: String]? = nil) -> InAppMessagingCardDisplay {
  38. // This may crash the preview if an invalid portrait image is provided, card messages must have
  39. // a valid portrait image.
  40. let portraitImageData = InAppMessagingImageData(imageURL: "https://firebase.google.com/",
  41. imageData: portraitImage.pngData()!)
  42. var landscapeImageData: InAppMessagingImageData?
  43. if let landscapeData = landscapeImage?.pngData() {
  44. landscapeImageData = InAppMessagingImageData(
  45. imageURL: "http://fakeurl.com",
  46. imageData: landscapeData
  47. )
  48. }
  49. let primaryActionButton = InAppMessagingActionButton(buttonText: primaryButtonText,
  50. buttonTextColor: primaryButtonTextColor,
  51. backgroundColor: primaryButtonBackgroundColor)
  52. var secondaryActionButton: InAppMessagingActionButton?
  53. if secondaryButtonText != nil,
  54. secondaryButtonTextColor != nil,
  55. secondaryButtonBackgroundColor != nil {
  56. secondaryActionButton = InAppMessagingActionButton(buttonText: secondaryButtonText!,
  57. buttonTextColor: secondaryButtonTextColor!,
  58. backgroundColor: secondaryButtonBackgroundColor!)
  59. }
  60. return InAppMessagingCardDisplay(
  61. campaignName: campaignName,
  62. titleText: title,
  63. bodyText: body,
  64. textColor: textColor,
  65. portraitImageData: portraitImageData,
  66. landscapeImageData: landscapeImageData,
  67. backgroundColor: backgroundColor,
  68. primaryActionButton: primaryActionButton,
  69. secondaryActionButton: secondaryActionButton,
  70. primaryActionURL: primaryActionURL,
  71. secondaryActionURL: secondaryActionURL,
  72. appData: appData
  73. )
  74. }
  75. public static func modalMessage(campaignName: String = "Modal message campaign",
  76. title: String = "Title for modal message",
  77. body: String? = "Body for modal message",
  78. textColor: UIColor = UIColor.black,
  79. backgroundColor: UIColor = UIColor.white,
  80. image: UIImage? = UIImage(systemName: "rectangle"),
  81. buttonText: String? = "Click me!",
  82. buttonTextColor: UIColor? = UIColor.systemBlue,
  83. buttonBackgroundColor: UIColor? = UIColor
  84. .white,
  85. actionURL: URL? = nil,
  86. appData: [String: String]? = nil) -> InAppMessagingModalDisplay {
  87. var imageData: InAppMessagingImageData?
  88. if let data = image?.pngData() {
  89. imageData = InAppMessagingImageData(imageURL: "https://firebase.google.com/", imageData: data)
  90. }
  91. var actionButton: InAppMessagingActionButton?
  92. if let buttonText,
  93. let buttonTextColor = buttonTextColor,
  94. let buttonBackgroundColor = buttonBackgroundColor {
  95. actionButton = InAppMessagingActionButton(buttonText: buttonText,
  96. buttonTextColor: buttonTextColor,
  97. backgroundColor: buttonBackgroundColor)
  98. }
  99. return InAppMessagingModalDisplay(
  100. campaignName: campaignName,
  101. titleText: title,
  102. bodyText: body,
  103. textColor: textColor,
  104. backgroundColor: backgroundColor,
  105. imageData: imageData,
  106. actionButton: actionButton,
  107. actionURL: actionURL,
  108. appData: appData
  109. )
  110. }
  111. public static func bannerMessage(campaignName: String = "Banner message campaign",
  112. title: String = "Title for banner message",
  113. body: String? = "Body for banner message",
  114. textColor: UIColor = UIColor.black,
  115. backgroundColor: UIColor = UIColor.white,
  116. image: UIImage? = UIImage(systemName: "square"),
  117. actionURL: URL? = nil,
  118. appData: [String: String]? = nil)
  119. -> InAppMessagingBannerDisplay {
  120. var imageData: InAppMessagingImageData?
  121. if let data = image?.pngData() {
  122. imageData = InAppMessagingImageData(imageURL: "https://firebase.google.com/", imageData: data)
  123. }
  124. return InAppMessagingBannerDisplay(
  125. campaignName: campaignName,
  126. titleText: title,
  127. bodyText: body,
  128. textColor: textColor,
  129. backgroundColor: backgroundColor,
  130. imageData: imageData,
  131. actionURL: actionURL,
  132. appData: appData
  133. )
  134. }
  135. public static func imageOnlyMessage(campaignName: String = "Image-only message campaign",
  136. image: UIImage,
  137. actionURL: URL? = nil,
  138. appData: [String: String]? = nil)
  139. -> InAppMessagingImageOnlyDisplay {
  140. // This may crash the preview if an invalid image is provided, image-only messages must have a
  141. // valid portrait image.
  142. let imageData = InAppMessagingImageData(imageURL: "https://firebase.google.com/",
  143. imageData: image.pngData()!)
  144. return InAppMessagingImageOnlyDisplay(
  145. campaignName: campaignName,
  146. imageData: imageData,
  147. actionURL: actionURL,
  148. appData: appData
  149. )
  150. }
  151. public class Delegate: NSObject, InAppMessagingDisplayDelegate {}
  152. }