|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +/// Hosting controller that wraps the `StoreCreationPlanView`. |
| 4 | +final class StoreCreationPlanHostingController: UIHostingController<StoreCreationPlanView> { |
| 5 | + private let onPurchase: () -> Void |
| 6 | + private let onClose: () -> Void |
| 7 | + |
| 8 | + init(viewModel: StoreCreationPlanViewModel, |
| 9 | + onPurchase: @escaping () -> Void, |
| 10 | + onClose: @escaping () -> Void) { |
| 11 | + self.onPurchase = onPurchase |
| 12 | + self.onClose = onClose |
| 13 | + super.init(rootView: StoreCreationPlanView(viewModel: viewModel)) |
| 14 | + |
| 15 | + rootView.onPurchase = { [weak self] in |
| 16 | + self?.onPurchase() |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + @available(*, unavailable) |
| 21 | + required dynamic init?(coder aDecoder: NSCoder) { |
| 22 | + fatalError("init(coder:) has not been implemented") |
| 23 | + } |
| 24 | + |
| 25 | + override func viewDidLoad() { |
| 26 | + super.viewDidLoad() |
| 27 | + |
| 28 | + configureNavigationBarAppearance() |
| 29 | + } |
| 30 | + |
| 31 | + /// Shows a transparent navigation bar without a bottom border and with a close button to dismiss. |
| 32 | + func configureNavigationBarAppearance() { |
| 33 | + addCloseNavigationBarButton(target: self, action: #selector(closeButtonTapped)) |
| 34 | + |
| 35 | + let appearance = UINavigationBarAppearance() |
| 36 | + appearance.configureWithTransparentBackground() |
| 37 | + appearance.backgroundColor = .withColorStudio(.wooCommercePurple, shade: .shade90) |
| 38 | + |
| 39 | + navigationItem.standardAppearance = appearance |
| 40 | + navigationItem.scrollEdgeAppearance = appearance |
| 41 | + navigationItem.compactAppearance = appearance |
| 42 | + } |
| 43 | + |
| 44 | + @objc private func closeButtonTapped() { |
| 45 | + onClose() |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/// Displays the WPCOM eCommerce plan for purchase during the store creation flow. |
| 50 | +struct StoreCreationPlanView: View { |
| 51 | + /// Set in the hosting controller. |
| 52 | + var onPurchase: (() -> Void) = {} |
| 53 | + |
| 54 | + let viewModel: StoreCreationPlanViewModel |
| 55 | + |
| 56 | + var body: some View { |
| 57 | + VStack(alignment: .leading, spacing: 0) { |
| 58 | + ScrollView { |
| 59 | + VStack(alignment: .leading, spacing: 0) { |
| 60 | + HStack(alignment: .center) { |
| 61 | + VStack(alignment: .leading, spacing: 12) { |
| 62 | + // Plan name. |
| 63 | + Text(Localization.planTitle) |
| 64 | + .fontWeight(.semibold) |
| 65 | + .font(.title3) |
| 66 | + .foregroundColor(.white) |
| 67 | + |
| 68 | + // Price information. |
| 69 | + HStack(alignment: .bottom) { |
| 70 | + Text(viewModel.plan.displayPrice) |
| 71 | + .fontWeight(.bold) |
| 72 | + .foregroundColor(.white) |
| 73 | + .largeTitleStyle() |
| 74 | + Text(Localization.priceDuration) |
| 75 | + .foregroundColor(Color(.secondaryLabel)) |
| 76 | + .bodyStyle() |
| 77 | + } |
| 78 | + } |
| 79 | + .padding(.horizontal, insets: .init(top: 0, leading: 24, bottom: 0, trailing: 0)) |
| 80 | + |
| 81 | + Spacer() |
| 82 | + |
| 83 | + Image(uiImage: .storeCreationPlanImage) |
| 84 | + } |
| 85 | + |
| 86 | + Divider() |
| 87 | + .frame(height: Layout.dividerHeight) |
| 88 | + .foregroundColor(Color(Layout.dividerColor)) |
| 89 | + .padding(.horizontal, insets: Layout.defaultPadding) |
| 90 | + |
| 91 | + VStack(alignment: .leading, spacing: 0) { |
| 92 | + Spacer() |
| 93 | + .frame(height: 8) |
| 94 | + |
| 95 | + // Header label. |
| 96 | + Text(Localization.subtitle) |
| 97 | + .fontWeight(.bold) |
| 98 | + .foregroundColor(Color(.white)) |
| 99 | + .titleStyle() |
| 100 | + |
| 101 | + Spacer() |
| 102 | + .frame(height: 16) |
| 103 | + |
| 104 | + // Powered by WPCOM. |
| 105 | + HStack(spacing: 5) { |
| 106 | + Text(Localization.poweredByWPCOMPrompt) |
| 107 | + .foregroundColor(Color(.secondaryLabel)) |
| 108 | + .footnoteStyle() |
| 109 | + Image(uiImage: .wpcomLogoImage) |
| 110 | + } |
| 111 | + |
| 112 | + Spacer() |
| 113 | + .frame(height: 32) |
| 114 | + |
| 115 | + // Plan features. |
| 116 | + VStack(alignment: .leading, spacing: 16) { |
| 117 | + ForEach(viewModel.features, id: \.title) { feature in |
| 118 | + HStack(spacing: 12) { |
| 119 | + Image(uiImage: feature.icon) |
| 120 | + .renderingMode(.template) |
| 121 | + .foregroundColor(Color(.wooCommercePurple(.shade90))) |
| 122 | + Text(feature.title) |
| 123 | + .foregroundColor(Color(.label)) |
| 124 | + .bodyStyle() |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + .padding(Layout.defaultPadding) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + VStack(spacing: 0) { |
| 134 | + Divider() |
| 135 | + .frame(height: Layout.dividerHeight) |
| 136 | + .foregroundColor(Color(Layout.dividerColor)) |
| 137 | + |
| 138 | + // Continue button. |
| 139 | + Button(String(format: Localization.continueButtonTitleFormat, viewModel.plan.displayPrice)) { |
| 140 | + onPurchase() |
| 141 | + } |
| 142 | + .buttonStyle(PrimaryButtonStyle()) |
| 143 | + .padding(Layout.defaultButtonPadding) |
| 144 | + |
| 145 | + // Refund information. |
| 146 | + Text(Localization.refundableNote) |
| 147 | + .multilineTextAlignment(.center) |
| 148 | + .foregroundColor(Color(.secondaryLabel)) |
| 149 | + .bodyStyle() |
| 150 | + |
| 151 | + Spacer() |
| 152 | + .frame(height: 24) |
| 153 | + } |
| 154 | + } |
| 155 | + .background(Color(.withColorStudio(.wooCommercePurple, shade: .shade90))) |
| 156 | + // This screen is using the dark theme for both light and dark modes. |
| 157 | + .preferredColorScheme(.dark) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +private extension StoreCreationPlanView { |
| 162 | + enum Layout { |
| 163 | + static let dividerHeight: CGFloat = 1 |
| 164 | + static let defaultPadding: EdgeInsets = .init(top: 16, leading: 16, bottom: 16, trailing: 16) |
| 165 | + static let defaultButtonPadding: EdgeInsets = .init(top: 16, leading: 16, bottom: 16, trailing: 16) |
| 166 | + static let dividerColor: UIColor = .separator |
| 167 | + } |
| 168 | + |
| 169 | + enum Localization { |
| 170 | + static let planTitle = NSLocalizedString( |
| 171 | + "eCommerce", |
| 172 | + comment: "Title of the store creation plan on the plan screen.") |
| 173 | + static let priceDuration = NSLocalizedString( |
| 174 | + "/month", |
| 175 | + comment: "The text is preceded by the monthly price on the store creation plan screen.") |
| 176 | + static let subtitle = NSLocalizedString( |
| 177 | + "All the featues you need, already built in", |
| 178 | + comment: "Subtitle of the store creation plan screen.") |
| 179 | + static let poweredByWPCOMPrompt = NSLocalizedString( |
| 180 | + "Powered by", |
| 181 | + comment: "The text is followed by a WordPress.com logo on the store creation plan screen.") |
| 182 | + static let continueButtonTitleFormat = NSLocalizedString( |
| 183 | + "Create Store for %1$@/month", |
| 184 | + comment: "Title of the button on the store creation plan view to purchase the plan. " + |
| 185 | + "%1$@ is replaced by the monthly price." |
| 186 | + ) |
| 187 | + static let refundableNote = NSLocalizedString( |
| 188 | + "There’s no risk, you can cancel for a full refund within 30 days.", |
| 189 | + comment: "Refund policy under the purchase button on the store creation plan screen." |
| 190 | + ) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +#if DEBUG |
| 195 | + |
| 196 | +/// Only used for `StoreCreationPlanView` preview. |
| 197 | +private struct Plan: WPComPlanProduct { |
| 198 | + let displayName: String |
| 199 | + let description: String |
| 200 | + let id: String |
| 201 | + let displayPrice: String |
| 202 | +} |
| 203 | + |
| 204 | +struct StoreCreationPlanView_Previews: PreviewProvider { |
| 205 | + static var previews: some View { |
| 206 | + StoreCreationPlanView(viewModel: |
| 207 | + .init(plan: Plan(displayName: "Debug Monthly", |
| 208 | + description: "1 Month of Debug Woo", |
| 209 | + id: "debug.woocommerce.ecommerce.monthly", |
| 210 | + displayPrice: "$69.99"))) |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +#endif |
0 commit comments