|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +/// Hosting controller that wraps the `StoreNameForm`. |
| 4 | +final class StoreNameFormHostingController: UIHostingController<StoreNameForm> { |
| 5 | + private let onContinue: (String) -> Void |
| 6 | + private let onClose: () -> Void |
| 7 | + |
| 8 | + init(onContinue: @escaping (String) -> Void, |
| 9 | + onClose: @escaping () -> Void) { |
| 10 | + self.onContinue = onContinue |
| 11 | + self.onClose = onClose |
| 12 | + super.init(rootView: StoreNameForm()) |
| 13 | + |
| 14 | + rootView.onContinue = { [weak self] storeName in |
| 15 | + self?.onContinue(storeName) |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + @available(*, unavailable) |
| 20 | + required dynamic init?(coder aDecoder: NSCoder) { |
| 21 | + fatalError("init(coder:) has not been implemented") |
| 22 | + } |
| 23 | + |
| 24 | + override func viewDidLoad() { |
| 25 | + super.viewDidLoad() |
| 26 | + |
| 27 | + configureNavigationBarAppearance() |
| 28 | + } |
| 29 | + |
| 30 | + /// Shows a transparent navigation bar without a bottom border and with a close button to dismiss. |
| 31 | + func configureNavigationBarAppearance() { |
| 32 | + addCloseNavigationBarButton(title: Localization.cancelButtonTitle, |
| 33 | + target: self, |
| 34 | + action: #selector(closeButtonTapped)) |
| 35 | + let appearance = UINavigationBarAppearance() |
| 36 | + appearance.configureWithTransparentBackground() |
| 37 | + appearance.backgroundColor = .systemBackground |
| 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 | +private extension StoreNameFormHostingController { |
| 50 | + enum Localization { |
| 51 | + static let cancelButtonTitle = NSLocalizedString("Cancel", comment: "Navigation bar button on the store name form to leave the store creation flow.") |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// Allows the user to enter a store name during the store creation flow. |
| 56 | +struct StoreNameForm: View { |
| 57 | + /// Set in the hosting controller. |
| 58 | + var onContinue: (String) -> Void = { _ in } |
| 59 | + |
| 60 | + @State private var name: String = "" |
| 61 | + |
| 62 | + var body: some View { |
| 63 | + VStack(alignment: .leading, spacing: 0) { |
| 64 | + ScrollView { |
| 65 | + VStack(alignment: .leading, spacing: 40) { |
| 66 | + VStack(alignment: .leading, spacing: 16) { |
| 67 | + // Top header label. |
| 68 | + Text(Localization.topHeader) |
| 69 | + .foregroundColor(Color(.secondaryLabel)) |
| 70 | + .footnoteStyle() |
| 71 | + |
| 72 | + // Title label. |
| 73 | + Text(Localization.title) |
| 74 | + .fontWeight(.bold) |
| 75 | + .titleStyle() |
| 76 | + |
| 77 | + // Subtitle label. |
| 78 | + Text(Localization.subtitle) |
| 79 | + .foregroundColor(Color(.secondaryLabel)) |
| 80 | + .bodyStyle() |
| 81 | + } |
| 82 | + |
| 83 | + VStack(alignment: .leading, spacing: 16) { |
| 84 | + // Text field prompt label. |
| 85 | + Text(Localization.textFieldPrompt) |
| 86 | + .foregroundColor(Color(.label)) |
| 87 | + .bodyStyle() |
| 88 | + |
| 89 | + // Store name text field. |
| 90 | + TextField(Localization.textFieldPlaceholder, text: $name) |
| 91 | + .font(.body) |
| 92 | + .textFieldStyle(RoundedBorderTextFieldStyle(focused: false)) |
| 93 | + .focused() |
| 94 | + } |
| 95 | + } |
| 96 | + .padding(Layout.contentPadding) |
| 97 | + } |
| 98 | + |
| 99 | + // Continue button. |
| 100 | + Button(Localization.continueButtonTitle) { |
| 101 | + onContinue(name) |
| 102 | + } |
| 103 | + .padding(Layout.defaultButtonPadding) |
| 104 | + .buttonStyle(PrimaryButtonStyle()) |
| 105 | + .disabled(name.isEmpty) |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +private extension StoreNameForm { |
| 111 | + enum Layout { |
| 112 | + static let spacingBetweenSubtitleAndStoreInfo: CGFloat = 40 |
| 113 | + static let spacingBetweenStoreNameAndDomain: CGFloat = 4 |
| 114 | + static let defaultHorizontalPadding: CGFloat = 16 |
| 115 | + static let dividerHeight: CGFloat = 1 |
| 116 | + static let contentPadding: EdgeInsets = .init(top: 38, leading: 16, bottom: 16, trailing: 16) |
| 117 | + static let defaultButtonPadding: EdgeInsets = .init(top: 16, leading: 16, bottom: 16, trailing: 16) |
| 118 | + static let storeInfoPadding: EdgeInsets = .init(top: 16, leading: 16, bottom: 16, trailing: 16) |
| 119 | + static let storeInfoCornerRadius: CGFloat = 8 |
| 120 | + } |
| 121 | + |
| 122 | + enum Localization { |
| 123 | + static let topHeader = NSLocalizedString( |
| 124 | + "ABOUT YOUR STORE", |
| 125 | + comment: "Header label on the top of the store name form in the store creation flow." |
| 126 | + ) |
| 127 | + static let title = NSLocalizedString( |
| 128 | + "What’s your store name?", |
| 129 | + comment: "Title label on the store name form in the store creation flow." |
| 130 | + ) |
| 131 | + static let subtitle = NSLocalizedString( |
| 132 | + "Don’t worry you can always change it later.", |
| 133 | + comment: "Subtitle label on the store name form in the store creation flow." |
| 134 | + ) |
| 135 | + static let textFieldPrompt = NSLocalizedString( |
| 136 | + "Store name", |
| 137 | + comment: "Text field prompt on the store name form in the store creation flow." |
| 138 | + ) |
| 139 | + static let textFieldPlaceholder = NSLocalizedString( |
| 140 | + "Type a name for your store", |
| 141 | + comment: "Text field placeholder on the store name form in the store creation flow." |
| 142 | + ) |
| 143 | + static let continueButtonTitle = NSLocalizedString( |
| 144 | + "Continue", |
| 145 | + comment: "Title of the button on the store creation store name form to continue." |
| 146 | + ) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +struct StoreNameForm_Previews: PreviewProvider { |
| 151 | + static var previews: some View { |
| 152 | + StoreNameForm() |
| 153 | + } |
| 154 | +} |
0 commit comments