@@ -35,18 +35,18 @@ public struct StatsSummaryData {
3535 public let period : StatsPeriodUnit
3636 public let periodStartDate : Date
3737
38- public let viewsCount : Int
39- public let visitorsCount : Int
40- public let likesCount : Int
41- public let commentsCount : Int
38+ public let viewsCount : Int ?
39+ public let visitorsCount : Int ?
40+ public let likesCount : Int ?
41+ public let commentsCount : Int ?
4242 public let postsCount : Int ?
4343
4444 public init ( period: StatsPeriodUnit ,
4545 periodStartDate: Date ,
46- viewsCount: Int ,
47- visitorsCount: Int ,
48- likesCount: Int ,
49- commentsCount: Int ,
46+ viewsCount: Int ? ,
47+ visitorsCount: Int ? ,
48+ likesCount: Int ? ,
49+ commentsCount: Int ? ,
5050 postsCount: Int ? ) {
5151 self . period = period
5252 self . periodStartDate = periodStartDate
@@ -91,14 +91,8 @@ extension StatsSummaryTimeIntervalData: StatsTimeIntervalData {
9191 // [["2019-01-01", 9001, 1234], ["2019-02-01", 1234, 1234]], where the first object in the "inner" array
9292 // is the `period`, second is `views`, etc.
9393
94- guard
95- let periodIndex = fieldsArray. firstIndex ( of: " period " ) ,
96- let viewsIndex = fieldsArray. firstIndex ( of: " views " ) ,
97- let visitorsIndex = fieldsArray. firstIndex ( of: " visitors " ) ,
98- let commentsIndex = fieldsArray. firstIndex ( of: " comments " ) ,
99- let likesIndex = fieldsArray. firstIndex ( of: " likes " )
100- else {
101- return nil
94+ guard let periodIndex = fieldsArray. firstIndex ( of: " period " ) else {
95+ return nil
10296 }
10397
10498 self . period = period
@@ -109,86 +103,46 @@ extension StatsSummaryTimeIntervalData: StatsTimeIntervalData {
109103 dataArray: $0,
110104 period: unit ?? period,
111105 periodIndex: periodIndex,
112- viewsIndex: viewsIndex ,
113- visitorsIndex: visitorsIndex ,
114- likesIndex: likesIndex ,
115- commentsIndex: commentsIndex ,
106+ viewsIndex: fieldsArray . firstIndex ( of : " views " ) ,
107+ visitorsIndex: fieldsArray . firstIndex ( of : " visitors " ) ,
108+ likesIndex: fieldsArray . firstIndex ( of : " likes " ) ,
109+ commentsIndex: fieldsArray . firstIndex ( of : " comments " ) ,
116110 postsIndex: fieldsArray. firstIndex ( of: " posts " )
117111 )
118112 }
119113 }
120114}
121115
122116private extension StatsSummaryData {
123- init ? ( dataArray: [ Any ] ,
124- period: StatsPeriodUnit ,
125- periodIndex: Int ,
126- viewsIndex: Int ? ,
127- visitorsIndex: Int ? ,
128- likesIndex: Int ? ,
129- commentsIndex: Int ? ,
130- postsIndex: Int ? ) {
131-
117+ init ? (
118+ dataArray: [ Any ] ,
119+ period: StatsPeriodUnit ,
120+ periodIndex: Int ,
121+ viewsIndex: Int ? ,
122+ visitorsIndex: Int ? ,
123+ likesIndex: Int ? ,
124+ commentsIndex: Int ? ,
125+ postsIndex: Int ?
126+ ) {
132127 guard
133128 let periodString = dataArray [ periodIndex] as? String ,
134129 let periodStart = type ( of: self ) . parsedDate ( from: periodString, for: period) else {
135130 return nil
136131 }
137132
138- let viewsCount : Int
139- let visitorsCount : Int
140- let likesCount : Int
141- let commentsCount : Int
142- var postsCount : Int ?
143-
144- if let viewsIndex = viewsIndex {
145- guard let count = dataArray [ viewsIndex] as? Int else {
146- return nil
147- }
148- viewsCount = count
149- } else {
150- viewsCount = 0
151- }
152-
153- if let visitorsIndex = visitorsIndex {
154- guard let count = dataArray [ visitorsIndex] as? Int else {
155- return nil
156- }
157- visitorsCount = count
158- } else {
159- visitorsCount = 0
160- }
161-
162- if let likesIndex = likesIndex {
163- guard let count = dataArray [ likesIndex] as? Int else {
164- return nil
165- }
166- likesCount = count
167- } else {
168- likesCount = 0
169- }
170-
171- if let commentsIndex = commentsIndex {
172- guard let count = dataArray [ commentsIndex] as? Int else {
173- return nil
174- }
175- commentsCount = count
176- } else {
177- commentsCount = 0
178- }
179-
180- if let postsIndex {
181- postsCount = dataArray [ postsIndex] as? Int
133+ func getValue( at index: Int ? ) -> Int ? {
134+ guard let index else { return nil }
135+ return dataArray [ index] as? Int
182136 }
183137
184138 self . period = period
185139 self . periodStartDate = periodStart
186140
187- self . viewsCount = viewsCount
188- self . visitorsCount = visitorsCount
189- self . likesCount = likesCount
190- self . commentsCount = commentsCount
191- self . postsCount = postsCount
141+ self . viewsCount = getValue ( at : viewsIndex )
142+ self . visitorsCount = getValue ( at : visitorsIndex )
143+ self . likesCount = getValue ( at : likesIndex )
144+ self . commentsCount = getValue ( at : commentsIndex )
145+ self . postsCount = getValue ( at : postsIndex )
192146 }
193147
194148 static func parsedDate( from dateString: String , for period: StatsPeriodUnit ) -> Date ? {
0 commit comments