1- public struct StatsAnnualAndMostPopularTimeInsight {
2-
1+ public struct StatsAnnualAndMostPopularTimeInsight : Codable {
32 /// - A `DateComponents` object with one field populated: `weekday`.
43 public let mostPopularDayOfWeek : DateComponents
54 public let mostPopularDayOfWeekPercentage : Int
65
76 /// - A `DateComponents` object with one field populated: `hour`.
87 public let mostPopularHour : DateComponents
98 public let mostPopularHourPercentage : Int
9+ public let years : [ Year ] ?
10+
11+ private enum CodingKeys : String , CodingKey {
12+ case mostPopularHour = " highest_hour "
13+ case mostPopularHourPercentage = " highest_hour_percent "
14+ case mostPopularDayOfWeek = " highest_day_of_week "
15+ case mostPopularDayOfWeekPercentage = " highest_day_percent "
16+ case years
17+ }
1018
11- public let annualInsightsYear : Int
12-
13- public let annualInsightsTotalPostsCount : Int
14- public let annualInsightsTotalWordsCount : Int
15- public let annualInsightsAverageWordsCount : Double
16-
17- public let annualInsightsTotalLikesCount : Int
18- public let annualInsightsAverageLikesCount : Double
19-
20- public let annualInsightsTotalCommentsCount : Int
21- public let annualInsightsAverageCommentsCount : Double
22-
23- public let annualInsightsTotalImagesCount : Int
24- public let annualInsightsAverageImagesCount : Double
25-
26- public init ( mostPopularDayOfWeek: DateComponents ,
27- mostPopularDayOfWeekPercentage: Int ,
28- mostPopularHour: DateComponents ,
29- mostPopularHourPercentage: Int ,
30- annualInsightsYear: Int ,
31- annualInsightsTotalPostsCount: Int ,
32- annualInsightsTotalWordsCount: Int ,
33- annualInsightsAverageWordsCount: Double ,
34- annualInsightsTotalLikesCount: Int ,
35- annualInsightsAverageLikesCount: Double ,
36- annualInsightsTotalCommentsCount: Int ,
37- annualInsightsAverageCommentsCount: Double ,
38- annualInsightsTotalImagesCount: Int ,
39- annualInsightsAverageImagesCount: Double ) {
40- self . mostPopularDayOfWeek = mostPopularDayOfWeek
41- self . mostPopularDayOfWeekPercentage = mostPopularDayOfWeekPercentage
42-
43- self . mostPopularHour = mostPopularHour
44- self . mostPopularHourPercentage = mostPopularHourPercentage
45-
46- self . annualInsightsYear = annualInsightsYear
47-
48- self . annualInsightsTotalPostsCount = annualInsightsTotalPostsCount
49- self . annualInsightsTotalWordsCount = annualInsightsTotalWordsCount
50- self . annualInsightsAverageWordsCount = annualInsightsAverageWordsCount
51-
52- self . annualInsightsTotalLikesCount = annualInsightsTotalLikesCount
53- self . annualInsightsAverageLikesCount = annualInsightsAverageLikesCount
54-
55- self . annualInsightsTotalCommentsCount = annualInsightsTotalCommentsCount
56- self . annualInsightsAverageCommentsCount = annualInsightsAverageCommentsCount
19+ public struct Year : Codable {
20+ public let year : String
21+ public let totalPosts : Int
22+ public let totalWords : Int
23+ public let averageWords : Double
24+ public let totalLikes : Int
25+ public let averageLikes : Double
26+ public let totalComments : Int
27+ public let averageComments : Double
28+ public let totalImages : Int
29+ public let averageImages : Double
30+
31+ private enum CodingKeys : String , CodingKey {
32+ case year
33+ case totalPosts = " total_posts "
34+ case totalWords = " total_words "
35+ case averageWords = " avg_words "
36+ case totalLikes = " total_likes "
37+ case averageLikes = " avg_likes "
38+ case totalComments = " total_comments "
39+ case averageComments = " avg_comments "
40+ case totalImages = " total_images "
41+ case averageImages = " avg_images "
42+ }
5743
58- self . annualInsightsTotalImagesCount = annualInsightsTotalImagesCount
59- self . annualInsightsAverageImagesCount = annualInsightsAverageImagesCount
44+ public init ( from decoder: Decoder ) throws {
45+ let container = try decoder. container ( keyedBy: CodingKeys . self)
46+ year = try container. decode ( String . self, forKey: . year)
47+ totalPosts = ( try ? container. decodeIfPresent ( Int . self, forKey: . totalPosts) ) ?? 0
48+ totalWords = ( try ? container. decode ( Int . self, forKey: . totalWords) ) ?? 0
49+ averageWords = ( try ? container. decode ( Double . self, forKey: . averageWords) ) ?? 0
50+ totalLikes = ( try ? container. decode ( Int . self, forKey: . totalLikes) ) ?? 0
51+ averageLikes = ( try ? container. decode ( Double . self, forKey: . averageLikes) ) ?? 0
52+ totalComments = ( try ? container. decode ( Int . self, forKey: . totalComments) ) ?? 0
53+ averageComments = ( try ? container. decode ( Double . self, forKey: . averageComments) ) ?? 0
54+ totalImages = ( try ? container. decode ( Int . self, forKey: . totalImages) ) ?? 0
55+ averageImages = ( try ? container. decode ( Double . self, forKey: . averageImages) ) ?? 0
56+ }
6057 }
6158}
6259
6360extension StatsAnnualAndMostPopularTimeInsight : StatsInsightData {
6461 public static var pathComponent : String {
6562 return " stats/insights "
6663 }
64+ }
6765
68- public init ? ( jsonDictionary: [ String : AnyObject ] ) {
69- guard
70- let highestHour = jsonDictionary [ " highest_hour " ] as? Int ,
71- let highestHourPercentageValue = jsonDictionary [ " highest_hour_percent " ] as? Double ,
72- let highestDayOfWeek = jsonDictionary [ " highest_day_of_week " ] as? Int ,
73- let highestDayOfWeekPercentageValue = jsonDictionary [ " highest_day_percent " ] as? Double ,
74- let yearlyInsights = jsonDictionary [ " years " ] as? [ [ String : AnyObject ] ] ,
75- let latestYearlyInsight = yearlyInsights. last,
76- let yearString = latestYearlyInsight [ " year " ] as? String ,
77- let currentYear = Int ( yearString)
78- else {
79- return nil
80- }
66+ extension StatsAnnualAndMostPopularTimeInsight {
67+ public init ( from decoder: Decoder ) throws {
68+ let container = try decoder. container ( keyedBy: CodingKeys . self)
69+ let years = try container. decodeIfPresent ( [ Year ] . self, forKey: . years)
70+ let highestHour = try container. decode ( Int . self, forKey: . mostPopularHour)
71+ let highestHourPercentageValue = try container. decode ( Double . self, forKey: . mostPopularHourPercentage)
72+ let highestDayOfWeek = try container. decode ( Int . self, forKey: . mostPopularDayOfWeek)
73+ let highestDayOfWeekPercentageValue = try container. decode ( Double . self, forKey: . mostPopularDayOfWeekPercentage)
8174
8275 let mappedWeekday : ( ( Int ) -> Int ) = {
8376 // iOS Calendar system is `1-based` and uses Sunday as the first day of the week.
@@ -93,20 +86,6 @@ extension StatsAnnualAndMostPopularTimeInsight: StatsInsightData {
9386 self . mostPopularDayOfWeekPercentage = Int ( highestDayOfWeekPercentageValue. rounded ( ) )
9487 self . mostPopularHour = hourComponents
9588 self . mostPopularHourPercentage = Int ( highestHourPercentageValue. rounded ( ) )
96-
97- self . annualInsightsYear = currentYear
98-
99- self . annualInsightsTotalPostsCount = latestYearlyInsight [ " total_posts " ] as? Int ?? 0
100- self . annualInsightsTotalWordsCount = latestYearlyInsight [ " total_words " ] as? Int ?? 0
101- self . annualInsightsAverageWordsCount = latestYearlyInsight [ " avg_words " ] as? Double ?? 0
102-
103- self . annualInsightsTotalLikesCount = latestYearlyInsight [ " total_likes " ] as? Int ?? 0
104- self . annualInsightsAverageLikesCount = latestYearlyInsight [ " avg_likes " ] as? Double ?? 0
105-
106- self . annualInsightsTotalCommentsCount = latestYearlyInsight [ " total_comments " ] as? Int ?? 0
107- self . annualInsightsAverageCommentsCount = latestYearlyInsight [ " avg_comments " ] as? Double ?? 0
108-
109- self . annualInsightsTotalImagesCount = latestYearlyInsight [ " total_images " ] as? Int ?? 0
110- self . annualInsightsAverageImagesCount = latestYearlyInsight [ " avg_images " ] as? Double ?? 0
89+ self . years = years
11190 }
11291}
0 commit comments