Skip to content

Commit

Permalink
Merge pull request #17332 from wordpress-mobile/task/empty-stats-save…
Browse files Browse the repository at this point in the history
…d-tab

Empty Stats: Stay on insights tab if Grow Audience card is showing
  • Loading branch information
momo-ozawa authored Oct 18, 2021
2 parents 6e1b759 + f280cfa commit af5fb8d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
6 changes: 5 additions & 1 deletion WordPress/Classes/Utility/ContentCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ struct DefaultContentCoordinator: ContentCoordinator {
}

private func setTimePeriodForStatsURLIfPossible(_ url: URL) {
guard let key = SiteStatsDashboardViewController.lastSelectedStatsPeriodTypeKey else {
return
}

let matcher = RouteMatcher(routes: UniversalLinkRouter.statsRoutes)
let matches = matcher.routesMatching(url)
if let match = matches.first,
let action = match.action as? StatsRoute,
let timePeriod = action.timePeriod {
// Initializing a StatsPeriodType to ensure we have a valid period
UserDefaults.standard.set(timePeriod.rawValue, forKey: SiteStatsDashboardViewController.lastSelectedStatsPeriodTypeKey)
UserDefaults.standard.set(timePeriod.rawValue, forKey: key)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class SiteStatsInsightsTableViewController: UITableViewController, StoryboardLoa
static var defaultStoryboardName: String = "SiteStatsDashboard"

// MARK: - Properties
var isGrowAudienceShowing: Bool {
return insightsToShow.contains(.growAudience)
}

private var insightsChangeReceipt: Receipt?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ fileprivate extension StatsPeriodType {

class SiteStatsDashboardViewController: UIViewController {

static let lastSelectedStatsPeriodTypeKey = "LastSelectedStatsPeriodType"
static var lastSelectedStatsPeriodTypeKey: String? {
guard let siteID = SiteStatsInformation.sharedInstance.siteID?.intValue else {
return nil
}
return "LastSelectedStatsPeriodType-\(siteID)"
}

static let lastSelectedStatsDateKey = "LastSelectedStatsDate"

// MARK: - Properties
Expand Down Expand Up @@ -148,11 +154,23 @@ private extension SiteStatsDashboardViewController {
private extension SiteStatsDashboardViewController {

func saveSelectedPeriodToUserDefaults() {
UserDefaults.standard.set(currentSelectedPeriod.rawValue, forKey: Self.lastSelectedStatsPeriodTypeKey)

guard let key = Self.lastSelectedStatsPeriodTypeKey,
!insightsTableViewController.isGrowAudienceShowing else {
return
}

UserDefaults.standard.set(currentSelectedPeriod.rawValue, forKey: key)
}

func getSelectedPeriodFromUserDefaults() -> StatsPeriodType {
return StatsPeriodType(rawValue: UserDefaults.standard.integer(forKey: Self.lastSelectedStatsPeriodTypeKey)) ?? .insights

guard let key = Self.lastSelectedStatsPeriodTypeKey,
let periodType = StatsPeriodType(rawValue: UserDefaults.standard.integer(forKey: key)) else {
return .insights
}

return periodType
}

func getLastSelectedDateFromUserDefaults() -> Date? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ class MySitesCoordinator: NSObject {
UserDefaults.standard.set(date, forKey: SiteStatsDashboardViewController.lastSelectedStatsDateKey)
}

UserDefaults.standard.set(timePeriod.rawValue, forKey: SiteStatsDashboardViewController.lastSelectedStatsPeriodTypeKey)
guard let key = SiteStatsDashboardViewController.lastSelectedStatsPeriodTypeKey else {
return
}

UserDefaults.standard.set(timePeriod.rawValue, forKey: key)

mySiteViewController.showDetailView(for: .stats)
}
Expand Down

0 comments on commit af5fb8d

Please sign in to comment.