-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathEnhancedCouponListViewController.swift
172 lines (150 loc) · 7.82 KB
/
EnhancedCouponListViewController.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import Foundation
import WordPressUI
import UIKit
import Yosemite
/// Shows a coupons list plus the entry to other accessory actions: search, creation.
///
final class EnhancedCouponListViewController: UIViewController {
private var couponListViewController: CouponListViewController?
private let siteID: Int64
private lazy var noticePresenter: DefaultNoticePresenter = {
let noticePresenter = DefaultNoticePresenter()
noticePresenter.presentingViewController = self
return noticePresenter
}()
init(siteID: Int64) {
self.siteID = siteID
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
configureNavigation()
configureCouponListViewController()
}
/// Create a `UIBarButtonItem` to be used as the search button on the top-left.
///
private lazy var searchBarButtonItem: UIBarButtonItem = {
let button = UIBarButtonItem(image: .searchBarButtonItemImage,
style: .plain,
target: self,
action: #selector(displaySearchCoupons))
button.accessibilityTraits = .button
button.accessibilityLabel = Localization.accessibilityLabelSearchCoupons
button.accessibilityHint = Localization.accessibilityHintSearchCoupons
button.accessibilityIdentifier = "coupon-search-button"
return button
}()
/// Create a `UIBarButtonItem` to be used as the create coupon button on the top-right.
///
private lazy var createCouponButtonItem: UIBarButtonItem = {
let button = UIBarButtonItem(image: .plusImage,
style: .plain,
target: self,
action: #selector(displayCouponTypeBottomSheet))
button.accessibilityTraits = .button
button.accessibilityLabel = Localization.accessibilityLabelCreateCoupons
button.accessibilityHint = Localization.accessibilityHintCreateCoupons
button.accessibilityIdentifier = "coupon-create-button"
return button
}()
}
private extension EnhancedCouponListViewController {
func configureCouponListViewController() {
let couponListViewController = CouponListViewController(siteID: siteID,
emptyStateActionTitle: Localization.createCouponAction,
onDataLoaded: configureNavigationBarItems,
emptyStateAction: displayCouponTypeBottomSheet,
onCouponSelected: showDetails)
couponListViewController.view.translatesAutoresizingMaskIntoConstraints = false
addChild(couponListViewController)
view.addSubview(couponListViewController.view)
view.pinSubviewToAllEdges(couponListViewController.view)
couponListViewController.didMove(toParent: self)
self.couponListViewController = couponListViewController
}
func configureNavigation() {
title = Localization.title
}
func configureNavigationBarItems(hasCoupons: Bool) {
if hasCoupons {
navigationItem.rightBarButtonItems = [createCouponButtonItem, searchBarButtonItem]
} else {
navigationItem.rightBarButtonItems = [createCouponButtonItem]
}
}
/// Shows `SearchViewController`.
///
@objc private func displaySearchCoupons() {
ServiceLocator.analytics.track(.couponsListSearchTapped)
let searchViewController = SearchViewController<TitleAndSubtitleAndStatusTableViewCell, CouponSearchUICommand>(
storeID: siteID,
command: CouponSearchUICommand(siteID: siteID),
cellType: TitleAndSubtitleAndStatusTableViewCell.self,
cellSeparator: .singleLine
)
let navigationController = WooNavigationController(rootViewController: searchViewController)
present(navigationController, animated: true, completion: nil)
}
/// Triggers the coupon creation flow
///
func startCouponCreation(discountType: Coupon.DiscountType) {
let viewModel = AddEditCouponViewModel(siteID: siteID,
discountType: discountType,
onSuccess: { [weak self] _ in
self?.couponListViewController?.refreshCouponList()
})
let addEditHostingController = AddEditCouponHostingController(viewModel: viewModel, onDisappear: { [weak self] _ in
guard let self = self else { return }
self.dismiss(animated: true)
})
present(addEditHostingController, animated: true)
}
@objc private func displayCouponTypeBottomSheet() {
ServiceLocator.analytics.track(.couponsListCreateTapped)
let viewProperties = BottomSheetListSelectorViewProperties(subtitle: Localization.createCouponAction)
let command = DiscountTypeBottomSheetListSelectorCommand(selected: nil) { [weak self] selectedType in
guard let self = self else { return }
self.presentedViewController?.dismiss(animated: true, completion: nil)
self.startCouponCreation(discountType: selectedType)
}
let bottomSheet = BottomSheetListSelectorViewController(viewProperties: viewProperties, command: command, onDismiss: nil)
let bottomSheetViewController = BottomSheetViewController(childViewController: bottomSheet)
bottomSheetViewController.show(from: self)
}
func showDetails(from coupon: Coupon) {
let detailsViewModel = CouponDetailsViewModel(coupon: coupon, onUpdate: { [weak self] in
guard let self = self else { return }
self.couponListViewController?.refreshCouponList()
}, onDeletion: { [weak self] in
guard let self = self else { return }
self.navigationController?.popViewController(animated: true)
let notice = Notice(title: Localization.couponDeleted, feedbackType: .success)
self.noticePresenter.enqueue(notice: notice)
})
let hostingController = CouponDetailsHostingController(viewModel: detailsViewModel)
navigationController?.pushViewController(hostingController, animated: true)
}
}
// MARK: - Localization
//
extension EnhancedCouponListViewController {
enum Localization {
static let title = NSLocalizedString(
"Coupons",
comment: "Coupon management coupon list screen title")
static let accessibilityLabelCreateCoupons = NSLocalizedString("Create coupons", comment: "Accessibility label for the Create Coupons button")
static let accessibilityHintCreateCoupons = NSLocalizedString("Start a Coupon creation by selecting a discount type in a bottom sheet",
comment: "VoiceOver accessibility hint, informing the user the button can be used to create coupons.")
static let accessibilityLabelSearchCoupons = NSLocalizedString("Search coupons", comment: "Accessibility label for the Search Coupons button")
static let accessibilityHintSearchCoupons = NSLocalizedString(
"Retrieves a list of coupons that contain a given keyword.",
comment: "VoiceOver accessibility hint, informing the user the button can be used to search coupons."
)
static let couponDeleted = NSLocalizedString("Coupon deleted", comment: "Notice message after deleting coupon from the Coupon Details screen")
static let createCouponAction = NSLocalizedString("Create Coupon",
comment: "Title of the create coupon button on the coupon list screen when it's empty")
}
}