Refactor map styles for forking (#1364)

* Add MapLibreStyle

* Refactor url builders

* Add doc

* Add UTs

* Rename style

* Improve doc

* Update project

* Amend doc

* Improve documentation
This commit is contained in:
Alfonso Grillo
2023-07-20 17:09:50 +02:00
committed by GitHub
parent 0561bac46b
commit e92b2c94ed
10 changed files with 124 additions and 56 deletions
@@ -199,9 +199,8 @@ final class AppSettings {
// MARK: - Maps
// maptiler map urls
let lightTileMapStyleURL: URL = "https://api.maptiler.com/maps/9bc819c8-e627-474a-a348-ec144fe3d810"
let darkTileMapStyleURL: URL = "https://api.maptiler.com/maps/dea61faf-292b-4774-9660-58fcef89a7f3"
// maptiler base url
let mapTilerBaseURL: URL = "https://api.maptiler.com/maps"
// maptiler api key
let mapTilerApiKey = InfoPlistReader.main.mapLibreAPIKey
@@ -18,7 +18,7 @@ import Foundation
extension MapTilerStyleBuilder {
init(appSettings: AppSettings) {
self.init(lightURL: appSettings.lightTileMapStyleURL, darkURL: appSettings.darkTileMapStyleURL, key: appSettings.mapTilerApiKey)
self.init(baseURL: appSettings.mapTilerBaseURL, key: appSettings.mapTilerApiKey)
}
}
@@ -28,11 +28,6 @@ enum ShowUserLocationMode {
case hide
}
enum MapTilerStyle {
case light
case dark
}
enum MapLibreError: Error {
case failedLoadingMap
case failedLocatingUser
@@ -17,25 +17,17 @@
import CoreLocation
struct MapTilerStaticMap: MapTilerStaticMapProtocol {
private let lightURL: URL
private let darkURL: URL
private let baseURL: URL
private let key: String
init(key: String, lightURL: URL, darkURL: URL) {
self.lightURL = lightURL
self.darkURL = darkURL
init(baseURL: URL, key: String) {
self.baseURL = baseURL
self.key = key
}
func staticMapURL(for style: MapTilerStyle, coordinates: CLLocationCoordinate2D, zoomLevel: Double, size: CGSize, attribution: MapTilerAttributionPlacement) -> URL? {
var url: URL
switch style {
case .light:
url = lightURL
case .dark:
url = darkURL
}
var url: URL = baseURL
url.appendPathComponent(style.rawValue, conformingTo: .item)
url.appendPathComponent(String(format: "static/%f,%f,%f/%dx%d@2x.png", coordinates.longitude, coordinates.latitude, zoomLevel, Int(size.width), Int(size.height)), conformingTo: .png)
url.append(queryItems: [.init(name: "attribution", value: attribution.rawValue)])
let authorization = MapTilerAuthorization(key: key)
@@ -0,0 +1,23 @@
//
// Copyright 2023 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/// The style for a map.
/// Values should be Map Libre style IDs generated with an account where the API key belongs to.
/// For more information read [FORKING.md](https://github.com/vector-im/element-x-ios/blob/develop/docs/FORKING.md#setup-the-location-sharing).
enum MapTilerStyle: String {
case light = "9bc819c8-e627-474a-a348-ec144fe3d810"
case dark = "dea61faf-292b-4774-9660-58fcef89a7f3"
}
@@ -17,25 +17,17 @@
import Foundation
struct MapTilerStyleBuilder: MapTilerStyleBuilderProtocol {
private let lightURL: URL
private let darkURL: URL
private let baseURL: URL
private let key: String
init(lightURL: URL, darkURL: URL, key: String) {
self.lightURL = lightURL
self.darkURL = darkURL
init(baseURL: URL, key: String) {
self.baseURL = baseURL
self.key = key
}
func dynamicMapURL(for style: MapTilerStyle) -> URL? {
var url: URL
switch style {
case .light:
url = lightURL
case .dark:
url = darkURL
}
var url: URL = baseURL
url.appendPathComponent(style.rawValue, conformingTo: .item)
url.appendPathComponent("style.json", conformingTo: .json)
let authorization = MapTilerAuthorization(key: key)
return authorization.authorizeURL(url)
@@ -70,9 +70,8 @@ private extension MapLibreStaticMapView {
self.init(coordinates: .init(latitude: geoURI.latitude, longitude: geoURI.longitude),
zoomLevel: 15,
attributionPlacement: .bottomLeft,
mapTilerStatic: MapTilerStaticMap(key: ServiceLocator.shared.settings.mapTilerApiKey,
lightURL: ServiceLocator.shared.settings.lightTileMapStyleURL,
darkURL: ServiceLocator.shared.settings.darkTileMapStyleURL),
mapTilerStatic: MapTilerStaticMap(baseURL: ServiceLocator.shared.settings.mapTilerBaseURL,
key: ServiceLocator.shared.settings.mapTilerApiKey),
pinAnnotationView: pinAnnotationView)
}
}