11import Foundation
22import WordPressShared
33
4- public struct StatsSubscribersSummaryData : Decodable , Equatable {
4+ public struct StatsSubscribersSummaryData : Equatable {
55 public let history : [ SubscriberData ]
6+ public let period : StatsPeriodUnit
7+ public let periodEndDate : Date
68
7- public init ( history: [ SubscriberData ] ) {
9+ public init ( history: [ SubscriberData ] , period : StatsPeriodUnit , periodEndDate : Date ) {
810 self . history = history
11+ self . period = period
12+ self . periodEndDate = periodEndDate
913 }
1014}
1115
12- extension StatsSubscribersSummaryData {
16+ extension StatsSubscribersSummaryData : StatsTimeIntervalData {
1317 public static var pathComponent : String {
1418 return " stats/subscribers "
1519 }
@@ -21,7 +25,14 @@ extension StatsSubscribersSummaryData {
2125 return df
2226 } ( )
2327
24- public struct SubscriberData : Decodable , Equatable {
28+ static var weeksDateFormatter : DateFormatter = {
29+ let df = DateFormatter ( )
30+ df. locale = Locale ( identifier: " en_US_POS " )
31+ df. dateFormat = " yyyy'W'MM'W'dd "
32+ return df
33+ } ( )
34+
35+ public struct SubscriberData : Equatable {
2536 public let date : Date
2637 public let count : Int
2738
@@ -31,7 +42,7 @@ extension StatsSubscribersSummaryData {
3142 }
3243 }
3344
34- public init ? ( jsonDictionary: [ String : AnyObject ] ) {
45+ public init ? ( date : Date , period : StatsPeriodUnit , jsonDictionary: [ String : AnyObject ] ) {
3546 guard
3647 let fields = jsonDictionary [ " fields " ] as? [ String ] ,
3748 let data = jsonDictionary [ " data " ] as? [ [ Any ] ] ,
@@ -43,9 +54,9 @@ extension StatsSubscribersSummaryData {
4354
4455 let history : [ SubscriberData ? ] = data. map { elements in
4556 guard elements. indices. contains ( dateIndex) && elements. indices. contains ( countIndex) ,
46- let dateString = elements [ dateIndex] as? String ,
47- let date = StatsSubscribersSummaryData . dateFormatter . date ( from: dateString) ,
48- let count = elements [ countIndex] as? Int
57+ let dateString = elements [ dateIndex] as? String ,
58+ let date = StatsSubscribersSummaryData . parsedDate ( from: dateString, for : period ) ,
59+ let count = elements [ countIndex] as? Int
4960 else {
5061 return nil
5162 }
@@ -54,14 +65,20 @@ extension StatsSubscribersSummaryData {
5465 }
5566
5667 let sorted = history. compactMap { $0 } . sorted ( by: { $0. date. compare ( $1. date) == . orderedAscending } )
57- self = . init( history: sorted)
68+
69+ self = . init( history: sorted, period: period, periodEndDate: date)
5870 }
5971
60- public static func queryProperties( quantity: Int , unit: StatsSubscribersSummaryData . Unit ) -> [ String : String ] {
61- return [ " quantity " : String ( quantity) , " unit " : unit. rawValue]
72+ private static func parsedDate( from dateString: String , for period: StatsPeriodUnit ) -> Date ? {
73+ switch period {
74+ case . week:
75+ return self . weeksDateFormatter. date ( from: dateString)
76+ case . day, . month, . year:
77+ return self . dateFormatter. date ( from: dateString)
78+ }
6279 }
6380
64- public enum Unit : String {
65- case day = " day "
81+ public static func queryProperties ( with date : Date , period : StatsPeriodUnit , maxCount : Int ) -> [ String : String ] {
82+ return [ " quantity " : String ( maxCount ) , " unit " : period . stringValue ]
6683 }
6784}
0 commit comments