Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

11.7 KB
Raw Blame
import Foundation
import Security

/// What the app last told the widgets (native/widgets.ts writes it through
/// TrekShell.setWidgetSnapshot into the keychain group the app and its
/// extensions share). Texts arrive already translated, so the widget needs no
/// localisation of its own and always speaks the language the app does.
///
/// Everything past `trip`, `today` and `labels` is optional: a binary that is
/// newer than the server's UI shows what it gets, an older one ignores the rest.
struct WidgetSnapshot: Codable {
    struct Trip: Codable {
        var title: String
        var start: String
        var end: String
        var path: String
        /// The destination in a few words ("Tokyo, Japan").
        var place: String?
        /// The trip's cover, a small JPEG as base64, already cropped for a widget.
        var cover: String?
        var dayCount: Int?
        var placeCount: Int?
        var memberCount: Int?
    }

    struct Stop: Codable, Hashable {
        var time: String?
        var name: String
        /// Minutes after midnight, for the now line.
        var minutes: Int?
        /// The category colour as #rrggbb.
        var color: String?
        /// An SF Symbol name for the stop's kind.
        var symbol: String?
    }

    struct Today: Codable {
        /// The calendar day it is, yyyy-MM-dd (absent from an app that predates `days`).
        var date: String?
        var dayLabel: String
        var stops: [Stop]
        /// 1-based position of today in the trip.
        var index: Int?
        var total: Int?
    }

    struct Weather: Codable {
        var temp: Int
        var high: Int?
        var low: Int?
        /// The provider's main condition ("Clear", "Clouds", "Rain", ...).
        var main: String
        var label: String
        var place: String?
        /// Chance of rain in percent.
        var rain: Int?
    }

    struct Booking: Codable, Hashable {
        var title: String
        /// flight, train, bus, car, cruise, hotel, restaurant, event, tour, other
        var kind: String
        /// yyyy-MM-dd, for the relative day.
        var date: String?
        var time: String?
        /// "FRA → HND", the location or the hotel's nights.
        var detail: String?
        var confirmed: Bool
        var path: String
    }

    struct Todo: Codable, Hashable {
        var name: String
        var due: String?
        /// 0 none, 1 low, 2 medium, 3 high.
        var priority: Int
    }

    struct Todos: Codable {
        var open: Int
        var total: Int
        var items: [Todo]
        var path: String
    }

    struct Packing: Codable {
        var packed: Int
        var total: Int
        var path: String
        var bags: [Bag]?
        var missing: [Item]?

        struct Bag: Codable, Hashable {
            var name: String
            /// #rrggbb
            var color: String
            var grams: Int
            /// The bag's weight limit in grams, if it has one.
            var limit: Int?
            var packed: Int
            var total: Int
        }

        struct Item: Codable, Hashable {
            var name: String
            var category: String?
            /// The colour of the bag it goes in.
            var color: String?
        }
    }

    /// The countries of the Atlas, independent of any trip.
    struct Atlas: Codable {
        var count: Int
        /// ISO 3166-1 alpha-2 codes.
        var codes: [String]
        var recent: [Country]
        var continents: [Continent]
        var trips: Int?
        var days: Int?

        struct Country: Codable, Hashable {
            var code: String
            var name: String
        }

        struct Continent: Codable, Hashable {
            var name: String
            var count: Int
        }
    }

    /// Who travels along, the trip chat and its open poll.
    struct Collab: Codable {
        var members: [Member]
        var messages: [Message]
        var poll: Poll?
        var path: String

        struct Member: Codable, Hashable {
            var name: String
            var initials: String
        }

        struct Message: Codable, Hashable {
            var author: String
            var initials: String
            var text: String
            var at: String?
            var mine: Bool
        }

        struct Poll: Codable {
            var question: String
            var options: [Option]
            var voters: Int

            struct Option: Codable, Hashable {
                var label: String
                var votes: Int
            }
        }
    }

    /// The trip's next flight or the one in the air, for the flight widget.
    /// Times are epoch seconds, so the widget moves its countdown and its
    /// aircraft on with the clock between two snapshots; the clock times at
    /// either airport come formatted, the widget has no zone data.
    struct Flight: Codable {
        var number: String
        var from: String
        var to: String
        var fromCity: String?
        var toCity: String?
        var dep: Double
        var arr: Double
        /// Observed take-off.
        var depActual: Double?
        /// Before take-off: the planned departure plus the delay seen so far.
        var depExpected: Double?
        /// The feed saw the aircraft in the air when the app last looked.
        var airborne: Bool?
        /// Touchdown once landed, else the estimate while in the air.
        var arrExpected: Double?
        var landed: Bool?
        var depTime: String
        var arrTime: String
        var arrTimeExpected: String?
        /// Minutes late against the schedule, negative when early.
        var delay: Int?
        var aircraft: String?
        /// When the feed last heard the aircraft.
        var seen: Double?
        var path: String
    }

