SPB Git

Repository Public License

Formatting.swift 5.9 KB 106 lines swift

1// ORIX Alarm2// File: App/Design/Formatting.swift3// Description: Display helpers shared by all features: times, weekdays, durations, mission and status labels.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import AlarmCore8import SwiftUI910enum Fmt {11    /// "7:05" / "07:05" following the 12/24-hour preference; `period` is "AM"/"PM" or empty.12    static func time(_ t: ClockTime, use24h: Bool?) -> (digits: String, period: String) {13        let is24 = use24h ?? !(DateFormatter.dateFormat(fromTemplate: "j", options: 0, locale: .current)?.contains("a") ?? false)14        if is24 { return (String(format: "%02d:%02d", t.hour, t.minute), "") }15        let h = t.hour % 12 == 0 ? 12 : t.hour % 1216        return (String(format: "%d:%02d", h, t.minute), t.hour < 12 ? "AM" : "PM")17    }18    static func timeString(_ t: ClockTime, use24h: Bool?) -> String {19        let r = time(t, use24h: use24h); return r.period.isEmpty ? r.digits : "\(r.digits) \(r.period)"20    }21    static func weekdaySymbol(_ day: Weekday, style: Int = 1) -> String {22        let symbols = style == 0 ? Calendar.current.veryShortStandaloneWeekdaySymbols : (style == 1 ? Calendar.current.shortStandaloneWeekdaySymbols : Calendar.current.standaloneWeekdaySymbols)23        return symbols[day.rawValue - 1]24    }25    /// "Every day", "Weekdays", "Weekends", "Mon Wed Fri", "Once".26    static func recurrence(_ days: Set<Weekday>) -> String {27        if days.isEmpty { return String(localized: "Once") }28        if days == Weekday.everyDay { return String(localized: "Every day") }29        if days == Weekday.weekdays { return String(localized: "Weekdays") }30        if days == Weekday.weekend { return String(localized: "Weekends") }31        return Weekday.ordered(firstWeekday: Calendar.current.firstWeekday).filter(days.contains).map { weekdaySymbol($0) }.joined(separator: " ")32    }33    /// "in 7 h 42 min", "in 12 min", "in 2 d 3 h".34    static func countdown(to date: Date, from now: Date) -> String {35        let total = max(0, Int(date.timeIntervalSince(now) / 60))36        let d = total / 1440, h = (total % 1440) / 60, m = total % 6037        if d > 0 { return String(localized: "in \(d) d \(h) h") }38        if h > 0 { return String(localized: "in \(h) h \(m) min") }39        return m == 0 ? String(localized: "in less than a minute") : String(localized: "in \(m) min")40    }41    static func duration(_ seconds: TimeInterval) -> String {42        let s = Int(seconds.rounded())43        if s < 60 { return String(localized: "\(s) s") }44        if s < 3600 { return s % 60 == 0 ? String(localized: "\(s / 60) min") : String(localized: "\(s / 60) min \(s % 60) s") }45        return (s % 3600) / 60 == 0 ? String(localized: "\(s / 3600) h") : String(localized: "\(s / 3600) h \((s % 3600) / 60) min")46    }47}4849extension MissionKind {50    var title: String {51        switch self {52        case .math: String(localized: "Math Blitz"); case .memoryMatrix: String(localized: "Memory Matrix"); case .qrEscape: String(localized: "QR Escape")53        case .steps: String(localized: "Step Challenge"); case .patternLock: String(localized: "Pattern Lock"); case .wordScramble: String(localized: "Word Scramble")54        case .logicGates: String(localized: "Logic Gates"); case .maze: String(localized: "Maze Escape"); case .colorSequence: String(localized: "Color Sequence")55        }56    }57    var symbol: String {58        switch self {59        case .math: "function"; case .memoryMatrix: "square.grid.3x3.fill"; case .qrEscape: "qrcode.viewfinder"; case .steps: "figure.walk"60        case .patternLock: "point.3.connected.trianglepath.dotted"; case .wordScramble: "textformat.abc"; case .logicGates: "cpu"61        case .maze: "square.grid.3x3.topleft.filled"; case .colorSequence: "circle.hexagongrid.fill"62        }63    }64    var summary: String {65        switch self {66        case .math: String(localized: "Solve arithmetic questions."); case .memoryMatrix: String(localized: "Memorise and reproduce lit cells.")67        case .qrEscape: String(localized: "Walk to a QR code you registered and scan it."); case .steps: String(localized: "Walk a number of steps.")68        case .patternLock: String(localized: "Redraw a pattern across a grid of nodes."); case .wordScramble: String(localized: "Unscramble a word.")69        case .logicGates: String(localized: "Set switches to light the output."); case .maze: String(localized: "Guide the light out of a maze.")70        case .colorSequence: String(localized: "Repeat a growing sequence of panels.")71        }72    }73    var tint: Color {74        switch self {75        case .math: Theme.sunrise; case .memoryMatrix: Theme.violet; case .qrEscape: Theme.azure; case .steps: Theme.success76        case .patternLock: Theme.gold; case .wordScramble: Theme.peach; case .logicGates: Theme.azure; case .maze: Theme.violet; case .colorSequence: Theme.ember77        }78    }79}8081extension Difficulty {82    var title: String {83        switch self {84        case .easy: String(localized: "Easy"); case .normal: String(localized: "Normal"); case .hard: String(localized: "Hard")85        case .expert: String(localized: "Expert"); case .adaptive: String(localized: "Adaptive")86        }87    }88}8990extension SystemAlarmStatus {91    var title: String {92        switch self {93        case .confirmed: String(localized: "Scheduled"); case .off: String(localized: "Off"); case .missing: String(localized: "Not scheduled")94        case .mismatched: String(localized: "Out of sync"); case .unverifiable: String(localized: "Cannot verify")95        }96    }97    var symbol: String {98        switch self {99        case .confirmed: "checkmark.seal.fill"; case .off: "moon.zzz"; case .missing, .mismatched: "exclamationmark.triangle.fill"; case .unverifiable: "questionmark.circle"100        }101    }102    var tint: Color {103        switch self { case .confirmed: Theme.success; case .off: Theme.textTertiary; case .missing, .mismatched: Theme.danger; case .unverifiable: Theme.gold }104    }105}106
Files main
Menu

Theme