SwiftUIPreviewHelpers.swift 7.6 KB

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