|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +struct CouponUsageDetails: View { |
| 4 | + |
| 5 | + @ObservedObject private var viewModel: CouponUsageDetailsViewModel |
| 6 | + |
| 7 | + init(viewModel: CouponUsageDetailsViewModel) { |
| 8 | + self.viewModel = viewModel |
| 9 | + } |
| 10 | + |
| 11 | + var body: some View { |
| 12 | + GeometryReader { geometry in |
| 13 | + ScrollView { |
| 14 | + VStack(alignment: .leading, spacing: 0) { |
| 15 | + ListHeaderView(text: Localization.usageRestriction.uppercased(), alignment: .left) |
| 16 | + .padding(.horizontal, insets: geometry.safeAreaInsets) |
| 17 | + VStack(alignment: .leading, spacing: 0) { |
| 18 | + Divider() |
| 19 | + TitleAndTextFieldRow(title: String.localizedStringWithFormat(Localization.minimumSpend, viewModel.currencySymbol), |
| 20 | + placeholder: Localization.none, |
| 21 | + text: $viewModel.minimumSpend, |
| 22 | + editable: false, |
| 23 | + keyboardType: .decimalPad) |
| 24 | + .padding(.horizontal, insets: geometry.safeAreaInsets) |
| 25 | + Divider() |
| 26 | + .padding(.leading, Constants.margin) |
| 27 | + .padding(.leading, insets: geometry.safeAreaInsets) |
| 28 | + TitleAndTextFieldRow(title: String.localizedStringWithFormat(Localization.maximumSpend, viewModel.currencySymbol), |
| 29 | + placeholder: Localization.none, |
| 30 | + text: $viewModel.maximumSpend, |
| 31 | + editable: false, |
| 32 | + keyboardType: .decimalPad) |
| 33 | + .padding(.horizontal, insets: geometry.safeAreaInsets) |
| 34 | + Divider() |
| 35 | + .padding(.leading, Constants.margin) |
| 36 | + .padding(.leading, insets: geometry.safeAreaInsets) |
| 37 | + TitleAndTextFieldRow(title: Localization.usageLimitPerCoupon, |
| 38 | + placeholder: Localization.unlimited, |
| 39 | + text: $viewModel.usageLimitPerCoupon, |
| 40 | + editable: false, |
| 41 | + keyboardType: .asciiCapableNumberPad) |
| 42 | + .padding(.horizontal, insets: geometry.safeAreaInsets) |
| 43 | + Divider() |
| 44 | + .padding(.leading, Constants.margin) |
| 45 | + .padding(.leading, insets: geometry.safeAreaInsets) |
| 46 | + TitleAndTextFieldRow(title: Localization.usageLimitPerUser, |
| 47 | + placeholder: Localization.unlimited, |
| 48 | + text: $viewModel.usageLimitPerUser, |
| 49 | + editable: false, |
| 50 | + keyboardType: .asciiCapableNumberPad) |
| 51 | + .padding(.horizontal, insets: geometry.safeAreaInsets) |
| 52 | + Divider() |
| 53 | + } |
| 54 | + .background(Color(.listForeground)) |
| 55 | + .padding(.bottom, Constants.margin) |
| 56 | + |
| 57 | + VStack(alignment: .leading, spacing: 0) { |
| 58 | + Divider() |
| 59 | + TitleAndTextFieldRow(title: Localization.limitUsageToXItems, |
| 60 | + placeholder: Localization.allQualifyingInCart, |
| 61 | + text: $viewModel.limitUsageToXItems, |
| 62 | + editable: false, |
| 63 | + keyboardType: .asciiCapableNumberPad) |
| 64 | + .padding(.horizontal, insets: geometry.safeAreaInsets) |
| 65 | + Divider() |
| 66 | + .padding(.leading, Constants.margin) |
| 67 | + .padding(.leading, insets: geometry.safeAreaInsets) |
| 68 | + TitleAndValueRow(title: Localization.allowedEmails, |
| 69 | + value: viewModel.allowedEmails.isNotEmpty ? |
| 70 | + .content(viewModel.allowedEmails) : |
| 71 | + .content(Localization.noRestrictions)) |
| 72 | + .padding(.horizontal, insets: geometry.safeAreaInsets) |
| 73 | + Divider() |
| 74 | + } |
| 75 | + .background(Color(.listForeground)) |
| 76 | + .padding(.top, Constants.margin) |
| 77 | + |
| 78 | + ListHeaderView(text: Localization.usageLimits.uppercased(), alignment: .left) |
| 79 | + .padding(.horizontal, insets: geometry.safeAreaInsets) |
| 80 | + VStack(alignment: .leading, spacing: 0) { |
| 81 | + Divider() |
| 82 | + TitleAndToggleRow(title: Localization.individualUseOnly, isOn: .constant(viewModel.individualUseOnly)) |
| 83 | + .padding(.horizontal, insets: geometry.safeAreaInsets) |
| 84 | + .padding(.horizontal, Constants.margin) |
| 85 | + .padding(.vertical, Constants.verticalSpacing) |
| 86 | + Divider() |
| 87 | + .padding(.leading, Constants.margin) |
| 88 | + .padding(.leading, insets: geometry.safeAreaInsets) |
| 89 | + TitleAndToggleRow(title: Localization.excludeSaleItems, isOn: .constant(viewModel.excludeSaleItems)) |
| 90 | + .padding(.horizontal, insets: geometry.safeAreaInsets) |
| 91 | + .padding(.horizontal, Constants.margin) |
| 92 | + .padding(.vertical, Constants.verticalSpacing) |
| 93 | + Divider() |
| 94 | + } |
| 95 | + .background(Color(.listForeground)) |
| 96 | + .padding(.bottom, Constants.margin) |
| 97 | + } |
| 98 | + } |
| 99 | + .background(Color(.listBackground)) |
| 100 | + .ignoresSafeArea(.container, edges: [.horizontal]) |
| 101 | + } |
| 102 | + .navigationTitle(Localization.usageDetails) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +private extension CouponUsageDetails { |
| 107 | + enum Constants { |
| 108 | + static let margin: CGFloat = 16 |
| 109 | + static let verticalSpacing: CGFloat = 8 |
| 110 | + } |
| 111 | + |
| 112 | + enum Localization { |
| 113 | + static let usageDetails = NSLocalizedString("Usage Details", comment: "Navigation title for usage details screen") |
| 114 | + static let usageRestriction = NSLocalizedString( |
| 115 | + "Usage Restrictions", |
| 116 | + comment: "Title for the usage restrictions section on coupon usage details screen" |
| 117 | + ) |
| 118 | + static let minimumSpend = NSLocalizedString( |
| 119 | + "Minimum Spend (%1$@)", |
| 120 | + comment: "Title for the minimum spend row on coupon usage details screen with currency symbol within the brackets. " + |
| 121 | + "Reads like: Minimum Spend ($)" |
| 122 | + ) |
| 123 | + static let maximumSpend = NSLocalizedString( |
| 124 | + "Maximum Spend (%1$@)", |
| 125 | + comment: "Title for the maximum spend row on coupon usage details screen with currency symbol within the brackets. " + |
| 126 | + "Reads like: Maximum Spend ($)" |
| 127 | + ) |
| 128 | + static let usageLimitPerCoupon = NSLocalizedString( |
| 129 | + "Usage Limit Per Coupon", |
| 130 | + comment: "Title for the usage limit per coupon row in coupon usage details screen." |
| 131 | + ) |
| 132 | + static let usageLimitPerUser = NSLocalizedString( |
| 133 | + "Usage Limit Per User", |
| 134 | + comment: "Title for the usage limit per user row in coupon usage details screen." |
| 135 | + ) |
| 136 | + static let limitUsageToXItems = NSLocalizedString( |
| 137 | + "Limit Usage to X Items", |
| 138 | + comment: "Title for the limit usage to X items row in coupon usage details screen." |
| 139 | + ) |
| 140 | + static let allowedEmails = NSLocalizedString( |
| 141 | + "Allowed Emails", |
| 142 | + comment: "Title for the allowed email row in coupon usage details screen." |
| 143 | + ) |
| 144 | + static let usageLimits = NSLocalizedString("Usage Limits", comment: "Title for the usage limits section on coupon usage details screen") |
| 145 | + static let individualUseOnly = NSLocalizedString( |
| 146 | + "Individual Use Only", |
| 147 | + comment: "Title for the individual use only row in coupon usage details screen." |
| 148 | + ) |
| 149 | + static let excludeSaleItems = NSLocalizedString( |
| 150 | + "Exclude Sale Items", |
| 151 | + comment: "Title for the exclude sale items row in coupon usage details screen." |
| 152 | + ) |
| 153 | + static let none = NSLocalizedString("None", comment: "Value for fields in Coupon Usage Details screen when no value is set") |
| 154 | + static let unlimited = NSLocalizedString("Unlimited", comment: "Value for fields in Coupon Usage Details screen when no limit is set") |
| 155 | + static let allQualifyingInCart = NSLocalizedString( |
| 156 | + "All Qualifying in Cart", |
| 157 | + comment: "Value for the limit usage to X items row in Coupon Usage Details screen when no limit is set" |
| 158 | + ) |
| 159 | + static let noRestrictions = NSLocalizedString( |
| 160 | + "No Restrictions", |
| 161 | + comment: "Value for the allowed emails row in Coupon Usage Details screen when no restriction is set" |
| 162 | + ) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +#if DEBUG |
| 167 | +struct CouponUsageDetails_Previews: PreviewProvider { |
| 168 | + static var previews: some View { |
| 169 | + CouponUsageDetails(viewModel: .init(coupon: .sampleCoupon)) |
| 170 | + |
| 171 | + CouponUsageDetails(viewModel: .init(coupon: .sampleCoupon)) |
| 172 | + .previewLayout(.fixed(width: 715, height: 320)) |
| 173 | + } |
| 174 | +} |
| 175 | +#endif |
0 commit comments