Compare commits
14 Commits
25cdebd1b2
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 22fbfb9790 | |||
| d8929d3776 | |||
| 2860ab0786 | |||
| e15be9ddc4 | |||
| b6ff4e81e9 | |||
| 34838f5ae1 | |||
| b00bf670e6 | |||
| 3b73a092d1 | |||
| 89e985fd39 | |||
| 29a26da043 | |||
| 11ee893367 | |||
|
5af6d38e0f
|
|||
| ceaf4a956e | |||
|
c28e3ecc4b
|
@@ -17,10 +17,14 @@ struct ContentView: View {
|
|||||||
.tabItem { Label("BLE", systemImage: "dot.radiowaves.left.and.right") }
|
.tabItem { Label("BLE", systemImage: "dot.radiowaves.left.and.right") }
|
||||||
OrchestratorView()
|
OrchestratorView()
|
||||||
.tabItem { Label("Orchestrator", systemImage: "bolt.horizontal.circle") }
|
.tabItem { Label("Orchestrator", systemImage: "bolt.horizontal.circle") }
|
||||||
|
TodosView()
|
||||||
|
.tabItem { Label("Todos", systemImage: "checklist") }
|
||||||
NavigationStack {
|
NavigationStack {
|
||||||
StockSettingsView(store: stockSettings)
|
StockSettingsView(store: stockSettings)
|
||||||
}
|
}
|
||||||
.tabItem { Label("Stocks", systemImage: "chart.line.uptrend.xyaxis") }
|
.tabItem { Label("Stocks", systemImage: "chart.line.uptrend.xyaxis") }
|
||||||
|
SettingsView()
|
||||||
|
.tabItem { Label("Settings", systemImage: "gearshape") }
|
||||||
}
|
}
|
||||||
.onAppear { orchestrator.start() }
|
.onAppear { orchestrator.start() }
|
||||||
}
|
}
|
||||||
@@ -30,10 +34,12 @@ struct ContentView_Previews: PreviewProvider {
|
|||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
if #available(iOS 16.0, *) {
|
if #available(iOS 16.0, *) {
|
||||||
let ble = BlePeripheralManager()
|
let ble = BlePeripheralManager()
|
||||||
let orchestrator = ContextOrchestrator(ble: ble)
|
let spotifyAuth = SpotifyAuthManager()
|
||||||
|
let orchestrator = ContextOrchestrator(ble: ble, spotifyAuth: spotifyAuth)
|
||||||
ContentView()
|
ContentView()
|
||||||
.environmentObject(ble)
|
.environmentObject(ble)
|
||||||
.environmentObject(orchestrator)
|
.environmentObject(orchestrator)
|
||||||
|
.environmentObject(spotifyAuth)
|
||||||
} else {
|
} else {
|
||||||
ContentView()
|
ContentView()
|
||||||
}
|
}
|
||||||
|
|||||||
193
IrisCompanion/iris/DataSources/TFLDataSource.swift
Normal file
193
IrisCompanion/iris/DataSources/TFLDataSource.swift
Normal file
@@ -0,0 +1,193 @@
|
|||||||
|
//
|
||||||
|
// TFLDataSource.swift
|
||||||
|
// iris
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
struct TFLDataSourceConfig: Sendable {
|
||||||
|
var cacheValiditySec: Int = 120
|
||||||
|
var ttlSec: Int = 300
|
||||||
|
var maxDisruptions: Int = 3
|
||||||
|
|
||||||
|
init() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
@MainActor
|
||||||
|
final class TFLDataSource {
|
||||||
|
struct Disruption: Sendable, Equatable {
|
||||||
|
let id: String
|
||||||
|
let lineName: String
|
||||||
|
let lineId: String
|
||||||
|
let severity: Int
|
||||||
|
let severityDescription: String
|
||||||
|
let reason: String?
|
||||||
|
let isMajor: Bool
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TFLData: Sendable, Equatable {
|
||||||
|
let disruptions: [Disruption]
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Snapshot: Sendable {
|
||||||
|
let data: TFLData
|
||||||
|
let diagnostics: [String: String]
|
||||||
|
}
|
||||||
|
|
||||||
|
enum TFLError: Error, LocalizedError, Sendable {
|
||||||
|
case networkFailed(message: String, diagnostics: [String: String])
|
||||||
|
case invalidResponse(diagnostics: [String: String])
|
||||||
|
|
||||||
|
var errorDescription: String? {
|
||||||
|
switch self {
|
||||||
|
case .networkFailed(let message, _):
|
||||||
|
return message
|
||||||
|
case .invalidResponse:
|
||||||
|
return "Invalid TFL response"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Severity 4 = Planned Closure, 5 = Part Closure, 10 = Good Service
|
||||||
|
private static let ignoredSeverities: Set<Int> = [4, 5, 10]
|
||||||
|
// Severity 1 = Closed, 2 = Suspended, 3 = Part Suspended, 6 = Severe Delays
|
||||||
|
private static let majorSeverities: Set<Int> = [1, 2, 3, 6]
|
||||||
|
|
||||||
|
private let config: TFLDataSourceConfig
|
||||||
|
private var cache: (timestamp: Int, data: TFLData)?
|
||||||
|
|
||||||
|
var ttlSec: Int { config.ttlSec }
|
||||||
|
|
||||||
|
init(config: TFLDataSourceConfig = .init()) {
|
||||||
|
self.config = config
|
||||||
|
}
|
||||||
|
|
||||||
|
func dataWithDiagnostics(now: Int) async throws -> Snapshot {
|
||||||
|
var diagnostics: [String: String] = [
|
||||||
|
"now": String(now),
|
||||||
|
"cache_validity_sec": String(config.cacheValiditySec),
|
||||||
|
]
|
||||||
|
|
||||||
|
if let cache = cache, now - cache.timestamp < config.cacheValiditySec {
|
||||||
|
diagnostics["source"] = "cache"
|
||||||
|
diagnostics["cache_age_sec"] = String(now - cache.timestamp)
|
||||||
|
return Snapshot(data: cache.data, diagnostics: diagnostics)
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let url = URL(string: "https://api.tfl.gov.uk/Line/Mode/tube,elizabeth-line/Status") else {
|
||||||
|
throw TFLError.invalidResponse(diagnostics: diagnostics)
|
||||||
|
}
|
||||||
|
|
||||||
|
var request = URLRequest(url: url)
|
||||||
|
request.setValue("application/json", forHTTPHeaderField: "Accept")
|
||||||
|
|
||||||
|
let (data, response) = try await URLSession.shared.data(for: request)
|
||||||
|
|
||||||
|
if let httpResponse = response as? HTTPURLResponse {
|
||||||
|
diagnostics["http_status"] = String(httpResponse.statusCode)
|
||||||
|
guard (200..<300).contains(httpResponse.statusCode) else {
|
||||||
|
throw TFLError.networkFailed(
|
||||||
|
message: "HTTP \(httpResponse.statusCode)",
|
||||||
|
diagnostics: diagnostics
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let lines = try JSONDecoder().decode([TFLLineStatus].self, from: data)
|
||||||
|
diagnostics["source"] = "network"
|
||||||
|
diagnostics["lines_returned"] = String(lines.count)
|
||||||
|
|
||||||
|
var disruptions: [Disruption] = []
|
||||||
|
var seenLines: Set<String> = []
|
||||||
|
|
||||||
|
for line in lines {
|
||||||
|
guard !seenLines.contains(line.id) else { continue }
|
||||||
|
|
||||||
|
for status in line.lineStatuses {
|
||||||
|
guard !Self.ignoredSeverities.contains(status.statusSeverity) else { continue }
|
||||||
|
|
||||||
|
seenLines.insert(line.id)
|
||||||
|
let isMajor = Self.majorSeverities.contains(status.statusSeverity)
|
||||||
|
|
||||||
|
let disruption = Disruption(
|
||||||
|
id: "\(line.id):\(status.statusSeverity)",
|
||||||
|
lineName: line.name,
|
||||||
|
lineId: line.id,
|
||||||
|
severity: status.statusSeverity,
|
||||||
|
severityDescription: status.statusSeverityDescription,
|
||||||
|
reason: status.reason ?? status.disruption?.description,
|
||||||
|
isMajor: isMajor
|
||||||
|
)
|
||||||
|
disruptions.append(disruption)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
disruptions.sort { $0.severity < $1.severity }
|
||||||
|
|
||||||
|
let limited = Array(disruptions.prefix(config.maxDisruptions))
|
||||||
|
diagnostics["disruptions_found"] = String(disruptions.count)
|
||||||
|
diagnostics["disruptions_returned"] = String(limited.count)
|
||||||
|
|
||||||
|
let tflData = TFLData(disruptions: limited)
|
||||||
|
cache = (timestamp: now, data: tflData)
|
||||||
|
|
||||||
|
return Snapshot(data: tflData, diagnostics: diagnostics)
|
||||||
|
}
|
||||||
|
|
||||||
|
func disruptionTitle(_ disruption: Disruption) -> String {
|
||||||
|
var name = disruption.lineName
|
||||||
|
name = name.replacingOccurrences(of: " & City", with: "")
|
||||||
|
name = name.replacingOccurrences(of: "Hammersmith", with: "H'smith")
|
||||||
|
name = name.replacingOccurrences(of: "Metropolitan", with: "Met")
|
||||||
|
name = name.replacingOccurrences(of: "Waterloo", with: "W'loo")
|
||||||
|
name = name.replacingOccurrences(of: "Elizabeth line", with: "Eliz.")
|
||||||
|
|
||||||
|
var severity = disruption.severityDescription
|
||||||
|
severity = severity.replacingOccurrences(of: "Minor Delays", with: "Delays")
|
||||||
|
severity = severity.replacingOccurrences(of: "Severe Delays", with: "Severe")
|
||||||
|
severity = severity.replacingOccurrences(of: "Part Closure", with: "Part Closed")
|
||||||
|
severity = severity.replacingOccurrences(of: "Part Suspended", with: "Part Susp.")
|
||||||
|
|
||||||
|
return "\(name): \(severity)"
|
||||||
|
}
|
||||||
|
|
||||||
|
func disruptionSubtitle(_ disruption: Disruption) -> String {
|
||||||
|
guard let reason = disruption.reason else {
|
||||||
|
return "Check TFL for details"
|
||||||
|
}
|
||||||
|
let phrases = reason.components(separatedBy: ".")
|
||||||
|
if let first = phrases.first?.trimmingCharacters(in: .whitespacesAndNewlines), !first.isEmpty {
|
||||||
|
return first
|
||||||
|
}
|
||||||
|
return "Check TFL for details"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - TFL API Response Models
|
||||||
|
|
||||||
|
private struct TFLLineStatus: Codable {
|
||||||
|
let id: String
|
||||||
|
let name: String
|
||||||
|
let modeName: String
|
||||||
|
let lineStatuses: [LineStatus]
|
||||||
|
|
||||||
|
struct LineStatus: Codable {
|
||||||
|
let statusSeverity: Int
|
||||||
|
let statusSeverityDescription: String
|
||||||
|
let reason: String?
|
||||||
|
let validityPeriods: [ValidityPeriod]?
|
||||||
|
let disruption: Disruption?
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ValidityPeriod: Codable {
|
||||||
|
let fromDate: String?
|
||||||
|
let toDate: String?
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Disruption: Codable {
|
||||||
|
let category: String?
|
||||||
|
let description: String?
|
||||||
|
let closureText: String?
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,17 @@
|
|||||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
<plist version="1.0">
|
<plist version="1.0">
|
||||||
<dict>
|
<dict>
|
||||||
|
<key>CFBundleURLTypes</key>
|
||||||
|
<array>
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleURLName</key>
|
||||||
|
<string>sh.nym.iris.spotify-auth</string>
|
||||||
|
<key>CFBundleURLSchemes</key>
|
||||||
|
<array>
|
||||||
|
<string>iris-spotify-auth</string>
|
||||||
|
</array>
|
||||||
|
</dict>
|
||||||
|
</array>
|
||||||
<key>MKDirectionsApplicationSupportedModes</key>
|
<key>MKDirectionsApplicationSupportedModes</key>
|
||||||
<array/>
|
<array/>
|
||||||
<key>NSCalendarsFullAccessUsageDescription</key>
|
<key>NSCalendarsFullAccessUsageDescription</key>
|
||||||
|
|||||||
@@ -86,6 +86,7 @@ final class HeuristicRanker {
|
|||||||
switch type {
|
switch type {
|
||||||
case .weatherWarning: return 1.0
|
case .weatherWarning: return 1.0
|
||||||
case .weatherAlert: return 0.9
|
case .weatherAlert: return 0.9
|
||||||
|
case .transitAlert: return 0.85
|
||||||
case .calendarEvent: return 0.8
|
case .calendarEvent: return 0.8
|
||||||
case .transit: return 0.75
|
case .transit: return 0.75
|
||||||
case .poiNearby: return 0.6
|
case .poiNearby: return 0.6
|
||||||
|
|||||||
31
IrisCompanion/iris/Models/TodoItem.swift
Normal file
31
IrisCompanion/iris/Models/TodoItem.swift
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
//
|
||||||
|
// TodoItem.swift
|
||||||
|
// iris
|
||||||
|
//
|
||||||
|
// Created by Codex.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
struct TodoItem: Identifiable, Codable, Equatable {
|
||||||
|
let id: UUID
|
||||||
|
var title: String
|
||||||
|
var isCompleted: Bool
|
||||||
|
var createdAt: Date
|
||||||
|
var updatedAt: Date
|
||||||
|
var completedAt: Date?
|
||||||
|
|
||||||
|
init(id: UUID = UUID(),
|
||||||
|
title: String,
|
||||||
|
isCompleted: Bool = false,
|
||||||
|
createdAt: Date = Date(),
|
||||||
|
updatedAt: Date = Date(),
|
||||||
|
completedAt: Date? = nil) {
|
||||||
|
self.id = id
|
||||||
|
self.title = title
|
||||||
|
self.isCompleted = isCompleted
|
||||||
|
self.createdAt = createdAt
|
||||||
|
self.updatedAt = updatedAt
|
||||||
|
self.completedAt = completedAt
|
||||||
|
}
|
||||||
|
}
|
||||||
116
IrisCompanion/iris/Models/TodoStore.swift
Normal file
116
IrisCompanion/iris/Models/TodoStore.swift
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
//
|
||||||
|
// TodoStore.swift
|
||||||
|
// iris
|
||||||
|
//
|
||||||
|
// Created by Codex.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
final class TodoStore {
|
||||||
|
private struct Persisted: Codable {
|
||||||
|
var schema: Int?
|
||||||
|
var items: [TodoItem]
|
||||||
|
}
|
||||||
|
|
||||||
|
private let queue = DispatchQueue(label: "iris.todostore.queue")
|
||||||
|
private let fileURL: URL
|
||||||
|
private var items: [TodoItem]
|
||||||
|
|
||||||
|
init(filename: String = "todos_v1.json") {
|
||||||
|
self.fileURL = Self.defaultFileURL(filename: filename)
|
||||||
|
let persisted = Self.load(from: fileURL)
|
||||||
|
self.items = persisted?.items ?? []
|
||||||
|
}
|
||||||
|
|
||||||
|
func snapshot() -> [TodoItem] {
|
||||||
|
queue.sync { items }
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
func add(title: String, now: Date = Date()) -> TodoItem {
|
||||||
|
queue.sync {
|
||||||
|
let item = TodoItem(title: title, createdAt: now, updatedAt: now)
|
||||||
|
items.append(item)
|
||||||
|
save()
|
||||||
|
return item
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
func updateTitle(id: UUID, title: String, now: Date = Date()) -> TodoItem? {
|
||||||
|
queue.sync {
|
||||||
|
guard let index = items.firstIndex(where: { $0.id == id }) else { return nil }
|
||||||
|
var item = items[index]
|
||||||
|
item.title = title
|
||||||
|
item.updatedAt = now
|
||||||
|
items[index] = item
|
||||||
|
save()
|
||||||
|
return item
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
func setCompleted(id: UUID, completed: Bool, now: Date = Date()) -> TodoItem? {
|
||||||
|
queue.sync {
|
||||||
|
guard let index = items.firstIndex(where: { $0.id == id }) else { return nil }
|
||||||
|
var item = items[index]
|
||||||
|
item.isCompleted = completed
|
||||||
|
item.updatedAt = now
|
||||||
|
item.completedAt = completed ? now : nil
|
||||||
|
items[index] = item
|
||||||
|
save()
|
||||||
|
return item
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func delete(id: UUID) {
|
||||||
|
queue.sync {
|
||||||
|
items.removeAll { $0.id == id }
|
||||||
|
save()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func save() {
|
||||||
|
let persisted = Persisted(schema: 1, items: items)
|
||||||
|
Self.save(persisted, to: fileURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func defaultFileURL(filename: String) -> URL {
|
||||||
|
let fm = FileManager.default
|
||||||
|
let base = fm.urls(for: .applicationSupportDirectory, in: .userDomainMask).first ?? fm.temporaryDirectory
|
||||||
|
let bundle = Bundle.main.bundleIdentifier ?? "iris"
|
||||||
|
return base
|
||||||
|
.appendingPathComponent(bundle, isDirectory: true)
|
||||||
|
.appendingPathComponent(filename, isDirectory: false)
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func load(from url: URL) -> Persisted? {
|
||||||
|
guard let data = try? Data(contentsOf: url) else { return nil }
|
||||||
|
return try? makeDecoder().decode(Persisted.self, from: data)
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func save(_ persisted: Persisted, to url: URL) {
|
||||||
|
do {
|
||||||
|
let fm = FileManager.default
|
||||||
|
try fm.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
|
||||||
|
let data = try makeEncoder().encode(persisted)
|
||||||
|
try data.write(to: url, options: [.atomic])
|
||||||
|
} catch {
|
||||||
|
// Best-effort persistence.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func makeEncoder() -> JSONEncoder {
|
||||||
|
let encoder = JSONEncoder()
|
||||||
|
encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes]
|
||||||
|
encoder.dateEncodingStrategy = .iso8601
|
||||||
|
return encoder
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func makeDecoder() -> JSONDecoder {
|
||||||
|
let decoder = JSONDecoder()
|
||||||
|
decoder.dateDecodingStrategy = .iso8601
|
||||||
|
return decoder
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,5 +17,6 @@ enum FeedItemType: String, Codable, CaseIterable {
|
|||||||
case currentWeather = "CURRENT_WEATHER"
|
case currentWeather = "CURRENT_WEATHER"
|
||||||
case calendarEvent = "CALENDAR_EVENT"
|
case calendarEvent = "CALENDAR_EVENT"
|
||||||
case stock = "STOCK"
|
case stock = "STOCK"
|
||||||
|
case transitAlert = "TRANSIT_ALERT"
|
||||||
case allQuiet = "ALL_QUIET"
|
case allQuiet = "ALL_QUIET"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,8 +24,18 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
|||||||
@Published private(set) var lastCalendarDiagnostics: [String: String] = [:]
|
@Published private(set) var lastCalendarDiagnostics: [String: String] = [:]
|
||||||
@Published private(set) var lastPipelineElapsedMs: Int? = nil
|
@Published private(set) var lastPipelineElapsedMs: Int? = nil
|
||||||
@Published private(set) var lastFetchFailed: Bool = false
|
@Published private(set) var lastFetchFailed: Bool = false
|
||||||
|
@Published private(set) var lastTFLDiagnostics: [String: String] = [:]
|
||||||
@Published private(set) var musicAuthorization: MusicAuthorization.Status = .notDetermined
|
@Published private(set) var musicAuthorization: MusicAuthorization.Status = .notDetermined
|
||||||
@Published private(set) var nowPlaying: NowPlayingSnapshot? = nil
|
@Published private(set) var nowPlaying: NowPlayingSnapshot? = nil
|
||||||
|
@Published private(set) var spotifyNowPlaying: SpotifyNowPlaying? = nil
|
||||||
|
@Published var musicSource: MusicSource = .appleMusic {
|
||||||
|
didSet {
|
||||||
|
if oldValue != musicSource {
|
||||||
|
switchMusicMonitor()
|
||||||
|
UserDefaults.standard.set(musicSource.rawValue, forKey: "music_source")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "iris", category: "ContextOrchestrator")
|
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "iris", category: "ContextOrchestrator")
|
||||||
|
|
||||||
@@ -34,11 +44,14 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
|||||||
private let calendarDataSource = CalendarDataSource()
|
private let calendarDataSource = CalendarDataSource()
|
||||||
private let poiDataSource = POIDataSource()
|
private let poiDataSource = POIDataSource()
|
||||||
private let stockDataSource = StockDataSource()
|
private let stockDataSource = StockDataSource()
|
||||||
|
private let tflDataSource = TFLDataSource()
|
||||||
private let ranker: HeuristicRanker
|
private let ranker: HeuristicRanker
|
||||||
private let store: FeedStore
|
private let store: FeedStore
|
||||||
private let server: LocalServer
|
private let server: LocalServer
|
||||||
private let ble: BlePeripheralManager
|
private let ble: BlePeripheralManager
|
||||||
private let nowPlayingMonitor = NowPlayingMonitor()
|
private let nowPlayingMonitor = NowPlayingMonitor()
|
||||||
|
private let spotifyMonitor: SpotifyNowPlayingMonitor
|
||||||
|
private let spotifyAuth: SpotifyAuthManager
|
||||||
|
|
||||||
private var lastRecomputeLocation: CLLocation? = nil
|
private var lastRecomputeLocation: CLLocation? = nil
|
||||||
private var lastRecomputeAccuracy: CLLocationAccuracy? = nil
|
private var lastRecomputeAccuracy: CLLocationAccuracy? = nil
|
||||||
@@ -47,11 +60,20 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
|||||||
|
|
||||||
init(store: FeedStore = FeedStore(),
|
init(store: FeedStore = FeedStore(),
|
||||||
server: LocalServer = LocalServer(),
|
server: LocalServer = LocalServer(),
|
||||||
ble: BlePeripheralManager) {
|
ble: BlePeripheralManager,
|
||||||
|
spotifyAuth: SpotifyAuthManager) {
|
||||||
self.store = store
|
self.store = store
|
||||||
self.server = server
|
self.server = server
|
||||||
self.ble = ble
|
self.ble = ble
|
||||||
|
self.spotifyAuth = spotifyAuth
|
||||||
|
self.spotifyMonitor = SpotifyNowPlayingMonitor(authManager: spotifyAuth)
|
||||||
self.ranker = HeuristicRanker(lastShownAt: { id in store.lastShownAt(feedItemId: id) })
|
self.ranker = HeuristicRanker(lastShownAt: { id in store.lastShownAt(feedItemId: id) })
|
||||||
|
|
||||||
|
if let savedSource = UserDefaults.standard.string(forKey: "music_source"),
|
||||||
|
let source = MusicSource(rawValue: savedSource) {
|
||||||
|
self.musicSource = source
|
||||||
|
}
|
||||||
|
|
||||||
super.init()
|
super.init()
|
||||||
|
|
||||||
locationManager.delegate = self
|
locationManager.delegate = self
|
||||||
@@ -74,13 +96,21 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
|||||||
|
|
||||||
nowPlayingMonitor.onUpdate = { [weak self] update in
|
nowPlayingMonitor.onUpdate = { [weak self] update in
|
||||||
Task { @MainActor in
|
Task { @MainActor in
|
||||||
guard let self else { return }
|
guard let self, self.musicSource == .appleMusic else { return }
|
||||||
self.musicAuthorization = update.authorization
|
self.musicAuthorization = update.authorization
|
||||||
self.nowPlaying = update.snapshot
|
self.nowPlaying = update.snapshot
|
||||||
self.pushLatestFeedToBle()
|
self.pushLatestFeedToBle()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
spotifyMonitor.onUpdate = { [weak self] update in
|
||||||
|
Task { @MainActor in
|
||||||
|
guard let self, self.musicSource == .spotify else { return }
|
||||||
|
self.spotifyNowPlaying = update.snapshot
|
||||||
|
self.pushLatestFeedToBle()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let feed = store.getFeed()
|
let feed = store.getFeed()
|
||||||
lastFeed = feed
|
lastFeed = feed
|
||||||
}
|
}
|
||||||
@@ -89,7 +119,7 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
|||||||
authorization = locationManager.authorizationStatus
|
authorization = locationManager.authorizationStatus
|
||||||
logger.info("start auth=\(String(describing: self.authorization), privacy: .public)")
|
logger.info("start auth=\(String(describing: self.authorization), privacy: .public)")
|
||||||
server.start()
|
server.start()
|
||||||
nowPlayingMonitor.start()
|
startMusicMonitor()
|
||||||
requestPermissionsIfNeeded()
|
requestPermissionsIfNeeded()
|
||||||
locationManager.startUpdatingLocation()
|
locationManager.startUpdatingLocation()
|
||||||
}
|
}
|
||||||
@@ -97,6 +127,27 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
|||||||
func stop() {
|
func stop() {
|
||||||
locationManager.stopUpdatingLocation()
|
locationManager.stopUpdatingLocation()
|
||||||
nowPlayingMonitor.stop()
|
nowPlayingMonitor.stop()
|
||||||
|
spotifyMonitor.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
private func startMusicMonitor() {
|
||||||
|
switch musicSource {
|
||||||
|
case .appleMusic:
|
||||||
|
nowPlayingMonitor.start()
|
||||||
|
case .spotify:
|
||||||
|
spotifyMonitor.start()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func switchMusicMonitor() {
|
||||||
|
nowPlayingMonitor.stop()
|
||||||
|
spotifyMonitor.stop()
|
||||||
|
nowPlaying = nil
|
||||||
|
spotifyNowPlaying = nil
|
||||||
|
|
||||||
|
startMusicMonitor()
|
||||||
|
pushLatestFeedToBle()
|
||||||
|
logger.info("Switched music source to \(self.musicSource.rawValue)")
|
||||||
}
|
}
|
||||||
|
|
||||||
func recomputeNow(reason: String = "manual") {
|
func recomputeNow(reason: String = "manual") {
|
||||||
@@ -209,11 +260,15 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
|||||||
async let stockResult = withTimeoutResult(seconds: 6) {
|
async let stockResult = withTimeoutResult(seconds: 6) {
|
||||||
try await self.stockDataSource.dataWithDiagnostics(symbols: stockSymbols, now: nowEpoch)
|
try await self.stockDataSource.dataWithDiagnostics(symbols: stockSymbols, now: nowEpoch)
|
||||||
}
|
}
|
||||||
|
async let tflResult = withTimeoutResult(seconds: 6) {
|
||||||
|
try await self.tflDataSource.dataWithDiagnostics(now: nowEpoch)
|
||||||
|
}
|
||||||
|
|
||||||
let wxRes = await weatherResult
|
let wxRes = await weatherResult
|
||||||
let calRes = await calendarResult
|
let calRes = await calendarResult
|
||||||
let poiRes = await poiResult
|
let poiRes = await poiResult
|
||||||
let stockRes = await stockResult
|
let stockRes = await stockResult
|
||||||
|
let tflRes = await tflResult
|
||||||
|
|
||||||
func calendarTTL(endAt: Int, now: Int) -> Int {
|
func calendarTTL(endAt: Int, now: Int) -> Int {
|
||||||
let ttl = endAt - now
|
let ttl = endAt - now
|
||||||
@@ -230,6 +285,7 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
|||||||
var calendarItems: [FeedItem] = []
|
var calendarItems: [FeedItem] = []
|
||||||
var poiItems: [FeedItem] = []
|
var poiItems: [FeedItem] = []
|
||||||
var stockItems: [FeedItem] = []
|
var stockItems: [FeedItem] = []
|
||||||
|
var tflItems: [FeedItem] = []
|
||||||
var weatherNowItem: FeedItem? = nil
|
var weatherNowItem: FeedItem? = nil
|
||||||
var fetchFailed = false
|
var fetchFailed = false
|
||||||
var wxDiagnostics: [String: String] = [:]
|
var wxDiagnostics: [String: String] = [:]
|
||||||
@@ -334,6 +390,7 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
|||||||
if pois.isEmpty {
|
if pois.isEmpty {
|
||||||
logger.info("no points of interests found")
|
logger.info("no points of interests found")
|
||||||
}
|
}
|
||||||
|
// POIs are FYI-only; do not compete for the right-now winner.
|
||||||
for poi in pois.prefix(2) {
|
for poi in pois.prefix(2) {
|
||||||
let subtitle = poiSubtitle(for: poi)
|
let subtitle = poiSubtitle(for: poi)
|
||||||
let confidence = min(max(poi.confidence, 0.0), 1.0)
|
let confidence = min(max(poi.confidence, 0.0), 1.0)
|
||||||
@@ -351,7 +408,6 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
|||||||
actions: ["DISMISS"]
|
actions: ["DISMISS"]
|
||||||
)
|
)
|
||||||
poiItems.append(item)
|
poiItems.append(item)
|
||||||
rightNowCandidates.append(.init(item: item, confidence: confidence, isEligibleForRightNow: true))
|
|
||||||
}
|
}
|
||||||
case .failure(let error):
|
case .failure(let error):
|
||||||
fetchFailed = true
|
fetchFailed = true
|
||||||
@@ -405,13 +461,49 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
|||||||
logger.warning("stock fetch failed: \(String(describing: error), privacy: .public)")
|
logger.warning("stock fetch failed: \(String(describing: error), privacy: .public)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var tflDiagnostics: [String: String] = [:]
|
||||||
|
switch tflRes {
|
||||||
|
case .success(let snapshot):
|
||||||
|
tflDiagnostics = snapshot.diagnostics
|
||||||
|
for disruption in snapshot.data.disruptions {
|
||||||
|
let confidence: Double = disruption.isMajor ? 0.9 : 0.6
|
||||||
|
let item = FeedItem(
|
||||||
|
id: "tfl:\(disruption.lineId):\(nowEpoch / 300)",
|
||||||
|
type: .transitAlert,
|
||||||
|
title: tflDataSource.disruptionTitle(disruption).truncated(maxLength: TextConstraints.titleMax),
|
||||||
|
subtitle: tflDataSource.disruptionSubtitle(disruption).truncated(maxLength: TextConstraints.subtitleMax),
|
||||||
|
priority: confidence,
|
||||||
|
ttlSec: tflDataSource.ttlSec,
|
||||||
|
condition: nil,
|
||||||
|
startsAt: nil,
|
||||||
|
poiType: nil,
|
||||||
|
bucket: disruption.isMajor ? .rightNow : .fyi,
|
||||||
|
actions: ["DISMISS"]
|
||||||
|
)
|
||||||
|
tflItems.append(item)
|
||||||
|
if disruption.isMajor {
|
||||||
|
rightNowCandidates.append(.init(item: item, confidence: confidence, isEligibleForRightNow: true))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !snapshot.data.disruptions.isEmpty {
|
||||||
|
logger.info("tfl disruptions fetched count=\(snapshot.data.disruptions.count)")
|
||||||
|
}
|
||||||
|
case .failure(let error):
|
||||||
|
if case TimeoutError.timedOut = error {
|
||||||
|
logger.warning("tfl fetch timeout")
|
||||||
|
} else {
|
||||||
|
logger.warning("tfl fetch failed: \(String(describing: error), privacy: .public)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let elapsedMs = Int(Date().timeIntervalSince(start) * 1000)
|
let elapsedMs = Int(Date().timeIntervalSince(start) * 1000)
|
||||||
lastPipelineElapsedMs = elapsedMs
|
lastPipelineElapsedMs = elapsedMs
|
||||||
lastFetchFailed = fetchFailed
|
lastFetchFailed = fetchFailed
|
||||||
lastWeatherDiagnostics = wxDiagnostics
|
lastWeatherDiagnostics = wxDiagnostics
|
||||||
lastCalendarDiagnostics = calDiagnostics
|
lastCalendarDiagnostics = calDiagnostics
|
||||||
|
lastTFLDiagnostics = tflDiagnostics
|
||||||
|
|
||||||
logger.info("pipeline right_now_candidates=\(rightNowCandidates.count) calendar_items=\(calendarItems.count) poi_items=\(poiItems.count) stock_items=\(stockItems.count) fetchFailed=\(fetchFailed) elapsed_ms=\(elapsedMs)")
|
logger.info("pipeline right_now_candidates=\(rightNowCandidates.count) calendar_items=\(calendarItems.count) poi_items=\(poiItems.count) stock_items=\(stockItems.count) tfl_items=\(tflItems.count) fetchFailed=\(fetchFailed) elapsed_ms=\(elapsedMs)")
|
||||||
|
|
||||||
if fetchFailed, rightNowCandidates.isEmpty, calendarItems.isEmpty, weatherNowItem == nil {
|
if fetchFailed, rightNowCandidates.isEmpty, calendarItems.isEmpty, weatherNowItem == nil {
|
||||||
let fallbackFeed = store.getFeed(now: nowEpoch)
|
let fallbackFeed = store.getFeed(now: nowEpoch)
|
||||||
@@ -422,10 +514,17 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let eligibleUnsuppressed = rightNowCandidates.filter { ranked in
|
let poiCandidateCount = rightNowCandidates.filter { $0.item.type == .poiNearby }.count
|
||||||
!store.isSuppressed(id: ranked.item.id, type: ranked.item.type, now: nowEpoch)
|
if poiCandidateCount > 0 {
|
||||||
|
logger.warning("dropping poi candidates from right-now ranking count=\(poiCandidateCount)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let eligibleUnsuppressed = rightNowCandidates
|
||||||
|
.filter { $0.item.type != .poiNearby }
|
||||||
|
.filter { ranked in
|
||||||
|
!store.isSuppressed(id: ranked.item.id, type: ranked.item.type, now: nowEpoch)
|
||||||
|
}
|
||||||
|
|
||||||
let winnerSelection = ranker.pickWinner(from: eligibleUnsuppressed, now: nowEpoch, context: userContext)
|
let winnerSelection = ranker.pickWinner(from: eligibleUnsuppressed, now: nowEpoch, context: userContext)
|
||||||
let winnerItem = winnerSelection?.item ?? FeedEnvelope.allQuiet(now: nowEpoch).feed[0]
|
let winnerItem = winnerSelection?.item ?? FeedEnvelope.allQuiet(now: nowEpoch).feed[0]
|
||||||
|
|
||||||
@@ -500,6 +599,28 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
let fyiTFL = tflItems
|
||||||
|
.filter { $0.bucket == .fyi }
|
||||||
|
.filter { $0.id != winnerItem.id }
|
||||||
|
.filter { !store.isSuppressed(id: $0.id, type: $0.type, now: nowEpoch) }
|
||||||
|
.prefix(2)
|
||||||
|
|
||||||
|
fyi.append(contentsOf: fyiTFL.map { item in
|
||||||
|
FeedItem(
|
||||||
|
id: item.id,
|
||||||
|
type: item.type,
|
||||||
|
title: item.title.truncated(maxLength: TextConstraints.titleMax),
|
||||||
|
subtitle: item.subtitle.truncated(maxLength: TextConstraints.subtitleMax),
|
||||||
|
priority: min(max(item.priority, 0.0), 1.0),
|
||||||
|
ttlSec: max(1, item.ttlSec),
|
||||||
|
condition: item.condition,
|
||||||
|
startsAt: item.startsAt,
|
||||||
|
poiType: item.poiType,
|
||||||
|
bucket: .fyi,
|
||||||
|
actions: ["DISMISS"]
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
let items = [winnerItem] + fyi
|
let items = [winnerItem] + fyi
|
||||||
let feedEnvelope = FeedEnvelope(
|
let feedEnvelope = FeedEnvelope(
|
||||||
schema: 1,
|
schema: 1,
|
||||||
@@ -541,7 +662,15 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func feedForGlass(base: FeedEnvelope, now: Int) -> FeedEnvelope {
|
private func feedForGlass(base: FeedEnvelope, now: Int) -> FeedEnvelope {
|
||||||
guard let nowPlayingCard = nowPlaying?.asFeedItem(baseGeneratedAt: base.generatedAt, now: now) else {
|
let nowPlayingCard: FeedItem?
|
||||||
|
switch musicSource {
|
||||||
|
case .appleMusic:
|
||||||
|
nowPlayingCard = nowPlaying?.asFeedItem(baseGeneratedAt: base.generatedAt, now: now)
|
||||||
|
case .spotify:
|
||||||
|
nowPlayingCard = spotifyNowPlaying?.asFeedItem(baseGeneratedAt: base.generatedAt, now: now)
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let nowPlayingCard else {
|
||||||
return base
|
return base
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
61
IrisCompanion/iris/Spotify/KeychainHelper.swift
Normal file
61
IrisCompanion/iris/Spotify/KeychainHelper.swift
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
//
|
||||||
|
// KeychainHelper.swift
|
||||||
|
// iris
|
||||||
|
//
|
||||||
|
// Secure token storage using iOS Keychain.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import Security
|
||||||
|
|
||||||
|
enum KeychainHelper {
|
||||||
|
private static let service = "sh.nym.iris.spotify"
|
||||||
|
|
||||||
|
static func save(key: String, data: Data) throws {
|
||||||
|
let query: [String: Any] = [
|
||||||
|
kSecClass as String: kSecClassGenericPassword,
|
||||||
|
kSecAttrService as String: service,
|
||||||
|
kSecAttrAccount as String: key
|
||||||
|
]
|
||||||
|
SecItemDelete(query as CFDictionary)
|
||||||
|
|
||||||
|
let attributes: [String: Any] = [
|
||||||
|
kSecClass as String: kSecClassGenericPassword,
|
||||||
|
kSecAttrService as String: service,
|
||||||
|
kSecAttrAccount as String: key,
|
||||||
|
kSecValueData as String: data,
|
||||||
|
kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlock
|
||||||
|
]
|
||||||
|
let status = SecItemAdd(attributes as CFDictionary, nil)
|
||||||
|
guard status == errSecSuccess else {
|
||||||
|
throw KeychainError.saveFailed(status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static func load(key: String) -> Data? {
|
||||||
|
let query: [String: Any] = [
|
||||||
|
kSecClass as String: kSecClassGenericPassword,
|
||||||
|
kSecAttrService as String: service,
|
||||||
|
kSecAttrAccount as String: key,
|
||||||
|
kSecReturnData as String: true,
|
||||||
|
kSecMatchLimit as String: kSecMatchLimitOne
|
||||||
|
]
|
||||||
|
var result: AnyObject?
|
||||||
|
let status = SecItemCopyMatching(query as CFDictionary, &result)
|
||||||
|
guard status == errSecSuccess else { return nil }
|
||||||
|
return result as? Data
|
||||||
|
}
|
||||||
|
|
||||||
|
static func delete(key: String) {
|
||||||
|
let query: [String: Any] = [
|
||||||
|
kSecClass as String: kSecClassGenericPassword,
|
||||||
|
kSecAttrService as String: service,
|
||||||
|
kSecAttrAccount as String: key
|
||||||
|
]
|
||||||
|
SecItemDelete(query as CFDictionary)
|
||||||
|
}
|
||||||
|
|
||||||
|
enum KeychainError: Error {
|
||||||
|
case saveFailed(OSStatus)
|
||||||
|
}
|
||||||
|
}
|
||||||
77
IrisCompanion/iris/Spotify/SpotifyAPIClient.swift
Normal file
77
IrisCompanion/iris/Spotify/SpotifyAPIClient.swift
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
//
|
||||||
|
// SpotifyAPIClient.swift
|
||||||
|
// iris
|
||||||
|
//
|
||||||
|
// Spotify Web API client for fetching currently playing track.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import os
|
||||||
|
|
||||||
|
@available(iOS 16.0, *)
|
||||||
|
@MainActor
|
||||||
|
final class SpotifyAPIClient {
|
||||||
|
private let authManager: SpotifyAuthManager
|
||||||
|
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "iris", category: "SpotifyAPI")
|
||||||
|
private let baseUrl = "https://api.spotify.com/v1"
|
||||||
|
|
||||||
|
init(authManager: SpotifyAuthManager) {
|
||||||
|
self.authManager = authManager
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCurrentPlayback() async throws -> SpotifyPlaybackState? {
|
||||||
|
try await getCurrentPlayback(isRetry: false)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func getCurrentPlayback(isRetry: Bool) async throws -> SpotifyPlaybackState? {
|
||||||
|
let refreshed = await authManager.refreshTokenIfNeeded()
|
||||||
|
guard refreshed else {
|
||||||
|
throw SpotifyAPIError.notAuthenticated
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let token = authManager.accessToken else {
|
||||||
|
throw SpotifyAPIError.notAuthenticated
|
||||||
|
}
|
||||||
|
|
||||||
|
var request = URLRequest(url: URL(string: "\(baseUrl)/me/player/currently-playing")!)
|
||||||
|
request.httpMethod = "GET"
|
||||||
|
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
|
||||||
|
|
||||||
|
let (data, response) = try await URLSession.shared.data(for: request)
|
||||||
|
|
||||||
|
guard let httpResponse = response as? HTTPURLResponse else {
|
||||||
|
throw SpotifyAPIError.invalidResponse
|
||||||
|
}
|
||||||
|
|
||||||
|
switch httpResponse.statusCode {
|
||||||
|
case 200:
|
||||||
|
return try JSONDecoder().decode(SpotifyPlaybackState.self, from: data)
|
||||||
|
case 204:
|
||||||
|
return nil
|
||||||
|
case 401:
|
||||||
|
guard !isRetry else {
|
||||||
|
throw SpotifyAPIError.tokenExpired
|
||||||
|
}
|
||||||
|
let refreshed = await authManager.refreshTokenIfNeeded()
|
||||||
|
if refreshed {
|
||||||
|
return try await getCurrentPlayback(isRetry: true)
|
||||||
|
}
|
||||||
|
throw SpotifyAPIError.tokenExpired
|
||||||
|
case 429:
|
||||||
|
let retryAfter = httpResponse.value(forHTTPHeaderField: "Retry-After")
|
||||||
|
logger.warning("Rate limited, retry after: \(retryAfter ?? "unknown")")
|
||||||
|
throw SpotifyAPIError.rateLimited
|
||||||
|
default:
|
||||||
|
logger.error("API error: \(httpResponse.statusCode)")
|
||||||
|
throw SpotifyAPIError.apiError(httpResponse.statusCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum SpotifyAPIError: Error {
|
||||||
|
case notAuthenticated
|
||||||
|
case tokenExpired
|
||||||
|
case invalidResponse
|
||||||
|
case rateLimited
|
||||||
|
case apiError(Int)
|
||||||
|
}
|
||||||
|
}
|
||||||
276
IrisCompanion/iris/Spotify/SpotifyAuthManager.swift
Normal file
276
IrisCompanion/iris/Spotify/SpotifyAuthManager.swift
Normal file
@@ -0,0 +1,276 @@
|
|||||||
|
//
|
||||||
|
// SpotifyAuthManager.swift
|
||||||
|
// iris
|
||||||
|
//
|
||||||
|
// OAuth 2.0 PKCE authentication for Spotify Web API.
|
||||||
|
//
|
||||||
|
|
||||||
|
import AuthenticationServices
|
||||||
|
import CommonCrypto
|
||||||
|
import Foundation
|
||||||
|
import os
|
||||||
|
|
||||||
|
enum SpotifyConfig {
|
||||||
|
static let redirectUri = "iris-spotify-auth://callback"
|
||||||
|
static let scopes = "user-read-playback-state user-read-currently-playing"
|
||||||
|
static let authUrl = "https://accounts.spotify.com/authorize"
|
||||||
|
static let tokenUrl = "https://accounts.spotify.com/api/token"
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(iOS 16.0, *)
|
||||||
|
@MainActor
|
||||||
|
final class SpotifyAuthManager: NSObject, ObservableObject {
|
||||||
|
@Published private(set) var isConnected: Bool = false
|
||||||
|
@Published private(set) var isAuthenticating: Bool = false
|
||||||
|
@Published private(set) var error: String? = nil
|
||||||
|
@Published private(set) var clientId: String = ""
|
||||||
|
|
||||||
|
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "iris", category: "SpotifyAuth")
|
||||||
|
private let tokensKey = "spotify_tokens"
|
||||||
|
private let clientIdKey = "spotify_client_id"
|
||||||
|
private var codeVerifier: String? = nil
|
||||||
|
private var authSession: ASWebAuthenticationSession? = nil
|
||||||
|
|
||||||
|
var isConfigured: Bool {
|
||||||
|
!clientId.isEmpty
|
||||||
|
}
|
||||||
|
|
||||||
|
override init() {
|
||||||
|
super.init()
|
||||||
|
clientId = UserDefaults.standard.string(forKey: clientIdKey) ?? ""
|
||||||
|
loadTokens()
|
||||||
|
}
|
||||||
|
|
||||||
|
func setClientId(_ id: String) {
|
||||||
|
let trimmed = id.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
|
clientId = trimmed
|
||||||
|
UserDefaults.standard.set(trimmed, forKey: clientIdKey)
|
||||||
|
logger.info("Spotify Client ID updated")
|
||||||
|
}
|
||||||
|
|
||||||
|
var accessToken: String? {
|
||||||
|
loadStoredTokens()?.accessToken
|
||||||
|
}
|
||||||
|
|
||||||
|
func startAuth() {
|
||||||
|
guard !isAuthenticating else { return }
|
||||||
|
isAuthenticating = true
|
||||||
|
error = nil
|
||||||
|
|
||||||
|
let verifier = generateCodeVerifier()
|
||||||
|
codeVerifier = verifier
|
||||||
|
let challenge = generateCodeChallenge(verifier: verifier)
|
||||||
|
|
||||||
|
var components = URLComponents(string: SpotifyConfig.authUrl)!
|
||||||
|
components.queryItems = [
|
||||||
|
URLQueryItem(name: "client_id", value: clientId),
|
||||||
|
URLQueryItem(name: "response_type", value: "code"),
|
||||||
|
URLQueryItem(name: "redirect_uri", value: SpotifyConfig.redirectUri),
|
||||||
|
URLQueryItem(name: "scope", value: SpotifyConfig.scopes),
|
||||||
|
URLQueryItem(name: "code_challenge_method", value: "S256"),
|
||||||
|
URLQueryItem(name: "code_challenge", value: challenge)
|
||||||
|
]
|
||||||
|
|
||||||
|
guard let authUrl = components.url else {
|
||||||
|
error = "Failed to build auth URL"
|
||||||
|
isAuthenticating = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let session = ASWebAuthenticationSession(
|
||||||
|
url: authUrl,
|
||||||
|
callbackURLScheme: "iris-spotify-auth"
|
||||||
|
) { [weak self] callbackUrl, authError in
|
||||||
|
Task { @MainActor in
|
||||||
|
self?.handleAuthCallback(callbackUrl: callbackUrl, error: authError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
session.presentationContextProvider = self
|
||||||
|
session.prefersEphemeralWebBrowserSession = false
|
||||||
|
authSession = session
|
||||||
|
|
||||||
|
if !session.start() {
|
||||||
|
error = "Failed to start auth session"
|
||||||
|
isAuthenticating = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleCallback(url: URL) {
|
||||||
|
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
|
||||||
|
let code = components.queryItems?.first(where: { $0.name == "code" })?.value else {
|
||||||
|
error = "Invalid callback URL"
|
||||||
|
isAuthenticating = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
Task {
|
||||||
|
await exchangeCodeForTokens(code: code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func disconnect() {
|
||||||
|
KeychainHelper.delete(key: tokensKey)
|
||||||
|
isConnected = false
|
||||||
|
logger.info("Disconnected from Spotify")
|
||||||
|
}
|
||||||
|
|
||||||
|
func refreshTokenIfNeeded() async -> Bool {
|
||||||
|
guard let tokens = loadStoredTokens() else { return false }
|
||||||
|
|
||||||
|
if !tokens.expiresWithinMinutes {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return await refreshToken(refreshToken: tokens.refreshToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func handleAuthCallback(callbackUrl: URL?, error authError: Error?) {
|
||||||
|
isAuthenticating = false
|
||||||
|
authSession = nil
|
||||||
|
|
||||||
|
if let authError = authError as? ASWebAuthenticationSessionError,
|
||||||
|
authError.code == .canceledLogin {
|
||||||
|
logger.info("User cancelled Spotify login")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if let authError {
|
||||||
|
error = authError.localizedDescription
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let callbackUrl else {
|
||||||
|
error = "No callback URL received"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
handleCallback(url: callbackUrl)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func exchangeCodeForTokens(code: String) async {
|
||||||
|
guard let verifier = codeVerifier else {
|
||||||
|
error = "Missing code verifier"
|
||||||
|
isAuthenticating = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let body = [
|
||||||
|
"grant_type": "authorization_code",
|
||||||
|
"code": code,
|
||||||
|
"redirect_uri": SpotifyConfig.redirectUri,
|
||||||
|
"client_id": clientId,
|
||||||
|
"code_verifier": verifier
|
||||||
|
]
|
||||||
|
|
||||||
|
do {
|
||||||
|
let tokenResponse = try await postTokenRequest(body: body)
|
||||||
|
let tokens = SpotifyTokens(
|
||||||
|
accessToken: tokenResponse.accessToken,
|
||||||
|
refreshToken: tokenResponse.refreshToken ?? "",
|
||||||
|
expiresAt: Date().addingTimeInterval(TimeInterval(tokenResponse.expiresIn))
|
||||||
|
)
|
||||||
|
try saveTokens(tokens)
|
||||||
|
isConnected = true
|
||||||
|
logger.info("Successfully authenticated with Spotify")
|
||||||
|
} catch {
|
||||||
|
self.error = "Token exchange failed: \(error.localizedDescription)"
|
||||||
|
logger.error("Token exchange failed: \(error.localizedDescription)")
|
||||||
|
}
|
||||||
|
|
||||||
|
codeVerifier = nil
|
||||||
|
isAuthenticating = false
|
||||||
|
}
|
||||||
|
|
||||||
|
private func refreshToken(refreshToken: String) async -> Bool {
|
||||||
|
let body = [
|
||||||
|
"grant_type": "refresh_token",
|
||||||
|
"refresh_token": refreshToken,
|
||||||
|
"client_id": clientId
|
||||||
|
]
|
||||||
|
|
||||||
|
do {
|
||||||
|
let tokenResponse = try await postTokenRequest(body: body)
|
||||||
|
let newRefreshToken = tokenResponse.refreshToken ?? refreshToken
|
||||||
|
let tokens = SpotifyTokens(
|
||||||
|
accessToken: tokenResponse.accessToken,
|
||||||
|
refreshToken: newRefreshToken,
|
||||||
|
expiresAt: Date().addingTimeInterval(TimeInterval(tokenResponse.expiresIn))
|
||||||
|
)
|
||||||
|
try saveTokens(tokens)
|
||||||
|
logger.info("Refreshed Spotify token")
|
||||||
|
return true
|
||||||
|
} catch {
|
||||||
|
logger.error("Token refresh failed: \(error.localizedDescription)")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func postTokenRequest(body: [String: String]) async throws -> SpotifyTokenResponse {
|
||||||
|
var request = URLRequest(url: URL(string: SpotifyConfig.tokenUrl)!)
|
||||||
|
request.httpMethod = "POST"
|
||||||
|
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
||||||
|
|
||||||
|
let bodyString = body.map { "\($0.key)=\($0.value.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? $0.value)" }
|
||||||
|
.joined(separator: "&")
|
||||||
|
request.httpBody = bodyString.data(using: .utf8)
|
||||||
|
|
||||||
|
let (data, response) = try await URLSession.shared.data(for: request)
|
||||||
|
|
||||||
|
guard let httpResponse = response as? HTTPURLResponse,
|
||||||
|
httpResponse.statusCode == 200 else {
|
||||||
|
let statusCode = (response as? HTTPURLResponse)?.statusCode ?? -1
|
||||||
|
throw SpotifyAuthError.tokenRequestFailed(statusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
return try JSONDecoder().decode(SpotifyTokenResponse.self, from: data)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func loadTokens() {
|
||||||
|
isConnected = loadStoredTokens() != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
private func loadStoredTokens() -> SpotifyTokens? {
|
||||||
|
guard let data = KeychainHelper.load(key: tokensKey) else { return nil }
|
||||||
|
return try? JSONDecoder().decode(SpotifyTokens.self, from: data)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func saveTokens(_ tokens: SpotifyTokens) throws {
|
||||||
|
let data = try JSONEncoder().encode(tokens)
|
||||||
|
try KeychainHelper.save(key: tokensKey, data: data)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func generateCodeVerifier() -> String {
|
||||||
|
var bytes = [UInt8](repeating: 0, count: 32)
|
||||||
|
_ = SecRandomCopyBytes(kSecRandomDefault, bytes.count, &bytes)
|
||||||
|
return Data(bytes).base64URLEncodedString()
|
||||||
|
}
|
||||||
|
|
||||||
|
private func generateCodeChallenge(verifier: String) -> String {
|
||||||
|
guard let data = verifier.data(using: .utf8) else { return "" }
|
||||||
|
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
|
||||||
|
data.withUnsafeBytes { buffer in
|
||||||
|
_ = CC_SHA256(buffer.baseAddress, CC_LONG(data.count), &hash)
|
||||||
|
}
|
||||||
|
return Data(hash).base64URLEncodedString()
|
||||||
|
}
|
||||||
|
|
||||||
|
enum SpotifyAuthError: Error {
|
||||||
|
case tokenRequestFailed(Int)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(iOS 16.0, *)
|
||||||
|
extension SpotifyAuthManager: ASWebAuthenticationPresentationContextProviding {
|
||||||
|
nonisolated func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
|
||||||
|
ASPresentationAnchor()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Data {
|
||||||
|
func base64URLEncodedString() -> String {
|
||||||
|
base64EncodedString()
|
||||||
|
.replacingOccurrences(of: "+", with: "-")
|
||||||
|
.replacingOccurrences(of: "/", with: "_")
|
||||||
|
.replacingOccurrences(of: "=", with: "")
|
||||||
|
}
|
||||||
|
}
|
||||||
91
IrisCompanion/iris/Spotify/SpotifyModels.swift
Normal file
91
IrisCompanion/iris/Spotify/SpotifyModels.swift
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
//
|
||||||
|
// SpotifyModels.swift
|
||||||
|
// iris
|
||||||
|
//
|
||||||
|
// Codable models for Spotify Web API responses.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
struct SpotifyTokens: Codable {
|
||||||
|
let accessToken: String
|
||||||
|
let refreshToken: String
|
||||||
|
let expiresAt: Date
|
||||||
|
|
||||||
|
enum CodingKeys: String, CodingKey {
|
||||||
|
case accessToken = "access_token"
|
||||||
|
case refreshToken = "refresh_token"
|
||||||
|
case expiresAt = "expires_at"
|
||||||
|
}
|
||||||
|
|
||||||
|
var isExpired: Bool {
|
||||||
|
Date() >= expiresAt
|
||||||
|
}
|
||||||
|
|
||||||
|
var expiresWithinMinutes: Bool {
|
||||||
|
Date().addingTimeInterval(5 * 60) >= expiresAt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SpotifyTokenResponse: Codable {
|
||||||
|
let accessToken: String
|
||||||
|
let tokenType: String
|
||||||
|
let expiresIn: Int
|
||||||
|
let refreshToken: String?
|
||||||
|
let scope: String?
|
||||||
|
|
||||||
|
enum CodingKeys: String, CodingKey {
|
||||||
|
case accessToken = "access_token"
|
||||||
|
case tokenType = "token_type"
|
||||||
|
case expiresIn = "expires_in"
|
||||||
|
case refreshToken = "refresh_token"
|
||||||
|
case scope
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SpotifyPlaybackState: Codable {
|
||||||
|
let isPlaying: Bool
|
||||||
|
let progressMs: Int?
|
||||||
|
let item: SpotifyTrack?
|
||||||
|
|
||||||
|
enum CodingKeys: String, CodingKey {
|
||||||
|
case isPlaying = "is_playing"
|
||||||
|
case progressMs = "progress_ms"
|
||||||
|
case item
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SpotifyTrack: Codable {
|
||||||
|
let id: String
|
||||||
|
let name: String
|
||||||
|
let artists: [SpotifyArtist]
|
||||||
|
let album: SpotifyAlbum
|
||||||
|
let durationMs: Int
|
||||||
|
|
||||||
|
enum CodingKeys: String, CodingKey {
|
||||||
|
case id, name, artists, album
|
||||||
|
case durationMs = "duration_ms"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SpotifyArtist: Codable {
|
||||||
|
let id: String
|
||||||
|
let name: String
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SpotifyAlbum: Codable {
|
||||||
|
let id: String
|
||||||
|
let name: String
|
||||||
|
}
|
||||||
|
|
||||||
|
enum MusicSource: String, CaseIterable, Codable {
|
||||||
|
case appleMusic = "apple_music"
|
||||||
|
case spotify = "spotify"
|
||||||
|
|
||||||
|
var displayName: String {
|
||||||
|
switch self {
|
||||||
|
case .appleMusic: return "Apple Music"
|
||||||
|
case .spotify: return "Spotify"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
145
IrisCompanion/iris/Spotify/SpotifyNowPlayingMonitor.swift
Normal file
145
IrisCompanion/iris/Spotify/SpotifyNowPlayingMonitor.swift
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
//
|
||||||
|
// SpotifyNowPlayingMonitor.swift
|
||||||
|
// iris
|
||||||
|
//
|
||||||
|
// Polling monitor for Spotify currently playing track.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import os
|
||||||
|
|
||||||
|
@available(iOS 16.0, *)
|
||||||
|
struct SpotifyNowPlaying: Equatable, Sendable {
|
||||||
|
let itemId: String
|
||||||
|
let title: String
|
||||||
|
let artist: String?
|
||||||
|
let album: String?
|
||||||
|
let isPlaying: Bool
|
||||||
|
|
||||||
|
func asFeedItem(baseGeneratedAt: Int, now: Int) -> FeedItem {
|
||||||
|
let desiredLifetimeSec = 30
|
||||||
|
let ttl = max(1, (now - baseGeneratedAt) + desiredLifetimeSec)
|
||||||
|
|
||||||
|
let subtitleParts = [artist, album]
|
||||||
|
.compactMap { $0?.trimmingCharacters(in: .whitespacesAndNewlines) }
|
||||||
|
.filter { !$0.isEmpty }
|
||||||
|
let subtitle = subtitleParts.isEmpty ? "Spotify" : subtitleParts.joined(separator: " • ")
|
||||||
|
|
||||||
|
return FeedItem(
|
||||||
|
id: "spotify:now:\(itemId)",
|
||||||
|
type: .nowPlaying,
|
||||||
|
title: title.truncated(maxLength: TextConstraints.titleMax),
|
||||||
|
subtitle: subtitle.truncated(maxLength: TextConstraints.subtitleMax),
|
||||||
|
priority: isPlaying ? 0.35 : 0.2,
|
||||||
|
ttlSec: ttl,
|
||||||
|
condition: nil,
|
||||||
|
startsAt: nil,
|
||||||
|
bucket: .fyi,
|
||||||
|
actions: ["DISMISS"]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(iOS 16.0, *)
|
||||||
|
@MainActor
|
||||||
|
final class SpotifyNowPlayingMonitor {
|
||||||
|
struct Update: Sendable {
|
||||||
|
let isConnected: Bool
|
||||||
|
let snapshot: SpotifyNowPlaying?
|
||||||
|
}
|
||||||
|
|
||||||
|
var onUpdate: ((Update) -> Void)? = nil
|
||||||
|
|
||||||
|
private let authManager: SpotifyAuthManager
|
||||||
|
private let apiClient: SpotifyAPIClient
|
||||||
|
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "iris", category: "SpotifyNowPlaying")
|
||||||
|
|
||||||
|
private var pollTimer: DispatchSourceTimer?
|
||||||
|
private var isRunning = false
|
||||||
|
private var lastSnapshot: SpotifyNowPlaying? = nil
|
||||||
|
|
||||||
|
init(authManager: SpotifyAuthManager) {
|
||||||
|
self.authManager = authManager
|
||||||
|
self.apiClient = SpotifyAPIClient(authManager: authManager)
|
||||||
|
}
|
||||||
|
|
||||||
|
func start() {
|
||||||
|
guard !isRunning else { return }
|
||||||
|
isRunning = true
|
||||||
|
logger.info("Spotify monitor started")
|
||||||
|
startPolling()
|
||||||
|
|
||||||
|
Task { @MainActor in
|
||||||
|
await refresh(reason: "start")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func stop() {
|
||||||
|
guard isRunning else { return }
|
||||||
|
isRunning = false
|
||||||
|
logger.info("Spotify monitor stopped")
|
||||||
|
|
||||||
|
pollTimer?.cancel()
|
||||||
|
pollTimer = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
private func startPolling() {
|
||||||
|
guard pollTimer == nil else { return }
|
||||||
|
let timer = DispatchSource.makeTimerSource(queue: .main)
|
||||||
|
timer.schedule(deadline: .now() + 4, repeating: 4)
|
||||||
|
timer.setEventHandler { [weak self] in
|
||||||
|
guard let self else { return }
|
||||||
|
Task { @MainActor in
|
||||||
|
await self.refresh(reason: "poll")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
timer.resume()
|
||||||
|
pollTimer = timer
|
||||||
|
}
|
||||||
|
|
||||||
|
private func refresh(reason: String) async {
|
||||||
|
guard authManager.isConnected else {
|
||||||
|
if lastSnapshot != nil {
|
||||||
|
lastSnapshot = nil
|
||||||
|
onUpdate?(Update(isConnected: false, snapshot: nil))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
let playback = try await apiClient.getCurrentPlayback()
|
||||||
|
|
||||||
|
guard let track = playback?.item, playback?.isPlaying == true else {
|
||||||
|
if lastSnapshot != nil {
|
||||||
|
lastSnapshot = nil
|
||||||
|
onUpdate?(Update(isConnected: true, snapshot: nil))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let artistName = track.artists.first?.name
|
||||||
|
let snapshot = SpotifyNowPlaying(
|
||||||
|
itemId: track.id,
|
||||||
|
title: track.name,
|
||||||
|
artist: artistName,
|
||||||
|
album: track.album.name,
|
||||||
|
isPlaying: playback?.isPlaying ?? false
|
||||||
|
)
|
||||||
|
|
||||||
|
guard snapshot != lastSnapshot else { return }
|
||||||
|
lastSnapshot = snapshot
|
||||||
|
onUpdate?(Update(isConnected: true, snapshot: snapshot))
|
||||||
|
logger.debug("Spotify now playing: \(snapshot.title)")
|
||||||
|
|
||||||
|
} catch SpotifyAPIClient.SpotifyAPIError.notAuthenticated {
|
||||||
|
if lastSnapshot != nil {
|
||||||
|
lastSnapshot = nil
|
||||||
|
onUpdate?(Update(isConnected: false, snapshot: nil))
|
||||||
|
}
|
||||||
|
} catch SpotifyAPIClient.SpotifyAPIError.rateLimited {
|
||||||
|
logger.warning("Spotify rate limited, skipping this poll")
|
||||||
|
} catch {
|
||||||
|
logger.error("Spotify fetch error: \(error.localizedDescription)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
73
IrisCompanion/iris/ViewModels/TodosViewModel.swift
Normal file
73
IrisCompanion/iris/ViewModels/TodosViewModel.swift
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
//
|
||||||
|
// TodosViewModel.swift
|
||||||
|
// iris
|
||||||
|
//
|
||||||
|
// Created by Codex.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
@MainActor
|
||||||
|
final class TodosViewModel: ObservableObject {
|
||||||
|
@Published private(set) var todos: [TodoItem] = []
|
||||||
|
@Published var newTitle: String = ""
|
||||||
|
|
||||||
|
private let store: TodoStore
|
||||||
|
|
||||||
|
init(store: TodoStore = TodoStore()) {
|
||||||
|
self.store = store
|
||||||
|
refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
func refresh() {
|
||||||
|
todos = sortedItems(store.snapshot())
|
||||||
|
}
|
||||||
|
|
||||||
|
func addTodo() {
|
||||||
|
let trimmed = normalizedTitle(newTitle)
|
||||||
|
guard !trimmed.isEmpty else { return }
|
||||||
|
store.add(title: trimmed)
|
||||||
|
newTitle = ""
|
||||||
|
refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteTodo(id: UUID) {
|
||||||
|
store.delete(id: id)
|
||||||
|
refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
func toggleCompleted(id: UUID) {
|
||||||
|
guard let current = todos.first(where: { $0.id == id }) else { return }
|
||||||
|
store.setCompleted(id: id, completed: !current.isCompleted)
|
||||||
|
refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateTitle(id: UUID, title: String) {
|
||||||
|
let trimmed = normalizedTitle(title)
|
||||||
|
guard !trimmed.isEmpty else { return }
|
||||||
|
store.updateTitle(id: id, title: trimmed)
|
||||||
|
refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
private func normalizedTitle(_ raw: String) -> String {
|
||||||
|
raw.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func sortedItems(_ items: [TodoItem]) -> [TodoItem] {
|
||||||
|
items.sorted { lhs, rhs in
|
||||||
|
if lhs.isCompleted != rhs.isCompleted {
|
||||||
|
return !lhs.isCompleted
|
||||||
|
}
|
||||||
|
if lhs.isCompleted {
|
||||||
|
let lhsDate = lhs.completedAt ?? lhs.updatedAt
|
||||||
|
let rhsDate = rhs.completedAt ?? rhs.updatedAt
|
||||||
|
if lhsDate != rhsDate {
|
||||||
|
return lhsDate > rhsDate
|
||||||
|
}
|
||||||
|
} else if lhs.createdAt != rhs.createdAt {
|
||||||
|
return lhs.createdAt > rhs.createdAt
|
||||||
|
}
|
||||||
|
return lhs.id.uuidString < rhs.id.uuidString
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,7 +45,7 @@ struct OrchestratorView: View {
|
|||||||
Button("Recompute Now") { orchestrator.recomputeNow() }
|
Button("Recompute Now") { orchestrator.recomputeNow() }
|
||||||
}
|
}
|
||||||
|
|
||||||
Section("Feed") {
|
Section("Winner") {
|
||||||
if let feed = orchestrator.lastFeed, let winner = feed.winnerItem() {
|
if let feed = orchestrator.lastFeed, let winner = feed.winnerItem() {
|
||||||
Text(winner.title)
|
Text(winner.title)
|
||||||
.font(.headline)
|
.font(.headline)
|
||||||
@@ -54,36 +54,56 @@ struct OrchestratorView: View {
|
|||||||
.font(.subheadline)
|
.font(.subheadline)
|
||||||
.foregroundStyle(.secondary)
|
.foregroundStyle(.secondary)
|
||||||
}
|
}
|
||||||
Text("type \(winner.type.rawValue) • prio \(String(format: "%.2f", winner.priority)) • ttl \(winner.ttlSec)s")
|
LabeledContent("Type") { Text(winner.type.rawValue) }
|
||||||
.font(.caption)
|
LabeledContent("Bucket") { Text(winner.bucket.rawValue) }
|
||||||
.foregroundStyle(.secondary)
|
LabeledContent("Priority") { Text(String(format: "%.2f", winner.priority)) }
|
||||||
|
LabeledContent("TTL") { Text("\(winner.ttlSec)s") }
|
||||||
if feed.feed.count > 1 {
|
if let poiType = winner.poiType {
|
||||||
Divider()
|
LabeledContent("POI type") { Text(poiType.rawValue) }
|
||||||
}
|
}
|
||||||
|
if let startsAt = winner.startsAt {
|
||||||
|
LabeledContent("Starts at") { Text("\(startsAt)") }
|
||||||
|
}
|
||||||
|
LabeledContent("ID") {
|
||||||
|
Text(winner.id)
|
||||||
|
.font(.caption)
|
||||||
|
.textSelection(.enabled)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Text("No winner yet")
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ForEach(feed.feed, id: \.id) { item in
|
Section("Feed") {
|
||||||
VStack(alignment: .leading, spacing: 6) {
|
if let feed = orchestrator.lastFeed {
|
||||||
HStack {
|
if feed.feed.isEmpty {
|
||||||
Text(item.title)
|
Text("No feed items yet")
|
||||||
.font(.headline)
|
.foregroundStyle(.secondary)
|
||||||
.lineLimit(1)
|
} else {
|
||||||
Spacer()
|
ForEach(feed.feed, id: \.id) { item in
|
||||||
Text(item.type.rawValue)
|
VStack(alignment: .leading, spacing: 6) {
|
||||||
|
HStack {
|
||||||
|
Text(item.title)
|
||||||
|
.font(.headline)
|
||||||
|
.lineLimit(1)
|
||||||
|
Spacer()
|
||||||
|
Text(item.type.rawValue)
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
}
|
||||||
|
if !item.subtitle.isEmpty {
|
||||||
|
Text(item.subtitle)
|
||||||
|
.font(.subheadline)
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
.lineLimit(1)
|
||||||
|
}
|
||||||
|
Text("bucket \(item.bucket.rawValue) • prio \(String(format: "%.2f", item.priority)) • ttl \(item.ttlSec)s")
|
||||||
.font(.caption)
|
.font(.caption)
|
||||||
.foregroundStyle(.secondary)
|
.foregroundStyle(.secondary)
|
||||||
}
|
}
|
||||||
if !item.subtitle.isEmpty {
|
.padding(.vertical, 4)
|
||||||
Text(item.subtitle)
|
|
||||||
.font(.subheadline)
|
|
||||||
.foregroundStyle(.secondary)
|
|
||||||
.lineLimit(1)
|
|
||||||
}
|
|
||||||
Text("bucket \(item.bucket.rawValue) • prio \(String(format: "%.2f", item.priority)) • ttl \(item.ttlSec)s")
|
|
||||||
.font(.caption)
|
|
||||||
.foregroundStyle(.secondary)
|
|
||||||
}
|
}
|
||||||
.padding(.vertical, 4)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Text("No feed yet")
|
Text("No feed yet")
|
||||||
|
|||||||
177
IrisCompanion/iris/Views/SettingsView.swift
Normal file
177
IrisCompanion/iris/Views/SettingsView.swift
Normal file
@@ -0,0 +1,177 @@
|
|||||||
|
//
|
||||||
|
// SettingsView.swift
|
||||||
|
// iris
|
||||||
|
//
|
||||||
|
// Settings UI for music source selection and Spotify connection.
|
||||||
|
//
|
||||||
|
|
||||||
|
import MusicKit
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
@available(iOS 16.0, *)
|
||||||
|
struct SettingsView: View {
|
||||||
|
@EnvironmentObject private var orchestrator: ContextOrchestrator
|
||||||
|
@EnvironmentObject private var spotifyAuth: SpotifyAuthManager
|
||||||
|
@State private var isConfigExpanded: Bool = false
|
||||||
|
@State private var clientIdInput: String = ""
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
NavigationStack {
|
||||||
|
List {
|
||||||
|
Section("Music Source") {
|
||||||
|
Picker("Source", selection: Binding(
|
||||||
|
get: { orchestrator.musicSource },
|
||||||
|
set: { newValue in
|
||||||
|
if newValue == .spotify && !spotifyAuth.isConnected {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
orchestrator.musicSource = newValue
|
||||||
|
}
|
||||||
|
)) {
|
||||||
|
ForEach(MusicSource.allCases, id: \.self) { source in
|
||||||
|
Text(source.displayName).tag(source)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.pickerStyle(.segmented)
|
||||||
|
|
||||||
|
if !spotifyAuth.isConnected {
|
||||||
|
Text("Connect Spotify below to enable it as a source")
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Section {
|
||||||
|
DisclosureGroup("Configuration", isExpanded: $isConfigExpanded) {
|
||||||
|
TextField("Client ID", text: $clientIdInput)
|
||||||
|
.textInputAutocapitalization(.never)
|
||||||
|
.autocorrectionDisabled()
|
||||||
|
.font(.system(.body, design: .monospaced))
|
||||||
|
|
||||||
|
Text("Get your Client ID from developer.spotify.com/dashboard")
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
|
||||||
|
Button("Save") {
|
||||||
|
spotifyAuth.setClientId(clientIdInput)
|
||||||
|
isConfigExpanded = false
|
||||||
|
}
|
||||||
|
.disabled(clientIdInput.trimmingCharacters(in: .whitespaces).isEmpty)
|
||||||
|
}
|
||||||
|
.onAppear {
|
||||||
|
clientIdInput = spotifyAuth.clientId
|
||||||
|
}
|
||||||
|
|
||||||
|
if spotifyAuth.isConnected {
|
||||||
|
HStack {
|
||||||
|
Label("Connected", systemImage: "checkmark.circle.fill")
|
||||||
|
.foregroundColor(.green)
|
||||||
|
Spacer()
|
||||||
|
}
|
||||||
|
Button("Disconnect", role: .destructive) {
|
||||||
|
spotifyAuth.disconnect()
|
||||||
|
if orchestrator.musicSource == .spotify {
|
||||||
|
orchestrator.musicSource = .appleMusic
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if spotifyAuth.isConfigured {
|
||||||
|
Button {
|
||||||
|
spotifyAuth.startAuth()
|
||||||
|
} label: {
|
||||||
|
HStack {
|
||||||
|
Label("Connect to Spotify", systemImage: "link")
|
||||||
|
Spacer()
|
||||||
|
if spotifyAuth.isAuthenticating {
|
||||||
|
ProgressView()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.disabled(spotifyAuth.isAuthenticating)
|
||||||
|
} else {
|
||||||
|
Text("Enter your Client ID above to connect")
|
||||||
|
.font(.callout)
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
}
|
||||||
|
|
||||||
|
if let error = spotifyAuth.error {
|
||||||
|
Text(error)
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.red)
|
||||||
|
}
|
||||||
|
} header: {
|
||||||
|
Text("Spotify")
|
||||||
|
} footer: {
|
||||||
|
if !spotifyAuth.isConnected && spotifyAuth.isConfigured {
|
||||||
|
Text("Connect your Spotify account to display current track on Glass.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Section {
|
||||||
|
HStack {
|
||||||
|
Text("Authorization")
|
||||||
|
Spacer()
|
||||||
|
Text(authStatusText(orchestrator.musicAuthorization))
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
}
|
||||||
|
} header: {
|
||||||
|
Text("Apple Music")
|
||||||
|
}
|
||||||
|
|
||||||
|
Section {
|
||||||
|
if let nowPlayingInfo = currentNowPlaying {
|
||||||
|
VStack(alignment: .leading, spacing: 4) {
|
||||||
|
Text(nowPlayingInfo.title)
|
||||||
|
.font(.headline)
|
||||||
|
if let artist = nowPlayingInfo.artist {
|
||||||
|
Text(artist)
|
||||||
|
.font(.subheadline)
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Text("Nothing playing")
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
}
|
||||||
|
} header: {
|
||||||
|
Text("Now Playing")
|
||||||
|
} footer: {
|
||||||
|
Text("Source: \(orchestrator.musicSource.displayName)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.navigationTitle("Settings")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private var currentNowPlaying: (title: String, artist: String?)? {
|
||||||
|
switch orchestrator.musicSource {
|
||||||
|
case .appleMusic:
|
||||||
|
guard let np = orchestrator.nowPlaying else { return nil }
|
||||||
|
return (np.title, np.artist)
|
||||||
|
case .spotify:
|
||||||
|
guard let np = orchestrator.spotifyNowPlaying else { return nil }
|
||||||
|
return (np.title, np.artist)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func authStatusText(_ status: MusicAuthorization.Status) -> String {
|
||||||
|
switch status {
|
||||||
|
case .notDetermined: return "Not Determined"
|
||||||
|
case .denied: return "Denied"
|
||||||
|
case .restricted: return "Restricted"
|
||||||
|
case .authorized: return "Authorized"
|
||||||
|
@unknown default: return "Unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(iOS 16.0, *)
|
||||||
|
struct SettingsView_Previews: PreviewProvider {
|
||||||
|
static var previews: some View {
|
||||||
|
let ble = BlePeripheralManager()
|
||||||
|
let spotifyAuth = SpotifyAuthManager()
|
||||||
|
let orchestrator = ContextOrchestrator(ble: ble, spotifyAuth: spotifyAuth)
|
||||||
|
SettingsView()
|
||||||
|
.environmentObject(orchestrator)
|
||||||
|
.environmentObject(spotifyAuth)
|
||||||
|
}
|
||||||
|
}
|
||||||
195
IrisCompanion/iris/Views/TodosView.swift
Normal file
195
IrisCompanion/iris/Views/TodosView.swift
Normal file
@@ -0,0 +1,195 @@
|
|||||||
|
//
|
||||||
|
// TodosView.swift
|
||||||
|
// iris
|
||||||
|
//
|
||||||
|
// Created by Codex.
|
||||||
|
//
|
||||||
|
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
struct TodosView: View {
|
||||||
|
@StateObject private var model = TodosViewModel()
|
||||||
|
@State private var isDoneExpanded = false
|
||||||
|
@State private var lastDoneCount = 0
|
||||||
|
|
||||||
|
private var openTodos: [TodoItem] {
|
||||||
|
model.todos.filter { !$0.isCompleted }
|
||||||
|
}
|
||||||
|
|
||||||
|
private var doneTodos: [TodoItem] {
|
||||||
|
model.todos.filter { $0.isCompleted }
|
||||||
|
}
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
NavigationStack {
|
||||||
|
List {
|
||||||
|
Section("Add") {
|
||||||
|
HStack(spacing: 12) {
|
||||||
|
TextField("Add a todo", text: $model.newTitle)
|
||||||
|
.textInputAutocapitalization(.sentences)
|
||||||
|
.disableAutocorrection(false)
|
||||||
|
.submitLabel(.done)
|
||||||
|
.onSubmit { model.addTodo() }
|
||||||
|
|
||||||
|
Button {
|
||||||
|
model.addTodo()
|
||||||
|
} label: {
|
||||||
|
Image(systemName: "plus.circle.fill")
|
||||||
|
.font(.title3)
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
|
.disabled(model.newTitle.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
|
||||||
|
.accessibilityLabel("Add todo")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Section("Todos") {
|
||||||
|
if openTodos.isEmpty {
|
||||||
|
Text("No open todos")
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
} else {
|
||||||
|
ForEach(openTodos) { item in
|
||||||
|
TodoRow(
|
||||||
|
item: item,
|
||||||
|
onToggle: { model.toggleCompleted(id: item.id) },
|
||||||
|
onDelete: { model.deleteTodo(id: item.id) },
|
||||||
|
onUpdateTitle: { model.updateTitle(id: item.id, title: $0) }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Section {
|
||||||
|
if isDoneExpanded {
|
||||||
|
if doneTodos.isEmpty {
|
||||||
|
Text("No completed todos")
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
} else {
|
||||||
|
ForEach(doneTodos) { item in
|
||||||
|
TodoRow(
|
||||||
|
item: item,
|
||||||
|
onToggle: { model.toggleCompleted(id: item.id) },
|
||||||
|
onDelete: { model.deleteTodo(id: item.id) },
|
||||||
|
onUpdateTitle: { model.updateTitle(id: item.id, title: $0) }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} header: {
|
||||||
|
Button {
|
||||||
|
withAnimation(.easeInOut(duration: 0.15)) {
|
||||||
|
isDoneExpanded.toggle()
|
||||||
|
}
|
||||||
|
} label: {
|
||||||
|
HStack(spacing: 8) {
|
||||||
|
Text("Done")
|
||||||
|
Spacer()
|
||||||
|
if doneTodos.count > 0 {
|
||||||
|
Text("\(doneTodos.count)")
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
}
|
||||||
|
Image(systemName: isDoneExpanded ? "chevron.down" : "chevron.right")
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
.font(.caption)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.navigationTitle("Todos")
|
||||||
|
.onAppear {
|
||||||
|
lastDoneCount = doneTodos.count
|
||||||
|
if lastDoneCount == 0 {
|
||||||
|
isDoneExpanded = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.onChange(of: doneTodos.count) { newCount in
|
||||||
|
if lastDoneCount == 0, newCount > 0, !isDoneExpanded {
|
||||||
|
withAnimation(.easeInOut(duration: 0.15)) {
|
||||||
|
isDoneExpanded = true
|
||||||
|
}
|
||||||
|
} else if newCount == 0, isDoneExpanded {
|
||||||
|
withAnimation(.easeInOut(duration: 0.15)) {
|
||||||
|
isDoneExpanded = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lastDoneCount = newCount
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private struct TodoRow: View {
|
||||||
|
let item: TodoItem
|
||||||
|
let onToggle: () -> Void
|
||||||
|
let onDelete: () -> Void
|
||||||
|
let onUpdateTitle: (String) -> Void
|
||||||
|
|
||||||
|
@State private var draftTitle: String
|
||||||
|
@FocusState private var isFocused: Bool
|
||||||
|
|
||||||
|
init(item: TodoItem,
|
||||||
|
onToggle: @escaping () -> Void,
|
||||||
|
onDelete: @escaping () -> Void,
|
||||||
|
onUpdateTitle: @escaping (String) -> Void) {
|
||||||
|
self.item = item
|
||||||
|
self.onToggle = onToggle
|
||||||
|
self.onDelete = onDelete
|
||||||
|
self.onUpdateTitle = onUpdateTitle
|
||||||
|
_draftTitle = State(initialValue: item.title)
|
||||||
|
}
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
HStack(spacing: 12) {
|
||||||
|
Button(action: onToggle) {
|
||||||
|
Image(systemName: item.isCompleted ? "checkmark.circle.fill" : "circle")
|
||||||
|
.foregroundStyle(item.isCompleted ? .secondary : .primary)
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
|
.accessibilityLabel(item.isCompleted ? "Mark incomplete" : "Mark complete")
|
||||||
|
|
||||||
|
TextField("Todo", text: $draftTitle)
|
||||||
|
.focused($isFocused)
|
||||||
|
.submitLabel(.done)
|
||||||
|
.onSubmit { commitTitleIfNeeded() }
|
||||||
|
.onChange(of: isFocused) { focused in
|
||||||
|
if !focused {
|
||||||
|
commitTitleIfNeeded()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.onChange(of: item.title) { newValue in
|
||||||
|
if !isFocused {
|
||||||
|
draftTitle = newValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.foregroundStyle(item.isCompleted ? .secondary : .primary)
|
||||||
|
.opacity(item.isCompleted ? 0.7 : 1.0)
|
||||||
|
}
|
||||||
|
.onDisappear {
|
||||||
|
if isFocused {
|
||||||
|
commitTitleIfNeeded()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.swipeActions(edge: .trailing, allowsFullSwipe: true) {
|
||||||
|
Button(role: .destructive, action: onDelete) {
|
||||||
|
Label("Delete", systemImage: "trash")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func commitTitleIfNeeded() {
|
||||||
|
let trimmed = draftTitle.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
|
guard !trimmed.isEmpty else {
|
||||||
|
draftTitle = item.title
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guard trimmed != item.title else { return }
|
||||||
|
onUpdateTitle(trimmed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TodosView_Previews: PreviewProvider {
|
||||||
|
static var previews: some View {
|
||||||
|
TodosView()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,12 +12,15 @@ struct irisApp: App {
|
|||||||
@Environment(\.scenePhase) private var scenePhase
|
@Environment(\.scenePhase) private var scenePhase
|
||||||
@StateObject private var ble: BlePeripheralManager
|
@StateObject private var ble: BlePeripheralManager
|
||||||
@StateObject private var orchestrator: ContextOrchestrator
|
@StateObject private var orchestrator: ContextOrchestrator
|
||||||
|
@StateObject private var spotifyAuth: SpotifyAuthManager
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
let bleManager = BlePeripheralManager()
|
let bleManager = BlePeripheralManager()
|
||||||
bleManager.start()
|
bleManager.start()
|
||||||
|
let spotify = SpotifyAuthManager()
|
||||||
_ble = StateObject(wrappedValue: bleManager)
|
_ble = StateObject(wrappedValue: bleManager)
|
||||||
_orchestrator = StateObject(wrappedValue: ContextOrchestrator(ble: bleManager))
|
_spotifyAuth = StateObject(wrappedValue: spotify)
|
||||||
|
_orchestrator = StateObject(wrappedValue: ContextOrchestrator(ble: bleManager, spotifyAuth: spotify))
|
||||||
}
|
}
|
||||||
|
|
||||||
var body: some Scene {
|
var body: some Scene {
|
||||||
@@ -25,6 +28,12 @@ struct irisApp: App {
|
|||||||
ContentView()
|
ContentView()
|
||||||
.environmentObject(ble)
|
.environmentObject(ble)
|
||||||
.environmentObject(orchestrator)
|
.environmentObject(orchestrator)
|
||||||
|
.environmentObject(spotifyAuth)
|
||||||
|
.onOpenURL { url in
|
||||||
|
if url.scheme == "iris-spotify-auth" {
|
||||||
|
spotifyAuth.handleCallback(url: url)
|
||||||
|
}
|
||||||
|
}
|
||||||
.onChange(of: scenePhase) { phase in
|
.onChange(of: scenePhase) { phase in
|
||||||
if phase == .active || phase == .background {
|
if phase == .active || phase == .background {
|
||||||
ble.start()
|
ble.start()
|
||||||
|
|||||||
Reference in New Issue
Block a user