|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +/// A view to be displayed when a coupon is created successfully. |
| 4 | +/// |
| 5 | +struct CouponCreationSuccess: View { |
| 6 | + let couponCode: String |
| 7 | + let shareMessage: String |
| 8 | + let onDismiss: () -> Void |
| 9 | + |
| 10 | + @State private var showingShareSheet: Bool = false |
| 11 | + @State private var imageScale = Constants.initialImageScale |
| 12 | + @State private var imageBottomSpace = Constants.initialImageBottomSpacing |
| 13 | + @State private var textOpacity = Constants.initialTextOpacity |
| 14 | + @State private var buttonsVerticalOffset = Constants.initialButtonsVerticalOffset |
| 15 | + |
| 16 | + var body: some View { |
| 17 | + VStack(alignment: .center, spacing: 0) { |
| 18 | + Spacer() |
| 19 | + |
| 20 | + VStack(alignment: .leading, spacing: 0) { |
| 21 | + Image(uiImage: UIImage.checkSuccessImage) |
| 22 | + .padding(.bottom, imageBottomSpace) |
| 23 | + .scaleEffect(imageScale) |
| 24 | + Text(Localization.successMessage) |
| 25 | + .font(.largeTitle) |
| 26 | + .bold() |
| 27 | + .opacity(textOpacity) |
| 28 | + Text(couponCode) |
| 29 | + .font(.largeTitle) |
| 30 | + .opacity(textOpacity) |
| 31 | + } |
| 32 | + .padding(Constants.contentPadding) |
| 33 | + |
| 34 | + Spacer() |
| 35 | + |
| 36 | + VStack(alignment: .center, spacing: Constants.contentPadding) { |
| 37 | + Button(Localization.shareCoupon) { |
| 38 | + showingShareSheet = true |
| 39 | + // TODO: add analytics |
| 40 | + } |
| 41 | + .buttonStyle(PrimaryButtonStyle()) |
| 42 | + .shareSheet(isPresented: $showingShareSheet) { |
| 43 | + ShareSheet(activityItems: [shareMessage]) |
| 44 | + } |
| 45 | + |
| 46 | + Button(Localization.close) { |
| 47 | + onDismiss() |
| 48 | + } |
| 49 | + .buttonStyle(SecondaryButtonStyle()) |
| 50 | + } |
| 51 | + .padding(Constants.contentPadding) |
| 52 | + .offset(x: 0, y: buttonsVerticalOffset) |
| 53 | + } |
| 54 | + .onAppear { |
| 55 | + animateEntry() |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private func animateEntry() { |
| 60 | + let buttonGroupAnimation = Animation.easeIn(duration: Constants.buttonGroupAnimationDuration) |
| 61 | + |
| 62 | + let imageAnimation = Animation |
| 63 | + .interpolatingSpring(stiffness: Constants.imageAnimationStiffness, |
| 64 | + damping: Constants.imageAnimationDamping, |
| 65 | + initialVelocity: Constants.imageAnimationInitialVelocity) |
| 66 | + .delay(Constants.imageAnimationDelay) |
| 67 | + |
| 68 | + let textAnimation = Animation |
| 69 | + .easeIn(duration: Constants.textAnimationDuration) |
| 70 | + .delay(Constants.textAnimationDelay) |
| 71 | + |
| 72 | + withAnimation(buttonGroupAnimation) { |
| 73 | + buttonsVerticalOffset = Constants.finalButtonsVerticalOffset |
| 74 | + } |
| 75 | + |
| 76 | + withAnimation(imageAnimation) { |
| 77 | + imageScale = Constants.finalImageScale |
| 78 | + } |
| 79 | + |
| 80 | + withAnimation(textAnimation) { |
| 81 | + imageBottomSpace = Constants.finalImageBottomSpacing |
| 82 | + textOpacity = Constants.finalTextOpacity |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +private extension CouponCreationSuccess { |
| 88 | + enum Constants { |
| 89 | + static let contentPadding: CGFloat = 16 |
| 90 | + |
| 91 | + static let initialImageBottomSpacing: CGFloat = 80 |
| 92 | + static let finalImageBottomSpacing: CGFloat = 40 |
| 93 | + static let initialImageScale: CGFloat = 0 |
| 94 | + static let finalImageScale: CGFloat = 1 |
| 95 | + |
| 96 | + static let initialTextOpacity: CGFloat = 0 |
| 97 | + static let finalTextOpacity: CGFloat = 1 |
| 98 | + |
| 99 | + static let initialButtonsVerticalOffset: CGFloat = 300 |
| 100 | + static let finalButtonsVerticalOffset: CGFloat = 0 |
| 101 | + |
| 102 | + static let buttonGroupAnimationDuration: CGFloat = 0.3 |
| 103 | + static let imageAnimationStiffness: CGFloat = 150 |
| 104 | + static let imageAnimationDamping: CGFloat = 15 |
| 105 | + static let imageAnimationInitialVelocity: CGFloat = 3 |
| 106 | + static let imageAnimationDelay: CGFloat = 0.7 |
| 107 | + static let textAnimationDuration: CGFloat = 0.3 |
| 108 | + static let textAnimationDelay: CGFloat = 0.5 |
| 109 | + } |
| 110 | + |
| 111 | + enum Localization { |
| 112 | + static let successMessage = NSLocalizedString("Your new coupon was created!", comment: "Message displayed when a coupon was successfully created") |
| 113 | + static let shareCoupon = NSLocalizedString("Share Coupon", comment: "Button to share coupon from the Coupon Creation Success screen.") |
| 114 | + static let close = NSLocalizedString("Close", comment: "Button to dismiss the Coupon Creation Success screen") |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +struct CouponCreationSuccess_Previews: PreviewProvider { |
| 119 | + static var previews: some View { |
| 120 | + CouponCreationSuccess(couponCode: "34sdfg", shareMessage: "Use this coupon to get 10% off all products") {} |
| 121 | + } |
| 122 | +} |
0 commit comments