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 pathStatsTopClicksTimeIntervalData.swift
More file actions
94 lines (79 loc) · 2.83 KB
/
Copy pathStatsTopClicksTimeIntervalData.swift
File metadata and controls
94 lines (79 loc) · 2.83 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
88
89
90
91
92
93
94
public struct StatsTopClicksTimeIntervalData {
public let period: StatsPeriodUnit
public let periodEndDate: Date
public let totalClicksCount: Int
public let otherClicksCount: Int
public let clicks: [StatsClick]
public init(period: StatsPeriodUnit,
periodEndDate: Date,
clicks: [StatsClick],
totalClicksCount: Int,
otherClicksCount: Int) {
self.period = period
self.periodEndDate = periodEndDate
self.clicks = clicks
self.totalClicksCount = totalClicksCount
self.otherClicksCount = otherClicksCount
}
}
public struct StatsClick {
public let title: String
public let clicksCount: Int
public let clickedURL: URL?
public let iconURL: URL?
public let children: [StatsClick]
public init(title: String,
clicksCount: Int,
clickedURL: URL?,
iconURL: URL?,
children: [StatsClick]) {
self.title = title
self.clicksCount = clicksCount
self.clickedURL = clickedURL
self.iconURL = iconURL
self.children = children
}
}
extension StatsTopClicksTimeIntervalData: StatsTimeIntervalData {
public static var pathComponent: String {
return "stats/clicks"
}
public init?(date: Date, period: StatsPeriodUnit, jsonDictionary: [String: AnyObject]) {
guard
let unwrappedDays = type(of: self).unwrapDaysDictionary(jsonDictionary: jsonDictionary),
let clicks = unwrappedDays["clicks"] as? [[String: AnyObject]]
else {
return nil
}
let totalClicks = unwrappedDays["total_clicks"] as? Int ?? 0
let otherClicks = unwrappedDays["other_clicks"] as? Int ?? 0
self.period = period
self.periodEndDate = date
self.totalClicksCount = totalClicks
self.otherClicksCount = otherClicks
self.clicks = clicks.compactMap { StatsClick(jsonDictionary: $0) }
}
}
extension StatsClick {
init?(jsonDictionary: [String: AnyObject]) {
guard
let title = jsonDictionary["name"] as? String,
let clicksCount = jsonDictionary["views"] as? Int
else {
return nil
}
let children: [StatsClick]
if let childrenJSON = jsonDictionary["children"] as? [[String: AnyObject]] {
children = childrenJSON.compactMap { StatsClick(jsonDictionary: $0) }
} else {
children = []
}
let icon = jsonDictionary["icon"] as? String
let urlString = jsonDictionary["url"] as? String
self.title = title
self.clicksCount = clicksCount
self.clickedURL = urlString.flatMap { URL(string: $0) }
self.iconURL = icon.flatMap { URL(string: $0) }
self.children = children
}
}