Skip to content

Commit 201dfd6

Browse files
authored
Merge pull request #6331 from woocommerce/issue/6324-hotfix-coupon-empty-state-view
Coupons: Hotfix empty state view for the coupon list
2 parents 54aa463 + 1c4ebe1 commit 201dfd6

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

WooCommerce/Classes/ViewRelated/Coupons/CouponListView.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ struct CouponListView: UIViewControllerRepresentable {
77

88
class Coordinator {
99
var parentObserver: NSKeyValueObservation?
10+
var rightBarButtonItemsObserver: NSKeyValueObservation?
1011
}
1112

1213
/// This is a UIKit solution for fixing Navigation Title and Bar Button Items ignored in NavigationView.
@@ -20,6 +21,12 @@ struct CouponListView: UIViewControllerRepresentable {
2021
vc.parent?.navigationItem.title = vc.title
2122
vc.parent?.navigationItem.rightBarButtonItems = vc.navigationItem.rightBarButtonItems
2223
})
24+
25+
// This fixes the issue when `rightBarButtonItem` is updated in `CouponListViewController`,
26+
// the hosting controller should be updated to reflect the change.
27+
context.coordinator.rightBarButtonItemsObserver = viewController.observe(\.navigationItem.rightBarButtonItems, changeHandler: { vc, _ in
28+
vc.parent?.navigationItem.rightBarButtonItems = vc.navigationItem.rightBarButtonItems
29+
})
2330
return viewController
2431
}
2532

WooCommerce/Classes/ViewRelated/Coupons/CouponListViewController.swift

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ final class CouponListViewController: UIViewController {
8585
}
8686
.store(in: &subscriptions)
8787

88+
viewModel.$couponViewModels
89+
.map { viewModels -> Bool in
90+
viewModels.isNotEmpty
91+
}
92+
.removeDuplicates()
93+
.sink { [weak self] hasData in
94+
self?.configureNavigationBarItems(hasCoupons: hasData)
95+
}
96+
.store(in: &subscriptions)
97+
8898
// Call this after the state subscription for extra safety
8999
viewModel.viewDidLoad()
90100
}
@@ -148,7 +158,10 @@ extension CouponListViewController: UITableViewDelegate {
148158
private extension CouponListViewController {
149159
func configureNavigation() {
150160
title = Localization.title
151-
navigationItem.rightBarButtonItem = searchBarButtonItem
161+
}
162+
163+
func configureNavigationBarItems(hasCoupons: Bool) {
164+
navigationItem.rightBarButtonItems = hasCoupons ? [searchBarButtonItem] : []
152165
}
153166

154167
func configureTableView() {
@@ -208,11 +221,10 @@ extension CouponListViewController {
208221
///
209222
func displayNoResultsOverlay() {
210223
let emptyStateViewController = EmptyStateViewController(style: .list)
211-
let config = EmptyStateViewController.Config.withButton(
224+
let config = EmptyStateViewController.Config.simple(
212225
message: .init(string: Localization.emptyStateMessage),
213-
image: .emptyCouponsImage,
214-
details: Localization.emptyStateDetails,
215-
buttonTitle: Localization.addCouponButton) { _ in }
226+
image: .emptyCouponsImage
227+
)
216228

217229
displayEmptyStateViewController(emptyStateViewController)
218230
emptyStateViewController.configure(config)
@@ -284,15 +296,9 @@ private extension CouponListViewController {
284296
comment: "Coupon management coupon list screen title")
285297

286298
static let emptyStateMessage = NSLocalizedString(
287-
"Everyone loves a deal",
299+
"No coupons found",
288300
comment: "The title on the placeholder overlay when there are no coupons on the coupon list screen.")
289301

290-
static let emptyStateDetails = NSLocalizedString(
291-
"Boost your business by sending customers special offers and discounts.",
292-
comment: "The description on the placeholder overlay when there are no coupons on the coupon list screen.")
293-
294-
static let addCouponButton = NSLocalizedString("Add Coupon", comment: "Title for the action button to add coupon on the coupon list screen.")
295-
296302
static let accessibilityLabelSearchCoupons = NSLocalizedString("Search coupons", comment: "Accessibility label for the Search Coupons button")
297303
static let accessibilityHintSearchCoupons = NSLocalizedString(
298304
"Retrieves a list of coupons that contain a given keyword.",

WooCommerce/Classes/ViewRelated/Coupons/CouponListViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class CouponListViewModel {
2323

2424
/// couponViewModels: ViewModels for the cells representing Coupons
2525
///
26-
var couponViewModels: [CouponListCellViewModel] = []
26+
@Published private(set) var couponViewModels: [CouponListCellViewModel] = []
2727

2828
/// siteID: siteID of the currently active site, used for fetching and storing coupons
2929
///

0 commit comments

Comments
 (0)