mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
feat(native): an iOS map tile store, so viewed maps keep working offline
WKWebView runs no service worker for a self-hosted server, so on iOS nothing kept a map tile once it scrolled away. Binaries with the tileCache feature answer a trek-tile:// scheme from a store on disk: a tile viewed once is served from there for up to 30 days (Mapbox's limit for a device cache, applied to every provider), then fetched anew; the store keeps under 250 MB, dropping the least recently used tiles first, sends a unique app User-Agent, and never prefetches. Mapbox's own resources (style, tiles, glyphs, sprites) and Leaflet's raster tiles go through it, telemetry never does. Offline, a Mapbox map in such an app now stays Mapbox where it was viewed, and the pill offers the stored trip region instead. The offline settings show what the store takes against its limit, with a way to empty it.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import UIKit
|
||||
import Capacitor
|
||||
import CryptoKit
|
||||
import WebKit
|
||||
|
||||
/// Points the WebView at the TREK server the user picked instead of the bundled
|
||||
/// address screen. The UI then comes from that server, in the version it runs,
|
||||
@@ -17,7 +19,18 @@ class TrekBridgeViewController: CAPBridgeViewController {
|
||||
return descriptor
|
||||
}
|
||||
|
||||
/// The map tile store answers trek-tile:// (TrekTileSchemeHandler below). A
|
||||
/// scheme handler can only be set on the configuration the WebView is built from.
|
||||
private let tileHandler = TrekTileSchemeHandler()
|
||||
|
||||
override func webViewConfiguration(for instanceConfiguration: InstanceConfiguration) -> WKWebViewConfiguration {
|
||||
let configuration = super.webViewConfiguration(for: instanceConfiguration)
|
||||
configuration.setURLSchemeHandler(tileHandler, forURLScheme: TrekTileSchemeHandler.scheme)
|
||||
return configuration
|
||||
}
|
||||
|
||||
override func capacitorDidLoad() {
|
||||
TrekTileStore.shared.prune()
|
||||
bridge?.registerPluginInstance(TrekShellPlugin())
|
||||
// WebKit's own swipe back slides away a screenshot of the whole page,
|
||||
// closes no sheet and takes every drag near the edge from the UI. The UI
|
||||
@@ -44,3 +57,218 @@ enum TrekServerStore {
|
||||
return support.appendingPathComponent("offline-start-page.html")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Tile store (trek-tile://)
|
||||
|
||||
/// Map tiles the user looked at, on disk, for the WebView's maps
|
||||
/// (client/src/native/tileCache.ts). WKWebView runs no service worker for a
|
||||
/// self-hosted server, so without this nothing kept a tile once the app closed.
|
||||
///
|
||||
/// Each tile lives as `<sha256>.bin` with its content type in `<sha256>.type`.
|
||||
/// A tile older than 30 days is never served again (Mapbox's limit for a device
|
||||
/// cache, applied to every provider) and is fetched anew. The store keeps under
|
||||
/// 250 MB: past that, the tiles used least recently go first. It sits in Caches,
|
||||
/// which iOS may empty under storage pressure and never backs up.
|
||||
final class TrekTileStore {
|
||||
static let shared = TrekTileStore()
|
||||
static let maxAge: TimeInterval = 30 * 24 * 60 * 60
|
||||
static let maxBytes: Int64 = 250 * 1024 * 1024
|
||||
/// Pruning brings the store down to this share of the limit, so it does not
|
||||
/// run again on the very next tile.
|
||||
static let pruneTarget = 0.8
|
||||
|
||||
let directory: URL
|
||||
let queue = DispatchQueue(label: "com.liketrek.trek.tiles", qos: .utility)
|
||||
private var writesSincePrune = 0
|
||||
|
||||
private init() {
|
||||
let caches = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
|
||||
directory = caches.appendingPathComponent("trek-tiles", isDirectory: true)
|
||||
try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
||||
}
|
||||
|
||||
/// The tile's identity without what changes per user or session: tokens and
|
||||
/// keys come out, the rest of the query is sorted.
|
||||
static func key(for url: URL) -> String {
|
||||
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)
|
||||
let dropped: Set<String> = ["access_token", "sku", "key", "api_key", "apikey"]
|
||||
let kept = components?.queryItems?
|
||||
.filter { !dropped.contains($0.name.lowercased()) }
|
||||
.sorted { $0.name < $1.name }
|
||||
components?.queryItems = (kept?.isEmpty ?? true) ? nil : kept
|
||||
let identity = components?.string ?? url.absoluteString
|
||||
return SHA256.hash(data: Data(identity.utf8)).map { String(format: "%02x", $0) }.joined()
|
||||
}
|
||||
|
||||
private func dataURL(_ key: String) -> URL { directory.appendingPathComponent(key + ".bin") }
|
||||
private func typeURL(_ key: String) -> URL { directory.appendingPathComponent(key + ".type") }
|
||||
|
||||
/// A stored tile younger than 30 days, marked as just used. Call on `queue`.
|
||||
func read(_ key: String) -> (data: Data, type: String)? {
|
||||
let file = dataURL(key)
|
||||
guard let attributes = try? FileManager.default.attributesOfItem(atPath: file.path),
|
||||
let created = attributes[.creationDate] as? Date else { return nil }
|
||||
if Date().timeIntervalSince(created) > TrekTileStore.maxAge {
|
||||
remove(key)
|
||||
return nil
|
||||
}
|
||||
guard let data = try? Data(contentsOf: file) else { return nil }
|
||||
let type = (try? String(contentsOf: typeURL(key), encoding: .utf8)) ?? "application/octet-stream"
|
||||
try? FileManager.default.setAttributes([.modificationDate: Date()], ofItemAtPath: file.path)
|
||||
return (data, type)
|
||||
}
|
||||
|
||||
func write(_ key: String, data: Data, type: String) {
|
||||
queue.async {
|
||||
try? data.write(to: self.dataURL(key), options: .atomic)
|
||||
try? type.write(to: self.typeURL(key), atomically: true, encoding: .utf8)
|
||||
self.writesSincePrune += 1
|
||||
if self.writesSincePrune >= 200 { self.pruneNow() }
|
||||
}
|
||||
}
|
||||
|
||||
private func remove(_ key: String) {
|
||||
try? FileManager.default.removeItem(at: dataURL(key))
|
||||
try? FileManager.default.removeItem(at: typeURL(key))
|
||||
}
|
||||
|
||||
private struct Entry {
|
||||
let key: String
|
||||
let size: Int64
|
||||
let used: Date
|
||||
let created: Date
|
||||
}
|
||||
|
||||
private func entries() -> [Entry] {
|
||||
let keys: [URLResourceKey] = [.fileSizeKey, .contentModificationDateKey, .creationDateKey]
|
||||
let files = (try? FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: keys)) ?? []
|
||||
return files.filter { $0.pathExtension == "bin" }.compactMap { file in
|
||||
guard let values = try? file.resourceValues(forKeys: Set(keys)) else { return nil }
|
||||
return Entry(
|
||||
key: file.deletingPathExtension().lastPathComponent,
|
||||
size: Int64(values.fileSize ?? 0),
|
||||
used: values.contentModificationDate ?? .distantPast,
|
||||
created: values.creationDate ?? .distantPast
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Drops what is past 30 days, then the least recently used tiles while the
|
||||
/// store is over its limit.
|
||||
func prune() { queue.async { self.pruneNow() } }
|
||||
|
||||
private func pruneNow() {
|
||||
writesSincePrune = 0
|
||||
let now = Date()
|
||||
var live: [Entry] = []
|
||||
for entry in entries() {
|
||||
if now.timeIntervalSince(entry.created) > TrekTileStore.maxAge { remove(entry.key) } else { live.append(entry) }
|
||||
}
|
||||
var total = live.reduce(Int64(0)) { $0 + $1.size }
|
||||
guard total > TrekTileStore.maxBytes else { return }
|
||||
let target = Int64(Double(TrekTileStore.maxBytes) * TrekTileStore.pruneTarget)
|
||||
for entry in live.sorted(by: { $0.used < $1.used }) {
|
||||
if total <= target { break }
|
||||
remove(entry.key)
|
||||
total -= entry.size
|
||||
}
|
||||
}
|
||||
|
||||
func usage(_ done: @escaping (Int64) -> Void) {
|
||||
queue.async { done(self.entries().reduce(Int64(0)) { $0 + $1.size }) }
|
||||
}
|
||||
|
||||
func clear(_ done: @escaping () -> Void) {
|
||||
queue.async {
|
||||
try? FileManager.default.removeItem(at: self.directory)
|
||||
try? FileManager.default.createDirectory(at: self.directory, withIntermediateDirectories: true)
|
||||
done()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Serves trek-tile://host/path from the store, or from https://host/path when
|
||||
/// it has no fresh copy, storing what comes back. Offline and not stored, the
|
||||
/// map gets an error and draws nothing there, as it would for a missing tile.
|
||||
final class TrekTileSchemeHandler: NSObject, WKURLSchemeHandler {
|
||||
static let scheme = "trek-tile"
|
||||
|
||||
/// Tasks WebKit still wants an answer for. Touched on the main thread only:
|
||||
/// start and stop arrive there, and every answer is posted back there.
|
||||
private var active = Set<ObjectIdentifier>()
|
||||
|
||||
private let session: URLSession = {
|
||||
let configuration = URLSessionConfiguration.default
|
||||
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
|
||||
configuration.timeoutIntervalForRequest = 20
|
||||
configuration.httpMaximumConnectionsPerHost = 4
|
||||
let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "0"
|
||||
// A unique app User-Agent, as OpenFreeMap and OpenStreetMap ask of apps.
|
||||
configuration.httpAdditionalHeaders = ["User-Agent": "TREK-App/\(version) (iOS; +https://liketrek.com)"]
|
||||
return URLSession(configuration: configuration)
|
||||
}()
|
||||
|
||||
func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
|
||||
let id = ObjectIdentifier(urlSchemeTask)
|
||||
guard let url = urlSchemeTask.request.url,
|
||||
var components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
|
||||
urlSchemeTask.didFailWithError(URLError(.badURL))
|
||||
return
|
||||
}
|
||||
components.scheme = "https"
|
||||
guard let remote = components.url else {
|
||||
urlSchemeTask.didFailWithError(URLError(.badURL))
|
||||
return
|
||||
}
|
||||
active.insert(id)
|
||||
let key = TrekTileStore.key(for: remote)
|
||||
let store = TrekTileStore.shared
|
||||
let accept = urlSchemeTask.request.value(forHTTPHeaderField: "Accept")
|
||||
store.queue.async {
|
||||
if let hit = store.read(key) {
|
||||
DispatchQueue.main.async {
|
||||
self.respond(urlSchemeTask, id: id, url: url, status: 200, type: hit.type, data: hit.data)
|
||||
}
|
||||
return
|
||||
}
|
||||
var request = URLRequest(url: remote)
|
||||
if let accept { request.setValue(accept, forHTTPHeaderField: "Accept") }
|
||||
self.session.dataTask(with: request) { data, response, _ in
|
||||
guard let data, let http = response as? HTTPURLResponse else {
|
||||
DispatchQueue.main.async {
|
||||
self.respond(urlSchemeTask, id: id, url: url, status: 504, type: "text/plain", data: Data())
|
||||
}
|
||||
return
|
||||
}
|
||||
let type = http.value(forHTTPHeaderField: "Content-Type") ?? "application/octet-stream"
|
||||
if http.statusCode == 200, !data.isEmpty { store.write(key, data: data, type: type) }
|
||||
DispatchQueue.main.async {
|
||||
self.respond(urlSchemeTask, id: id, url: url, status: http.statusCode, type: type, data: data)
|
||||
}
|
||||
}.resume()
|
||||
}
|
||||
}
|
||||
|
||||
func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
|
||||
active.remove(ObjectIdentifier(urlSchemeTask))
|
||||
}
|
||||
|
||||
/// Answers once, and never after WebKit stopped the task: that would throw.
|
||||
private func respond(_ task: WKURLSchemeTask, id: ObjectIdentifier, url: URL, status: Int, type: String, data: Data) {
|
||||
guard active.remove(id) != nil else { return }
|
||||
let headers = [
|
||||
"Content-Type": type,
|
||||
"Content-Length": String(data.count),
|
||||
// The page's origin is the user's server; WebGL reads tiles cross-origin.
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Cache-Control": "no-store",
|
||||
]
|
||||
guard let response = HTTPURLResponse(url: url, statusCode: status, httpVersion: "HTTP/1.1", headerFields: headers) else {
|
||||
task.didFailWithError(URLError(.badServerResponse))
|
||||
return
|
||||
}
|
||||
task.didReceive(response)
|
||||
task.didReceive(data)
|
||||
task.didFinish()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ public class TrekShellPlugin: CAPPlugin, CAPBridgedPlugin, ASWebAuthenticationPr
|
||||
CAPPluginMethod(name: "refreshOfflineCopy", returnType: CAPPluginReturnPromise),
|
||||
CAPPluginMethod(name: "openOfflineCopy", returnType: CAPPluginReturnPromise),
|
||||
CAPPluginMethod(name: "getCapabilities", returnType: CAPPluginReturnPromise),
|
||||
CAPPluginMethod(name: "getTileCacheUsage", returnType: CAPPluginReturnPromise),
|
||||
CAPPluginMethod(name: "clearTileCache", returnType: CAPPluginReturnPromise),
|
||||
]
|
||||
|
||||
/// What this binary offers the server's UI, raised with every native feature
|
||||
@@ -24,7 +26,20 @@ public class TrekShellPlugin: CAPPlugin, CAPBridgedPlugin, ASWebAuthenticationPr
|
||||
/// and a server can require a minimum (minNativeShellApi in /api/health/features).
|
||||
static let shellApi = 2
|
||||
/// The native features by name, so the UI can turn each on by itself.
|
||||
static let features: [String] = ["capabilities", "edgeBack"]
|
||||
static let features: [String] = ["capabilities", "edgeBack", "tileCache"]
|
||||
|
||||
// MARK: - Map tile store
|
||||
|
||||
/// How much the stored map tiles take on disk, for the offline settings.
|
||||
@objc func getTileCacheUsage(_ call: CAPPluginCall) {
|
||||
TrekTileStore.shared.usage { bytes in
|
||||
call.resolve(["bytes": bytes, "limit": TrekTileStore.maxBytes])
|
||||
}
|
||||
}
|
||||
|
||||
@objc func clearTileCache(_ call: CAPPluginCall) {
|
||||
TrekTileStore.shared.clear { call.resolve() }
|
||||
}
|
||||
|
||||
@objc func getCapabilities(_ call: CAPPluginCall) {
|
||||
let info = Bundle.main.infoDictionary
|
||||
|
||||
Reference in New Issue
Block a user