This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathStatsTopCountryTimeIntervalData.swift
More file actions
87 lines (72 loc) · 2.58 KB
/
Copy pathStatsTopCountryTimeIntervalData.swift
File metadata and controls
87 lines (72 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
public struct StatsTopCountryTimeIntervalData {
public let period: StatsPeriodUnit
public let periodEndDate: Date
public let totalViewsCount: Int
public let otherViewsCount: Int
public let countries: [StatsCountry]
public init(period: StatsPeriodUnit,
periodEndDate: Date,
countries: [StatsCountry],
totalViewsCount: Int,
otherViewsCount: Int) {
self.period = period
self.periodEndDate = periodEndDate
self.countries = countries
self.totalViewsCount = totalViewsCount
self.otherViewsCount = otherViewsCount
}
}
public struct StatsCountry {
public let name: String
public let code: String
public let viewsCount: Int
public init(name: String,
code: String,
viewsCount: Int) {
self.name = name
self.code = code
self.viewsCount = viewsCount
}
}
extension StatsTopCountryTimeIntervalData: StatsTimeIntervalData {
public static var pathComponent: String {
return "stats/country-views"
}
public init?(date: Date, period: StatsPeriodUnit, jsonDictionary: [String: AnyObject]) {
guard
let unwrappedDays = type(of: self).unwrapDaysDictionary(jsonDictionary: jsonDictionary),
let countriesViews = unwrappedDays["views"] as? [[String: AnyObject]]
else {
return nil
}
let countryInfo = jsonDictionary["country-info"] as? [String: AnyObject] ?? [:]
let totalViews = unwrappedDays["total_views"] as? Int ?? 0
let otherViews = unwrappedDays["other_views"] as? Int ?? 0
self.periodEndDate = date
self.period = period
self.totalViewsCount = totalViews
self.otherViewsCount = otherViews
self.countries = countriesViews.compactMap { StatsCountry(jsonDictionary: $0, countryInfo: countryInfo) }
}
}
extension StatsCountry {
init?(jsonDictionary: [String: AnyObject], countryInfo: [String: AnyObject]) {
guard
let viewsCount = jsonDictionary["views"] as? Int,
let countryCode = jsonDictionary["country_code"] as? String
else {
return nil
}
let name: String
if
let countryDict = countryInfo[countryCode] as? [String: AnyObject],
let countryName = countryDict["country_full"] as? String {
name = countryName
} else {
name = countryCode
}
self.viewsCount = viewsCount
self.code = countryCode
self.name = name
}
}