-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGEOCountryAdministrationData.swift
More file actions
69 lines (57 loc) · 2.95 KB
/
GEOCountryAdministrationData.swift
File metadata and controls
69 lines (57 loc) · 2.95 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
//
// GEOCountryAdministrationData.swift
// RKGeonames
//
// Created by Stefan Buretea on 1/17/17.
// Copyright © 2017 Stefan Burettea. All rights reserved.
//
import Foundation
import RxCocoa
import RxSwift
class GEOCountryAdministrationData: GEOCountryDomainData {
let country: GEOCountry
let year: String
fileprivate enum Sections: String {
case capitalCity = "CAPITAL CITY"
case surface = "SURFACE"
case currentTime = "CURRENT TIME"
case timeZone = "TIMEZONE"
case sunrise = "SUNRISE"
case sunset = "SUNSET"
static let allValues:[Sections] = [.capitalCity,.surface,.currentTime,.timeZone,.sunrise,.sunset]
}
fileprivate struct Constants {
static let square = 0x00B2;
}
required init(country: GEOCountry, year: String) {
self.country = country
self.year = year
}
func retrieve() -> Observable<[GEOCountryDomainDataItem]> {
var result:[GEOCountryDomainDataItem] = []
result.append(GEOCountryDomainDataItem(title: Sections.capitalCity.rawValue, value: country.capitalCity ?? ""))
result.append(GEOCountryDomainDataItem(title: Sections.surface.rawValue, value: String(format:"%@ km%C", country.surfaceArea, Constants.square)))
let cityRequest = GEOCapitalCityRequest(country: country)
return GEOHTTPClient.requestCityData(with: cityRequest)
.map { cities in
return cities.index(where: { city in
guard let cityCountryCode = city.countrycode, let name = city.name,
let countryCapitalCity = self.country.capitalCity,
let countryCode = self.country.countryCode else { return false }
return name == countryCapitalCity && cityCountryCode == countryCode
}).map { cities[$0] }
}
.flatMapLatest { city -> Observable<GEOTimezone> in
guard let latitude = city?.lat, let longitude = city?.lng else { return Observable.empty() }
guard let timezoneRequest = GEOTimezoneRequest(latitude: "\(latitude)", longitude: "\(longitude)") else { return Observable.empty() }
return GEOHTTPClient.requestTimezone(with: timezoneRequest)
}
.flatMapLatest { timezone -> Observable<[GEOCountryDomainDataItem]> in
result.append(GEOCountryDomainDataItem(title: Sections.currentTime.rawValue, value: timezone.time ?? ""))
result.append(GEOCountryDomainDataItem(title: Sections.timeZone.rawValue, value: timezone.asString))
result.append(GEOCountryDomainDataItem(title: Sections.sunset.rawValue, value: timezone.sunset ?? ""))
result.append(GEOCountryDomainDataItem(title: Sections.sunrise.rawValue, value: timezone.sunrise ?? ""))
return Observable.just(result)
}
}
}