Skip to content

Commit b2d3018

Browse files
committed
Insights Management: Handle save and dismiss
1 parent 3051894 commit b2d3018

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

WordPress/Classes/ViewRelated/Stats/Insights/Insights Management/AddInsightTableViewController.swift

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import Gridicons
55
// can't be represented in an Obj-C protocol.
66
protocol StatsInsightsManagementDelegate: AnyObject {
77
func userUpdatedActiveInsights(_ insights: [StatSection])
8-
func insightsManagementDismissed()
98
}
109

1110
class AddInsightTableViewController: UITableViewController {
@@ -22,9 +21,14 @@ class AddInsightTableViewController: UITableViewController {
2221
}
2322
}
2423

24+
private var hasChanges: Bool {
25+
return insightsShown != originalInsightsShown
26+
}
27+
2528
private var selectedStat: StatSection?
2629

27-
private lazy var saveButton = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(saveInsights))
30+
private lazy var saveButton = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(saveTapped))
31+
private var dismissedViaButton = false
2832

2933
private lazy var tableHandler: ImmuTableViewHandler = {
3034
return ImmuTableViewHandler(takeOver: self)
@@ -72,12 +76,18 @@ class AddInsightTableViewController: UITableViewController {
7276
override func viewWillDisappear(_ animated: Bool) {
7377
super.viewWillDisappear(animated)
7478

79+
// Catches user swiping down to dismiss
7580
if FeatureFlag.statsNewAppearance.enabled {
76-
// TODO: Check for any changes, prompt user
77-
} else {
78-
if selectedStat == nil {
79-
insightsDelegate?.addInsightDismissed?()
81+
if !dismissedViaButton {
82+
if hasChanges {
83+
promptToSave(from: presentingViewController)
84+
} else {
85+
WPAnalytics.trackEvent(.statsInsightsManagementDismissed)
86+
insightsDelegate?.addInsightDismissed?()
87+
}
8088
}
89+
} else if selectedStat == nil {
90+
insightsDelegate?.addInsightDismissed?()
8191
}
8292
}
8393

@@ -142,30 +152,68 @@ class AddInsightTableViewController: UITableViewController {
142152
return
143153
}
144154

145-
if insightsShown != originalInsightsShown {
155+
if hasChanges {
146156
navigationItem.rightBarButtonItem = saveButton
147157
} else {
148158
navigationItem.rightBarButtonItem = nil
149159
}
150160
}
151161

152-
@objc func saveInsights() {
153-
insightsManagementDelegate?.userUpdatedActiveInsights(insightsShown)
162+
@objc private func doneTapped() {
163+
dismissedViaButton = true
164+
165+
if FeatureFlag.statsNewAppearance.enabled && hasChanges {
166+
promptToSave(from: self)
167+
} else {
168+
dismiss()
169+
}
170+
}
171+
172+
@objc func saveTapped() {
173+
dismissedViaButton = true
174+
175+
saveChanges()
154176

155177
dismiss(animated: true, completion: nil)
156178
}
157179

158-
@objc private func doneTapped() {
180+
private func dismiss() {
159181
WPAnalytics.trackEvent(.statsInsightsManagementDismissed)
182+
insightsDelegate?.addInsightDismissed?()
183+
160184
dismiss(animated: true, completion: nil)
185+
}
186+
187+
private func saveChanges() {
188+
insightsManagementDelegate?.userUpdatedActiveInsights(insightsShown)
189+
190+
WPAnalytics.track(.statsInsightsManagementSaved, properties: ["types": [insightsShown.map({$0.title})]])
161191

162-
// TODO: Prompt user if they have unsaved changes
192+
// Update original to stop us detecting changes on dismiss
193+
originalInsightsShown = insightsShown
194+
}
195+
196+
private func promptToSave(from viewController: UIViewController?) {
197+
let alert = UIAlertController(title: nil, message: TextContent.savePromptMessage, preferredStyle: .actionSheet)
198+
alert.addAction(UIAlertAction(title: TextContent.savePromptSaveButton, style: .default, handler: { _ in
199+
self.saveTapped()
200+
}))
201+
alert.addAction(UIAlertAction(title: TextContent.savePromptDiscardButton, style: .destructive, handler: { _ in
202+
self.dismiss()
203+
}))
204+
alert.addCancelActionWithTitle(TextContent.savePromptCancelButton, handler: nil)
205+
viewController?.present(alert, animated: true, completion: nil)
163206
}
164207

165208
fileprivate enum TextContent {
166209
static let title = NSLocalizedString("stats.insights.management.title", value: "Manage Stats Cards", comment: "Title of the Stats Insights Management screen")
167210
static let activeCardsHeader = NSLocalizedString("stats.insights.management.activeCards", value: "Active Cards", comment: "Header title indicating which Stats Insights cards the user currently has set to active.")
168211
static let placeholderRowTitle = NSLocalizedString("stats.insights.management.selectCardsPrompt", value: "Select cards from the list below", comment: "Prompt displayed on the Stats Insights management screen telling the user to tap a row to add it to their list of active cards.")
212+
213+
static let savePromptMessage = NSLocalizedString("stats.insights.management.savePrompt.message", value: "You've made changes to your active Insights cards.", comment: "Title of alert in Stats Insights management, prompting the user to save changes to their list of active Stats cards.")
214+
static let savePromptSaveButton = NSLocalizedString("stats.insights.management.savePrompt.saveButton", value: "Save Changes", comment: "Title of button in Stats Insights management, prompting the user to save changes to their list of active Stats cards.")
215+
static let savePromptDiscardButton = NSLocalizedString("stats.insights.management.savePrompt.discardButton", value: "Discard Changes", comment: "Title of button in Stats Insights management, prompting the user to discard changes to their list of active Stats cards.")
216+
static let savePromptCancelButton = NSLocalizedString("stats.insights.management.savePrompt.cancelButton", value: "Cancel", comment: "Title of button to cancel an alert and take no action.")
169217
}
170218
}
171219

WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -645,15 +645,10 @@ extension SiteStatsInsightsTableViewController: StatsInsightsManagementDelegate
645645
return
646646
}
647647

648-
// TODO: Tracks events
649648
insightsToShow = insightTypes
650649
refreshInsights(forceRefresh: true)
651650
updateView()
652651
}
653-
654-
func insightsManagementDismissed() {
655-
656-
}
657652
}
658653

659654
// MARK: - SharingViewControllerDelegate

0 commit comments

Comments
 (0)