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 pathTimeZoneServiceRemote.swift
More file actions
59 lines (53 loc) · 2.38 KB
/
Copy pathTimeZoneServiceRemote.swift
File metadata and controls
59 lines (53 loc) · 2.38 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
import Foundation
public class TimeZoneServiceRemote: ServiceRemoteWordPressComREST {
public enum ResponseError: Error {
case decodingFailed
}
public func getTimezones(success: @escaping (([TimeZoneGroup]) -> Void), failure: @escaping ((Error) -> Void)) {
let endpoint = "timezones"
let path = self.path(forEndpoint: endpoint, withVersion: ._2_0)
wordPressComRESTAPI.get(path, parameters: nil, success: { (response, _) in
do {
let groups = try self.timezoneGroupsFromResponse(response)
success(groups)
} catch {
failure(error)
}
}) { (error, _) in
failure(error)
}
}
}
private extension TimeZoneServiceRemote {
func timezoneGroupsFromResponse(_ response: Any) throws -> [TimeZoneGroup] {
guard let response = response as? [String: Any],
let timeZonesByContinent = response["timezones_by_continent"] as? [String: [[String: String]]],
let manualUTCOffsets = response["manual_utc_offsets"] as? [[String: String]] else {
throw ResponseError.decodingFailed
}
let continentGroups: [TimeZoneGroup] = try timeZonesByContinent.map({
let (groupName, rawZones) = $0
let zones = try rawZones.map({ try parseNamedTimezone(response: $0) })
return TimeZoneGroup(name: groupName, timezones: zones)
}).sorted(by: { return $0.name < $1.name })
let utcOffsets: [WPTimeZone] = try manualUTCOffsets.map({ try parseOffsetTimezone(response: $0) })
let utcOffsetsGroup = TimeZoneGroup(
name: NSLocalizedString("Manual Offsets", comment: "Section name for manual offsets in time zone selector"),
timezones: utcOffsets)
return continentGroups + [utcOffsetsGroup]
}
func parseNamedTimezone(response: [String: String]) throws -> WPTimeZone {
guard let label = response["label"],
let value = response["value"] else {
throw ResponseError.decodingFailed
}
return NamedTimeZone(label: label, value: value)
}
func parseOffsetTimezone(response: [String: String]) throws -> WPTimeZone {
guard let value = response["value"],
let zone = OffsetTimeZone.fromValue(value) else {
throw ResponseError.decodingFailed
}
return zone
}
}