SwiftUIPreviewHelpers.swift 7.6 KB

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