@@ -2,17 +2,25 @@ import Charts
22import UIKit
33import Yosemite
44
5+ /// Different display modes of site visit stats
6+ ///
7+ enum SiteVisitStatsMode {
8+ case `default`
9+ case redactedDueToJetpack
10+ case hidden
11+ }
12+
513/// Shows the store stats with v4 API for a time range.
614///
7- class StoreStatsV4PeriodViewController : UIViewController {
15+ final class StoreStatsV4PeriodViewController : UIViewController {
816
917 // MARK: - Public Properties
1018
1119 let granularity : StatsGranularityV4
1220
13- var shouldShowSiteVisitStats : Bool = true {
21+ var siteVisitStatsMode : SiteVisitStatsMode = . default {
1422 didSet {
15- updateSiteVisitStatsVisibility ( shouldShowSiteVisitStats : shouldShowSiteVisitStats )
23+ updateSiteVisitStats ( mode : siteVisitStatsMode )
1624 }
1725 }
1826
@@ -145,6 +153,7 @@ class StoreStatsV4PeriodViewController: UIViewController {
145153 roundSmallNumbers: false ) ?? String ( )
146154 }
147155
156+ private lazy var visitorsEmptyView = StoreStatsSiteVisitEmptyView ( )
148157 // MARK: - Initialization
149158
150159 /// Designated Initializer
@@ -270,6 +279,12 @@ private extension StoreStatsV4PeriodViewController {
270279 timeRangeBarView. backgroundColor = . systemColor( . secondarySystemGroupedBackground)
271280 visitorsStackView. backgroundColor = . systemColor( . secondarySystemGroupedBackground)
272281
282+ // Visitor empty view - insert it at the second-to-last index,
283+ // since we need the footer view (with height = 20) as the last item in the stack view.
284+ let emptyViewIndex = max ( 0 , visitorsStackView. arrangedSubviews. count - 2 )
285+ visitorsStackView. insertArrangedSubview ( visitorsEmptyView, at: emptyViewIndex)
286+ visitorsEmptyView. isHidden = true
287+
273288 // Time range bar bottom border view
274289 timeRangeBarBottomBorderView. backgroundColor = . systemColor( . separator)
275290
@@ -292,7 +307,7 @@ private extension StoreStatsV4PeriodViewController {
292307 lastUpdated. backgroundColor = . listForeground
293308
294309 // Visibility
295- updateSiteVisitStatsVisibility ( shouldShowSiteVisitStats : shouldShowSiteVisitStats )
310+ updateSiteVisitStats ( mode : siteVisitStatsMode )
296311
297312 // Accessibility elements
298313 xAxisAccessibilityView. isAccessibilityElement = true
@@ -402,9 +417,16 @@ private extension StoreStatsV4PeriodViewController {
402417
403418// MARK: - UI Updates
404419//
405- extension StoreStatsV4PeriodViewController {
406- func updateSiteVisitStatsVisibility( shouldShowSiteVisitStats: Bool ) {
407- visitorsStackView? . isHidden = !shouldShowSiteVisitStats
420+ private extension StoreStatsV4PeriodViewController {
421+ func updateSiteVisitStats( mode: SiteVisitStatsMode ) {
422+ // Hides site visit stats for "today"
423+ guard timeRange != . today else {
424+ visitorsStackView. isHidden = true
425+ return
426+ }
427+
428+ visitorsStackView. isHidden = mode == . hidden
429+ reloadSiteFields ( )
408430 }
409431}
410432
@@ -463,30 +485,28 @@ private extension StoreStatsV4PeriodViewController {
463485 ///
464486 /// - Parameter selectedIndex: the index of interval data for the bar chart. Nil if no bar is selected.
465487 func updateSiteVisitStats( selectedIndex: Int ? ) {
466- guard shouldShowSiteVisitStats else {
467- return
468- }
469- guard let selectedIndex = selectedIndex else {
470- updateSiteVisitStatsVisibility ( shouldShowSiteVisitStats: shouldShowSiteVisitStats)
471- reloadSiteFields ( )
472- return
473- }
474- // Hides site visit stats for "today".
475- guard timeRange != . today else {
476- updateSiteVisitStatsVisibility ( shouldShowSiteVisitStats: false )
477- return
478- }
479- updateSiteVisitStatsVisibility ( shouldShowSiteVisitStats: true )
480- guard visitorsData != nil else {
481- return
482- }
483-
484- var visitorsText = Constants . placeholderText
485- if selectedIndex < siteStatsItems. count {
486- let siteStatsItem = siteStatsItems [ selectedIndex]
487- visitorsText = Double ( siteStatsItem. visitors) . humanReadableString ( )
488+ updateSiteVisitStats ( mode: siteVisitStatsMode)
489+
490+ switch siteVisitStatsMode {
491+ case . hidden, . redactedDueToJetpack:
492+ break
493+ case . default:
494+ guard let selectedIndex = selectedIndex else {
495+ reloadSiteFields ( )
496+ return
497+ }
498+ guard visitorsData != nil else {
499+ return
500+ }
501+ var visitorsText = Constants . placeholderText
502+ if selectedIndex < siteStatsItems. count {
503+ let siteStatsItem = siteStatsItems [ selectedIndex]
504+ visitorsText = Double ( siteStatsItem. visitors) . humanReadableString ( )
505+ }
506+ visitorsData. text = visitorsText
507+ visitorsData. isHidden = false
508+ visitorsEmptyView. isHidden = true
488509 }
489- visitorsData. text = visitorsText
490510 }
491511
492512 /// Updates date bar based on the selected bar index.
@@ -655,8 +675,19 @@ private extension StoreStatsV4PeriodViewController {
655675 reloadSiteFields ( )
656676 reloadChart ( animateChart: animateChart)
657677 reloadLastUpdatedField ( )
658- let visitStatsElements = shouldShowSiteVisitStats ? [ visitorsTitle as Any ,
659- visitorsData as Any ] : [ ]
678+ let visitStatsElements : [ Any ] = {
679+ switch siteVisitStatsMode {
680+ case . default:
681+ return [ visitorsTitle as Any ,
682+ visitorsData as Any ]
683+ case . redactedDueToJetpack:
684+ return [ visitorsTitle as Any ,
685+ visitorsEmptyView as Any ]
686+ case . hidden:
687+ return [ ]
688+ }
689+ } ( )
690+
660691 view. accessibilityElements = visitStatsElements + [ ordersTitle as Any ,
661692 ordersData as Any ,
662693 revenueTitle as Any ,
@@ -685,15 +716,25 @@ private extension StoreStatsV4PeriodViewController {
685716 }
686717
687718 func reloadSiteFields( ) {
688- guard visitorsData != nil else {
689- return
690- }
719+ switch siteVisitStatsMode {
720+ case . hidden:
721+ break
722+ case . redactedDueToJetpack:
723+ visitorsData. isHidden = true
724+ visitorsEmptyView. isHidden = false
725+ case . default:
726+ guard visitorsData != nil else {
727+ return
728+ }
691729
692- var visitorsText = Constants . placeholderText
693- if let siteStats = siteStats {
694- visitorsText = Double ( siteStats. totalVisitors) . humanReadableString ( )
730+ var visitorsText = Constants . placeholderText
731+ if let siteStats = siteStats {
732+ visitorsText = Double ( siteStats. totalVisitors) . humanReadableString ( )
733+ }
734+ visitorsData. text = visitorsText
735+ visitorsData. isHidden = false
736+ visitorsEmptyView. isHidden = true
695737 }
696- visitorsData. text = visitorsText
697738 }
698739
699740 func reloadChart( animateChart: Bool = true ) {
0 commit comments