Repository Public
1import CoreImage2import CoreImage.CIFilterBuiltins3import Foundation4import LocalAuthentication5import MCCCore6import UIKit7import UserNotifications89/// Pure helpers of the settings screens.10enum SettingsLogic {11 /// Admin panes the session may open (hint only: the server enforces every request).12 static func adminPanes(can: (String) -> Bool) -> [SettingsPane] {13 SettingsPane.admin.filter { pane in pane.permission.map(can) ?? true }14 }1516 static let graceOptions: [TimeInterval] = [0, 60, 300]17 static func graceLabel(_ seconds: TimeInterval) -> String {18 seconds <= 0 ? tr("settings.lock.grace.immediately") : tr("settings.lock.grace.after", Format.duration(seconds: seconds))19 }2021 static let invitationTTLs = [15, 60, 240, 1440]22 static func ttlLabel(minutes: Int) -> String { Format.duration(seconds: Double(minutes) * 60) }2324 static func biometryLabel(_ type: LABiometryType) -> String {25 switch type {26 case .faceID: "Face ID"27 case .touchID: "Touch ID"28 case .opticID: "Optic ID"29 default: tr("settings.lock.passcode")30 }31 }32 static func biometrySymbol(_ type: LABiometryType) -> String {33 switch type {34 case .faceID: "faceid"35 case .touchID: "touchid"36 case .opticID: "opticid"37 default: "lock"38 }39 }4041 static func authorizationKey(_ status: UNAuthorizationStatus) -> String {42 switch status {43 case .authorized: "authorized"44 case .provisional: "provisional"45 case .ephemeral: "ephemeral"46 case .denied: "denied"47 case .notDetermined: "not_determined"48 @unknown default: "not_determined"49 }50 }51 static var authorizationKeys: [String] {52 ["authorized", "provisional", "ephemeral", "denied", "not_determined"].map { "settings.notifications.status." + $0 }53 }5455 static func streamKey(_ state: StreamState) -> String {56 switch state {57 case .idle: "idle"58 case .connecting: "connecting"59 case .connected: "connected"60 case .reconnecting: "reconnecting"61 case .paused: "paused"62 case .stopped: "stopped"63 }64 }65 static var streamKeys: [String] { ["idle", "connecting", "connected", "reconnecting", "paused", "stopped"].map { "settings.about.stream." + $0 } }66 static func streamTone(_ state: StreamState) -> StatusTone {67 switch state {68 case .connected: .ok69 case .connecting, .reconnecting: .warning70 case .paused, .idle: .neutral71 case .stopped: .offline72 }73 }7475 static var componentKeys: [String] { ["ok", "degraded", "down", "failed", "unknown"].map { "settings.system.status." + $0 } }76 static func componentTone(_ raw: String) -> StatusTone {77 switch raw {78 case "ok": .ok79 case "degraded": .warning80 case "down", "failed": .critical81 default: .neutral82 }83 }8485 static var tokenStatusKeys: [String] { API.EnrollmentToken.StatusPayload.allCases.map { "settings.agents.token_status." + $0.rawValue } }86 static func tokenTone(_ s: API.EnrollmentToken.StatusPayload) -> StatusTone {87 switch s {88 case .pending: .info89 case .consumed: .ok90 case .expired: .neutral91 case .revoked: .offline92 }93 }9495 /// Every key built at runtime by the settings and maintenance screens (verified by a unit test).96 static var dynamicKeys: [String] {97 authorizationKeys + streamKeys + componentKeys + tokenStatusKeys + MaintenanceScope.dynamicKeys + NoteDraft.Target.dynamicKeys98 + ["titleMissing", "endNotAfterStart", "untilBeforeStart"].map { "maintenance.problem." + $0 }99 }100101 /// Plain-text diagnostics for a support request. No token, no identifier of a credential.102 struct Diagnostics: Equatable, Sendable {103 var appVersion: String104 var build: String105 var system: String106 var model: String107 var origin: String108 var serverVersion: String?109 var apiVersion: String?110 var stream: String111 var offline: Bool112 var lastEventAt: Date?113 var language: String114 var userHandle: String?115 var role: String?116117 var text: String {118 var lines = [119 "MacLustr CC \(appVersion) (\(build))",120 "System: \(system) · \(model)",121 "Server: \(origin)",122 "Server version: \(serverVersion ?? "unknown") · API \(apiVersion ?? "unknown")",123 "Event stream: \(stream)",124 "Offline: \(offline ? "yes" : "no")",125 "Last event: \(lastEventAt.map { Format.utc($0) } ?? "none")",126 "Language: \(language)",127 ]128 if let userHandle { lines.append("User: \(userHandle) (\(role ?? "?"))") }129 return lines.joined(separator: "\n")130 }131 }132133 /// QR code of a one-time link, black on white with a quiet zone (maximum contrast, whatever the theme).134 @MainActor135 static func qrCode(for text: String, side: CGFloat = 480) -> UIImage? {136 let filter = CIFilter.qrCodeGenerator()137 filter.message = Data(text.utf8)138 filter.correctionLevel = "M"139 guard let output = filter.outputImage, output.extent.width > 0 else { return nil }140 let scale = max(1, (side / output.extent.width).rounded(.down))141 let scaled = output.transformed(by: CGAffineTransform(scaleX: scale, y: scale))142 guard let cg = CIContext().createCGImage(scaled, from: scaled.extent) else { return nil }143 return UIImage(cgImage: cg)144 }145}146147/// A destructive or sensitive operation waiting for the user's explicit confirmation.148struct PendingConfirmation: Identifiable {149 let id = UUID()150 var title: String151 var message: String152 var confirmTitle: String153 var destructive = true154 var identifier = "confirm-action"155 var action: @MainActor () async throws -> Void156}157