|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +/// Hosting controller wrapper for `JetpackBenefitsBanner` |
| 4 | +/// |
| 5 | +final class JetpackBenefitsBannerHostingController: UIHostingController<JetpackBenefitsBanner> { |
| 6 | + init() { |
| 7 | + super.init(rootView: JetpackBenefitsBanner()) |
| 8 | + } |
| 9 | + |
| 10 | + required dynamic init?(coder aDecoder: NSCoder) { |
| 11 | + fatalError("init(coder:) has not been implemented") |
| 12 | + } |
| 13 | + |
| 14 | + /// Actions are set in a separate function because most of the time, they will require to access `self` to be able to present new view controllers. |
| 15 | + /// |
| 16 | + func setActions(tapAction: @escaping () -> Void, dismissAction: @escaping () -> Void) { |
| 17 | + rootView.tapAction = tapAction |
| 18 | + rootView.dismissAction = dismissAction |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/// A banner about Jetpack benefits that can be tapped to show more details or dismiss. |
| 23 | +struct JetpackBenefitsBanner: View { |
| 24 | + /// Closure invoked when the banner is tapped |
| 25 | + /// |
| 26 | + var tapAction: () -> Void = {} |
| 27 | + |
| 28 | + /// Closure invoked when the dismiss button is tapped |
| 29 | + /// |
| 30 | + var dismissAction: () -> Void = {} |
| 31 | + |
| 32 | + // Tracks the scale of the view due to accessibility changes |
| 33 | + @ScaledMetric private var scale: CGFloat = 1.0 |
| 34 | + |
| 35 | + var body: some View { |
| 36 | + Group { |
| 37 | + HStack(spacing: Layout.horizontalSpacing) { |
| 38 | + Image(uiImage: .jetpackLogoImage) |
| 39 | + .resizable() |
| 40 | + .renderingMode(.template) |
| 41 | + .foregroundColor(Color(.jetpackGreen)) |
| 42 | + .frame(width: Layout.iconDimension * scale, height: Layout.iconDimension * scale) |
| 43 | + VStack(alignment: .leading, spacing: Layout.verticalTextSpacing) { |
| 44 | + Text(Localization.title) |
| 45 | + .foregroundColor(.white) |
| 46 | + .bodyStyle() |
| 47 | + Text(Localization.subtitle) |
| 48 | + .foregroundColor(Color(.gray(.shade30))) |
| 49 | + .bodyStyle() |
| 50 | + } |
| 51 | + Spacer() |
| 52 | + Button(action: dismissAction) { |
| 53 | + Image(uiImage: .closeButton) |
| 54 | + .foregroundColor(Color(.gray(.shade30))) |
| 55 | + } |
| 56 | + } |
| 57 | + .padding(insets: Layout.padding) |
| 58 | + } |
| 59 | + .background(Color(.jetpackBenefitsBackground)) |
| 60 | + .fixedSize(horizontal: false, vertical: true) |
| 61 | + .contentShape(Rectangle()) |
| 62 | + .onTapGesture { |
| 63 | + self.tapAction() |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +private extension JetpackBenefitsBanner { |
| 69 | + enum Localization { |
| 70 | + static let title = NSLocalizedString("Get the full experience with Jetpack", comment: "Title of the Jetpack benefits banner.") |
| 71 | + static let subtitle = NSLocalizedString("See the benefits", comment: "Subtitle of the Jetpack benefits banner.") |
| 72 | + } |
| 73 | + |
| 74 | + enum Layout { |
| 75 | + static let padding = EdgeInsets(top: 16, leading: 16, bottom: 16, trailing: 16) |
| 76 | + static let iconDimension = CGFloat(24) |
| 77 | + static let horizontalSpacing = CGFloat(10) |
| 78 | + static let verticalTextSpacing = CGFloat(5) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +struct JetpackBenefitsBanner_Previews: PreviewProvider { |
| 83 | + static var previews: some View { |
| 84 | + JetpackBenefitsBanner() |
| 85 | + .preferredColorScheme(.dark) |
| 86 | + .environment(\.sizeCategory, .extraSmall) |
| 87 | + .previewLayout(.sizeThatFits) |
| 88 | + JetpackBenefitsBanner() |
| 89 | + .preferredColorScheme(.light) |
| 90 | + .environment(\.sizeCategory, .medium) |
| 91 | + .previewLayout(.sizeThatFits) |
| 92 | + JetpackBenefitsBanner() |
| 93 | + .preferredColorScheme(.dark) |
| 94 | + .environment(\.sizeCategory, .extraExtraLarge) |
| 95 | + .previewLayout(.sizeThatFits) |
| 96 | + JetpackBenefitsBanner() |
| 97 | + .preferredColorScheme(.light) |
| 98 | + .environment(\.sizeCategory, .extraExtraExtraLarge) |
| 99 | + .previewLayout(.sizeThatFits) |
| 100 | + } |
| 101 | +} |
0 commit comments