Skip to content

Commit eb7503f

Browse files
authored
Merge pull request #2270 from woocommerce/issue/2037-filter-products-discard-changes
Show discard changes prompt when dismissing filter products screen with filter changes
2 parents a3c6f33 + aad6403 commit eb7503f

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

WooCommerce/Classes/ViewRelated/Filters/FilterListViewController.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import UIKit
44
///
55
protocol FilterListViewModel {
66
/// The type of the final value returned to the caller of `FilterListViewController`.
7-
associatedtype Criteria
7+
associatedtype Criteria: Equatable
88

99
// Filter Action UI configuration
1010

@@ -82,6 +82,7 @@ final class FilterListViewController<ViewModel: FilterListViewModel>: UIViewCont
8282
@IBOutlet private weak var filterActionContainerView: UIView!
8383

8484
private let viewModel: ViewModel
85+
private let originalCriteria: ViewModel.Criteria
8586
private let listSelectorCommand: FilterListSelectorCommand
8687

8788
private lazy var listSelector: ListSelectorViewController
@@ -108,6 +109,7 @@ final class FilterListViewController<ViewModel: FilterListViewModel>: UIViewCont
108109
/// - onFilterAction: Called when the user taps on the Filter CTA.
109110
init(viewModel: ViewModel, onFilterAction: @escaping (ViewModel.Criteria) -> Void) {
110111
self.viewModel = viewModel
112+
self.originalCriteria = viewModel.criteria
111113
self.onFilterAction = onFilterAction
112114
self.listSelectorCommand = FilterListSelectorCommand(data: viewModel.filterTypeViewModels)
113115
super.init(nibName: "FilterListViewController", bundle: nil)
@@ -146,6 +148,13 @@ final class FilterListViewController<ViewModel: FilterListViewModel>: UIViewCont
146148
}
147149

148150
@objc func dismissButtonTapped() {
151+
if hasFilterChanges() {
152+
UIAlertController.presentDiscardChangesActionSheet(viewController: self, onDiscard: { [weak self] in
153+
self?.dismiss(animated: true) {}
154+
})
155+
return
156+
}
157+
149158
dismiss(animated: true) {}
150159
}
151160

@@ -255,6 +264,14 @@ private extension FilterListViewController {
255264
}
256265
}
257266

267+
// MARK: Private helpers
268+
//
269+
private extension FilterListViewController {
270+
func hasFilterChanges() -> Bool {
271+
return viewModel.criteria != originalCriteria
272+
}
273+
}
274+
258275
private extension FilterListViewController {
259276
final class FilterListSelectorCommand: ListSelectorCommand {
260277
typealias Cell = SettingTitleAndValueTableViewCell

WooCommerce/Classes/ViewRelated/Filters/FilterListViewController.xib

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2222
<subviews>
2323
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" translatesAutoresizingMaskIntoConstraints="NO" id="0Hi-U6-ohm">
24-
<rect key="frame" x="0.0" y="44" width="414" height="818"/>
24+
<rect key="frame" x="0.0" y="0.0" width="414" height="862"/>
2525
<subviews>
2626
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="pgc-1V-BCe">
27-
<rect key="frame" x="0.0" y="0.0" width="414" height="768"/>
27+
<rect key="frame" x="0.0" y="0.0" width="414" height="812"/>
2828
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
2929
</view>
3030
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="0sS-tG-DzA">
31-
<rect key="frame" x="0.0" y="768" width="414" height="50"/>
31+
<rect key="frame" x="0.0" y="812" width="414" height="50"/>
3232
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
3333
<constraints>
3434
<constraint firstAttribute="height" constant="50" placeholder="YES" id="Tq2-i3-6qV"/>
@@ -39,7 +39,7 @@
3939
</subviews>
4040
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
4141
<constraints>
42-
<constraint firstItem="0Hi-U6-ohm" firstAttribute="top" secondItem="fnl-2z-Ty3" secondAttribute="top" id="Sny-jC-qtZ"/>
42+
<constraint firstItem="0Hi-U6-ohm" firstAttribute="top" secondItem="i5M-Pr-FkT" secondAttribute="top" id="Sny-jC-qtZ"/>
4343
<constraint firstItem="fnl-2z-Ty3" firstAttribute="trailing" secondItem="0Hi-U6-ohm" secondAttribute="trailing" id="hbI-xD-9g6"/>
4444
<constraint firstItem="fnl-2z-Ty3" firstAttribute="bottom" secondItem="0Hi-U6-ohm" secondAttribute="bottom" id="jyt-Qs-ekO"/>
4545
<constraint firstItem="0Hi-U6-ohm" firstAttribute="leading" secondItem="fnl-2z-Ty3" secondAttribute="leading" id="yfB-fN-j1x"/>

0 commit comments

Comments
 (0)