From 53f3ee7e4ba4d96663971f9278e9de026bf06abb Mon Sep 17 00:00:00 2001 From: Momo Ozawa Date: Fri, 10 Sep 2021 11:12:09 +0100 Subject: [PATCH 1/4] Add: load / dimiss a permanently dimissable insight --- ...SiteStatsInsightsTableViewController.swift | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift b/WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift index 55948b6fde35..f58a3a516e97 100644 --- a/WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift +++ b/WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift @@ -297,6 +297,33 @@ private extension SiteStatsInsightsTableViewController { UserDefaults.standard.set(hideCustomizeCard, forKey: userDefaultsHideCustomizeKey) } + /// Loads an insight that can be permanently dismissed. Adds or removes the insight from the list of insights to show. + /// + /// - Parameters: + /// - insight: An insight that can be permanently dismissed for all sites + /// - userDefaultsHideInsightKey: The UserDefaults key that indicates whether or not the insight should be hidden + func loadPermanentlyDismissableInsight(_ insight: InsightType, using userDefaultsHideInsightKey: String) { + + let shouldAddInsight = + !UserDefaults.standard.bool(forKey: userDefaultsHideInsightKey) && !insightsToShow.contains(insight) + + if shouldAddInsight { + insightsToShow.insert(insight, at: 0) + } else { + insightsToShow = insightsToShow.filter { $0 != insight } + } + } + + /// Permanently dismisses an insight for all sites. + /// + /// - Parameters: + /// - insight: An insight that can be permanently dismissed for all sites + /// - userDefaultsHideInsightKey: The UserDefaults key that indicates whether or not the insight should be hidden + func permanentlyDismissInsight(_ insight: InsightType, using userDefaultsHideInsightKey: String) { + insightsToShow = insightsToShow.filter { $0 != insight } + UserDefaults.standard.set(true, forKey: userDefaultsHideInsightKey) + } + // MARK: - Customize Card Management func dismissCustomizeCard() { From 99bf079aa8a1b1a105a4936b0386fde3952dae62 Mon Sep 17 00:00:00 2001 From: Momo Ozawa Date: Fri, 10 Sep 2021 11:14:19 +0100 Subject: [PATCH 2/4] Refactor: minimum index for movable cards --- .../Stats/Insights/SiteStatsInsightsTableViewController.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift b/WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift index f58a3a516e97..f8350d6c60ba 100644 --- a/WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift +++ b/WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift @@ -396,7 +396,9 @@ private extension SiteStatsInsightsTableViewController { } func canMoveInsightUp(_ insight: InsightType) -> Bool { - let minIndex = hideCustomizeCard ? 0 : 1 + let isShowingPinnedCard = insightsToShow.contains(.customize) + + let minIndex = isShowingPinnedCard ? 1 : 0 guard let currentIndex = indexOfInsight(insight), (currentIndex - 1) >= minIndex else { From 14be7cbe0f50c8466e50d3710bb5f3baa5687acd Mon Sep 17 00:00:00 2001 From: Momo Ozawa Date: Fri, 10 Sep 2021 11:20:14 +0100 Subject: [PATCH 3/4] Refactor: loading / dismissing customize card --- .../SiteStatsInsightsTableViewController.swift | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift b/WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift index f8350d6c60ba..66be67c3e278 100644 --- a/WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift +++ b/WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift @@ -288,11 +288,6 @@ private extension SiteStatsInsightsTableViewController { addCustomizeCard() } - func loadCustomizeCardSetting() { - hideCustomizeCard = UserDefaults.standard.bool(forKey: userDefaultsHideCustomizeKey) - addCustomizeCard() - } - func writeCustomizeCardSetting() { UserDefaults.standard.set(hideCustomizeCard, forKey: userDefaultsHideCustomizeKey) } @@ -326,9 +321,12 @@ private extension SiteStatsInsightsTableViewController { // MARK: - Customize Card Management + func loadCustomizeCardSetting() { + loadPermanentlyDismissableInsight(.customize, using: userDefaultsHideCustomizeKey) + } + func dismissCustomizeCard() { - hideCustomizeCard = true - removeCustomizeCard() + permanentlyDismissInsight(.customize, using: userDefaultsHideCustomizeKey) } func removeCustomizeCard() { From 5982e7c4e7f7bc17eabb7eafd8c3d0bcadc2989f Mon Sep 17 00:00:00 2001 From: Momo Ozawa Date: Fri, 10 Sep 2021 11:37:24 +0100 Subject: [PATCH 4/4] Delete: obsolete properties and methods --- ...SiteStatsInsightsTableViewController.swift | 24 ------------------- 1 file changed, 24 deletions(-) diff --git a/WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift b/WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift index 66be67c3e278..23833f47e3d4 100644 --- a/WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift +++ b/WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsTableViewController.swift @@ -98,7 +98,6 @@ class SiteStatsInsightsTableViewController: UITableViewController, StoryboardLoa // Store 'customize' separately as it is not per site. private let userDefaultsHideCustomizeKey = "StatsInsightsHideCustomizeCard" - private var hideCustomizeCard = false // Store Insights settings for all sites. // Used when writing to/reading from User Defaults. @@ -266,15 +265,10 @@ private extension SiteStatsInsightsTableViewController { func writeInsightsToUserDefaults() { - writeCustomizeCardSetting() - guard let siteID = SiteStatsInformation.sharedInstance.siteID?.stringValue else { return } - // Remove 'customize' from array since it is not per site. - removeCustomizeCard() - let insightTypesValues = InsightType.valuesForTypes(insightsToShow) let currentSiteInsights = [siteID: insightTypesValues] @@ -283,13 +277,6 @@ private extension SiteStatsInsightsTableViewController { allSitesInsights.append(currentSiteInsights) UserDefaults.standard.set(allSitesInsights, forKey: userDefaultsInsightTypesKey) - - // Add back 'customize'. - addCustomizeCard() - } - - func writeCustomizeCardSetting() { - UserDefaults.standard.set(hideCustomizeCard, forKey: userDefaultsHideCustomizeKey) } /// Loads an insight that can be permanently dismissed. Adds or removes the insight from the list of insights to show. @@ -329,17 +316,6 @@ private extension SiteStatsInsightsTableViewController { permanentlyDismissInsight(.customize, using: userDefaultsHideCustomizeKey) } - func removeCustomizeCard() { - insightsToShow = insightsToShow.filter { $0 != .customize } - } - - func addCustomizeCard() { - if !hideCustomizeCard && !insightsToShow.contains(.customize) { - // Insert customize at the beginning of the array so it is displayed first. - insightsToShow.insert(.customize, at: 0) - } - } - // MARK: - Insights Management func showAddInsightView() {