    struct Labels: Codable {
        var nextTrip: String
        var today: String
        var noTrip: String
        /// Contains "{n}".
        var inDays: String
        var tomorrow: String
        var nothingPlanned: String
        var days: String?
        var dayOf: String?
        var bookings: String?
        var noBookings: String?
        var todos: String?
        var noTodos: String?
        var packing: String?
        var packed: String?
        var confirmed: String?
        var pending: String?
        var todayShort: String?
        var tomorrowShort: String?
        var now: String?
        var places: String?
        var travellers: String?
        var allDone: String?
        var openTrek: String?
        var missing: String?
        var countries: String?
        var world: String?
        var group: String?
        var poll: String?
        var votes: String?
        var noMessages: String?
        var departure: String?
        var flight: String?
        /// Contains "{time}", where the widget puts its running countdown.
        var flightDeparts: String?
        var flightInAir: String?
        /// Contains "{time}".
        var flightLands: String?
        var flightLanded: String?
        var flightOnTime: String?
        /// Contains "{m}".
        var flightLate: String?
        /// Contains "{m}".
        var flightEarly: String?
        var flightEstimated: String?
        /// Past the departure time with the aircraft not yet in the air.
        var flightReady: String?
        var noFlight: String?
    }

    var trip: Trip?
    var today: Today?
    /// The trip's days from the day the app wrote this on, each with its date,
    /// so the widget turns to the next day itself (see `on(_:)`).
    var days: [Today]?
    var weather: Weather?
    var bookings: [Booking]?
    var todos: Todos?
    var packing: Packing?
    var atlas: Atlas?
    var collab: Collab?
    var flight: Flight?
    var labels: Labels

    /// The dashboard's snapshot.
    static func load() -> WidgetSnapshot? {
        read("widget.snapshot").flatMap { try? JSONDecoder().decode(WidgetSnapshot.self, from: $0) }
    }

    /// The snapshot of the trip a widget is set to, the dashboard's for none or
    /// "auto", and the dashboard's too while the app has not written that trip yet.
    static func load(trip id: String?) -> WidgetSnapshot? {
        guard let id, id != "auto", let data = read("widget.trip.\(id)"),
              var snapshot = try? JSONDecoder().decode(WidgetSnapshot.self, from: data) else { return load() }
        if snapshot.atlas == nil { snapshot.atlas = load()?.atlas }
        return snapshot
    }

    /// A trip a widget can be set to, as the app lists them.
    struct TripRef: Codable {
        var id: Int
        var title: String
        var start: String
        var end: String
    }

    static func tripIndex() -> [TripRef] {
        read("widget.trips").flatMap { try? JSONDecoder().decode([TripRef].self, from: $0) } ?? []
    }

    private static func read(_ account: String) -> Data? {
        let query: [String: Any] = [
            kSecClass as String: kSecClassGenericPassword,
            kSecAttrService as String: "com.liketrek.trek.push",
            kSecAttrAccount as String: account,
            kSecReturnData as String: true,
        ]
        var result: CFTypeRef?
        guard SecItemCopyMatching(query as CFDictionary, &result) == errSecSuccess else { return nil }
        return result as? Data
    }

    /// Whole days from today to the trip's start, in the device's calendar.
    func daysUntilStart(from now: Date = Date()) -> Int? {
        guard let trip, let start = WidgetSnapshot.day(trip.start) else { return nil }
        return WidgetSnapshot.daysBetween(now, start)
    }

    /// The trip's length in days, from its dates when the app sent no count.
    var tripLength: Int? {
        if let count = trip?.dayCount, count > 0 { return count }
        guard let trip, let start = WidgetSnapshot.day(trip.start), let end = WidgetSnapshot.day(trip.end),
              let days = WidgetSnapshot.daysBetween(start, end) else { return nil }
        return days + 1
    }

    /// The snapshot as it reads on `date`: that date's trip day out of `days`,
    /// none before or after the trip, and only the bookings from that date on.
    /// The app writes its snapshot only while it runs, so without this a widget
    /// showed the day it last ran (TestFlight feedback: "stuck on day 1"). A
    /// snapshot from an app without `days` reads as it was written.
    func on(_ date: Date) -> WidgetSnapshot {
        guard let days, !days.isEmpty else { return self }
        let key = WidgetSnapshot.key(date)
        var snapshot = self
        if today?.date != key { snapshot.today = days.first { $0.date == key } }
        snapshot.bookings = bookings?.filter { ($0.date ?? key) >= key }
        return snapshot
    }

    /// A date as the app writes one, yyyy-MM-dd in the device's calendar day.
    static func key(_ date: Date) -> String {
        var calendar = Calendar(identifier: .gregorian)
        calendar.timeZone = .current
        let parts = calendar.dateComponents([.year, .month, .day], from: date)
        return String(format: "%04d-%02d-%02d", parts.year ?? 0, parts.month ?? 0, parts.day ?? 0)
    }

    static func daysBetween(_ from: Date, _ to: Date) -> Int? {
        let calendar = Calendar.current
        return calendar.dateComponents([.day], from: calendar.startOfDay(for: from), to: calendar.startOfDay(for: to)).day
    }

    static func day(_ iso: String) -> Date? {
        let formatter = DateFormatter()
        formatter.calendar = Calendar(identifier: .gregorian)
        formatter.locale = Locale(identifier: "en_US_POSIX")
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter.date(from: String(iso.prefix(10)))
    }

    /// The app link a tap opens: com.liketrek.trek://navigate<path>.
    static func link(_ path: String) -> URL? {
        URL(string: "com.liketrek.trek://navigate\(path.hasPrefix("/") ? path : "/" + path)")
    }
}