-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathPOSCouponCreationSheet.swift
111 lines (98 loc) · 3.85 KB
/
POSCouponCreationSheet.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import SwiftUI
import struct Yosemite.Coupon
import enum Yosemite.POSItem
import struct Yosemite.POSCoupon
extension View {
func posCouponCreationSheet(
isPresented: Binding<Bool>,
onSuccess: @escaping (POSItem) -> Void
) -> some View {
modifier(POSCouponCreationSheetModifier(isPresented: isPresented, onSuccess: onSuccess))
}
}
private struct POSCouponCreationSheetModifier: ViewModifier {
@Binding var isPresented: Bool
let onSuccess: (POSItem) -> Void
@State private var selectedType: POSCouponDiscountType?
@State private var showCouponSelectionSheet: Bool = false
func body(content: Content) -> some View {
content
.sheet(item: $selectedType) { (posDiscountType: POSCouponDiscountType) in
var addedCouponItem: POSItem?
let viewModel = AddEditCouponViewModel(discountType: posDiscountType.discountType, onSuccess: { coupon in
addedCouponItem = .coupon(.init(id: UUID(), code: coupon.code))
})
var view = AddEditCoupon(viewModel)
view.dismissHandler = {
selectedType = nil
}
view.onDisappear = {
if let couponItem = addedCouponItem {
selectedType = nil
onSuccess(couponItem)
addedCouponItem = nil
}
}
view.discountTypeHandler = { _ in
showCouponSelectionSheet = true
}
return view
.interactiveDismissDisabled()
.discountTypeSelectionSheet(isPresented: $showCouponSelectionSheet) { type in
showCouponSelectionSheet = false
viewModel.discountType = type.discountType
}
}
.discountTypeSelectionSheet(isPresented: $isPresented) { type in
selectedType = type
}
}
}
private extension View {
func discountTypeSelectionSheet(
isPresented: Binding<Bool>,
onSelection: @escaping (POSCouponDiscountType) -> Void
) -> some View {
sheet(isPresented: isPresented) {
let command = DiscountTypeBottomSheetListSelectorCommand(selected: nil) { type in
onSelection(.init(discountType: type))
}
NavigationView {
BottomSheetListSelector(
viewProperties: BottomSheetListSelectorViewProperties(),
command: command,
onDismiss: { _ in
isPresented.wrappedValue = false
}
)
.navigationBarTitleDisplayMode(.large)
.navigationTitle(Localization.selectCouponTypeTitle)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button(Localization.selectCouponCancelButtonTitle) {
isPresented.wrappedValue = false
}
}
}
}
.navigationViewStyle(.stack)
.interactiveDismissDisabled()
}
}
}
private struct POSCouponDiscountType: Identifiable, Equatable {
var id: String { discountType.rawValue }
let discountType: Coupon.DiscountType
}
private enum Localization {
static let selectCouponTypeTitle = NSLocalizedString(
"pos.couponCreationSheet.selectCoupon.title",
value: "Create coupon",
comment: "A title for the view that selects the type of coupon to create"
)
static let selectCouponCancelButtonTitle = NSLocalizedString(
"pos.couponCreationSheet.selectCoupon.cancel",
value: "Cancel",
comment: "A button that dismisses coupon creation sheet"
)
}