From 9528b0b57e1ec0d5864f169a9a0ad71d54494e49 Mon Sep 17 00:00:00 2001 From: christophergyman Date: Sat, 10 Jan 2026 20:30:18 +0000 Subject: [PATCH] Add Yahoo Finance stock data integration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add StockDataSource to fetch quotes from Yahoo Finance API - Add StockSettingsStore for persisting user's stock symbols - Add StockSettingsView with UI to manage symbols (max 5) - Add STOCK feed item type and ranker weight (0.3) - Integrate stock fetch into ContextOrchestrator pipeline - Stock cards appear in FYI bucket and sync to Glass via BLE 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- IrisCompanion/iris/ContentView.swift | 5 + .../iris/DataSources/StockDataSource.swift | 186 ++++++++++++++++++ .../iris/Models/HeuristicRanker.swift | 1 + .../iris/Models/StockSettingsStore.swift | 48 +++++ IrisCompanion/iris/Models/Winner.swift | 1 + .../Orchestrator/ContextOrchestrator.swift | 71 ++++++- .../iris/Views/StockSettingsView.swift | 104 ++++++++++ 7 files changed, 415 insertions(+), 1 deletion(-) create mode 100644 IrisCompanion/iris/DataSources/StockDataSource.swift create mode 100644 IrisCompanion/iris/Models/StockSettingsStore.swift create mode 100644 IrisCompanion/iris/Views/StockSettingsView.swift diff --git a/IrisCompanion/iris/ContentView.swift b/IrisCompanion/iris/ContentView.swift index a89accf..26a33fc 100644 --- a/IrisCompanion/iris/ContentView.swift +++ b/IrisCompanion/iris/ContentView.swift @@ -9,6 +9,7 @@ import SwiftUI struct ContentView: View { @EnvironmentObject private var orchestrator: ContextOrchestrator + @StateObject private var stockSettings = StockSettingsStore() var body: some View { TabView { @@ -16,6 +17,10 @@ struct ContentView: View { .tabItem { Label("BLE", systemImage: "dot.radiowaves.left.and.right") } OrchestratorView() .tabItem { Label("Orchestrator", systemImage: "bolt.horizontal.circle") } + NavigationStack { + StockSettingsView(store: stockSettings) + } + .tabItem { Label("Stocks", systemImage: "chart.line.uptrend.xyaxis") } } .onAppear { orchestrator.start() } } diff --git a/IrisCompanion/iris/DataSources/StockDataSource.swift b/IrisCompanion/iris/DataSources/StockDataSource.swift new file mode 100644 index 0000000..a189317 --- /dev/null +++ b/IrisCompanion/iris/DataSources/StockDataSource.swift @@ -0,0 +1,186 @@ +// +// StockDataSource.swift +// iris +// + +import Foundation + +struct StockDataSourceConfig: Sendable { + var maxSymbols: Int = 5 + var cacheValiditySec: Int = 300 + var ttlSec: Int = 600 + + init() {} +} + +final class StockDataSource { + struct StockQuote: Sendable, Equatable { + let symbol: String + let shortName: String + let price: Double + let change: Double + let changePercent: Double + let marketState: String + } + + struct StockData: Sendable, Equatable { + let quotes: [StockQuote] + } + + struct Snapshot: Sendable { + let data: StockData + let diagnostics: [String: String] + } + + enum StockError: Error, LocalizedError, Sendable { + case noSymbolsConfigured + case networkFailed(message: String, diagnostics: [String: String]) + case rateLimited(diagnostics: [String: String]) + case invalidResponse(diagnostics: [String: String]) + + var errorDescription: String? { + switch self { + case .noSymbolsConfigured: + return "No symbols configured" + case .networkFailed(let message, _): + return message + case .rateLimited: + return "Rate limited by Yahoo Finance" + case .invalidResponse: + return "Invalid response from Yahoo Finance" + } + } + } + + private let config: StockDataSourceConfig + private var cache: (timestamp: Int, data: StockData)? + + init(config: StockDataSourceConfig = .init()) { + self.config = config + } + + func dataWithDiagnostics(symbols: [String], now: Int) async throws -> Snapshot { + var diagnostics: [String: String] = [ + "now": String(now), + "symbols_requested": symbols.joined(separator: ","), + "max_symbols": String(config.maxSymbols), + ] + + guard !symbols.isEmpty else { + diagnostics["result"] = "no_symbols" + return Snapshot(data: StockData(quotes: []), diagnostics: diagnostics) + } + + let limitedSymbols = Array(symbols.prefix(config.maxSymbols)) + diagnostics["symbols_queried"] = limitedSymbols.joined(separator: ",") + + 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) + } + + let symbolsParam = limitedSymbols.joined(separator: ",") + guard let encodedSymbols = symbolsParam.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), + let url = URL(string: "https://query1.finance.yahoo.com/v7/finance/quote?symbols=\(encodedSymbols)") else { + diagnostics["error"] = "invalid_url" + throw StockError.networkFailed(message: "Invalid URL", diagnostics: diagnostics) + } + + var request = URLRequest(url: url) + request.setValue("Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X)", forHTTPHeaderField: "User-Agent") + request.setValue("application/json", forHTTPHeaderField: "Accept") + + let data: Data + let response: URLResponse + do { + (data, response) = try await URLSession.shared.data(for: request) + } catch { + diagnostics["error"] = "network_error" + diagnostics["error_message"] = String(describing: error) + if let cached = cache { + diagnostics["source"] = "cache_fallback" + return Snapshot(data: cached.data, diagnostics: diagnostics) + } + throw StockError.networkFailed(message: String(describing: error), diagnostics: diagnostics) + } + + if let httpResponse = response as? HTTPURLResponse { + diagnostics["http_status"] = String(httpResponse.statusCode) + if httpResponse.statusCode == 403 { + diagnostics["error"] = "rate_limited" + if let cached = cache { + diagnostics["source"] = "cache_fallback" + return Snapshot(data: cached.data, diagnostics: diagnostics) + } + throw StockError.rateLimited(diagnostics: diagnostics) + } + guard (200..<300).contains(httpResponse.statusCode) else { + diagnostics["error"] = "http_error" + if let cached = cache { + diagnostics["source"] = "cache_fallback" + return Snapshot(data: cached.data, diagnostics: diagnostics) + } + throw StockError.networkFailed(message: "HTTP \(httpResponse.statusCode)", diagnostics: diagnostics) + } + } + + let yahooResponse: YahooQuoteResponse + do { + yahooResponse = try JSONDecoder().decode(YahooQuoteResponse.self, from: data) + } catch { + diagnostics["error"] = "json_decode_error" + diagnostics["error_message"] = String(describing: error) + if let cached = cache { + diagnostics["source"] = "cache_fallback" + return Snapshot(data: cached.data, diagnostics: diagnostics) + } + throw StockError.invalidResponse(diagnostics: diagnostics) + } + + let quotes: [StockQuote] = (yahooResponse.quoteResponse.result ?? []).compactMap { quote in + guard let price = quote.regularMarketPrice else { return nil } + return StockQuote( + symbol: quote.symbol, + shortName: quote.shortName ?? quote.symbol, + price: price, + change: quote.regularMarketChange ?? 0, + changePercent: quote.regularMarketChangePercent ?? 0, + marketState: quote.marketState ?? "CLOSED" + ) + } + + diagnostics["source"] = "network" + diagnostics["quotes_returned"] = String(quotes.count) + + let stockData = StockData(quotes: quotes) + cache = (timestamp: now, data: stockData) + + return Snapshot(data: stockData, diagnostics: diagnostics) + } +} + +// MARK: - Yahoo Finance API Response Models + +private struct YahooQuoteResponse: Codable { + let quoteResponse: QuoteResponse + + struct QuoteResponse: Codable { + let result: [Quote]? + let error: QuoteError? + } + + struct Quote: Codable { + let symbol: String + let shortName: String? + let regularMarketPrice: Double? + let regularMarketChange: Double? + let regularMarketChangePercent: Double? + let marketState: String? + } + + struct QuoteError: Codable { + let code: String? + let description: String? + } +} diff --git a/IrisCompanion/iris/Models/HeuristicRanker.swift b/IrisCompanion/iris/Models/HeuristicRanker.swift index 7400f97..bd09752 100644 --- a/IrisCompanion/iris/Models/HeuristicRanker.swift +++ b/IrisCompanion/iris/Models/HeuristicRanker.swift @@ -90,6 +90,7 @@ final class HeuristicRanker { case .transit: return 0.75 case .poiNearby: return 0.6 case .info: return 0.4 + case .stock: return 0.3 case .nowPlaying: return 0.25 case .currentWeather: return 0.0 case .allQuiet: return 0.0 diff --git a/IrisCompanion/iris/Models/StockSettingsStore.swift b/IrisCompanion/iris/Models/StockSettingsStore.swift new file mode 100644 index 0000000..b12530a --- /dev/null +++ b/IrisCompanion/iris/Models/StockSettingsStore.swift @@ -0,0 +1,48 @@ +// +// StockSettingsStore.swift +// iris +// + +import Foundation +import Combine + +@MainActor +final class StockSettingsStore: ObservableObject { + nonisolated static let userDefaultsKey = "iris.stock.symbols" + + @Published private(set) var symbols: [String] = [] + + private let maxSymbols = 5 + + init() { + loadSymbols() + } + + private func loadSymbols() { + symbols = UserDefaults.standard.stringArray(forKey: Self.userDefaultsKey) ?? [] + } + + func saveSymbols(_ newSymbols: [String]) { + let cleaned = newSymbols + .map { $0.uppercased().trimmingCharacters(in: .whitespacesAndNewlines) } + .filter { !$0.isEmpty } + .prefix(maxSymbols) + symbols = Array(cleaned) + UserDefaults.standard.set(symbols, forKey: Self.userDefaultsKey) + } + + @discardableResult + func addSymbol(_ symbol: String) -> Bool { + guard symbols.count < maxSymbols else { return false } + let cleaned = symbol.uppercased().trimmingCharacters(in: .whitespacesAndNewlines) + guard !cleaned.isEmpty, !symbols.contains(cleaned) else { return false } + symbols.append(cleaned) + UserDefaults.standard.set(symbols, forKey: Self.userDefaultsKey) + return true + } + + func removeSymbol(_ symbol: String) { + symbols.removeAll { $0 == symbol.uppercased() } + UserDefaults.standard.set(symbols, forKey: Self.userDefaultsKey) + } +} diff --git a/IrisCompanion/iris/Models/Winner.swift b/IrisCompanion/iris/Models/Winner.swift index 840782b..618780e 100644 --- a/IrisCompanion/iris/Models/Winner.swift +++ b/IrisCompanion/iris/Models/Winner.swift @@ -16,5 +16,6 @@ enum FeedItemType: String, Codable, CaseIterable { case nowPlaying = "NOW_PLAYING" case currentWeather = "CURRENT_WEATHER" case calendarEvent = "CALENDAR_EVENT" + case stock = "STOCK" case allQuiet = "ALL_QUIET" } diff --git a/IrisCompanion/iris/Orchestrator/ContextOrchestrator.swift b/IrisCompanion/iris/Orchestrator/ContextOrchestrator.swift index a16c9bb..b70ba33 100644 --- a/IrisCompanion/iris/Orchestrator/ContextOrchestrator.swift +++ b/IrisCompanion/iris/Orchestrator/ContextOrchestrator.swift @@ -33,6 +33,7 @@ final class ContextOrchestrator: NSObject, ObservableObject { private let weatherDataSource = WeatherDataSource() private let calendarDataSource = CalendarDataSource() private let poiDataSource = POIDataSource() + private let stockDataSource = StockDataSource() private let ranker: HeuristicRanker private let store: FeedStore private let server: LocalServer @@ -204,10 +205,15 @@ final class ContextOrchestrator: NSObject, ObservableObject { async let poiResult = withTimeoutResult(seconds: 6) { try await self.poiDataSource.data(for: location, now: nowEpoch) } + let stockSymbols = UserDefaults.standard.stringArray(forKey: StockSettingsStore.userDefaultsKey) ?? [] + async let stockResult = withTimeoutResult(seconds: 6) { + try await self.stockDataSource.dataWithDiagnostics(symbols: stockSymbols, now: nowEpoch) + } let wxRes = await weatherResult let calRes = await calendarResult let poiRes = await poiResult + let stockRes = await stockResult func calendarTTL(endAt: Int, now: Int) -> Int { let ttl = endAt - now @@ -223,6 +229,7 @@ final class ContextOrchestrator: NSObject, ObservableObject { var rightNowCandidates: [HeuristicRanker.Ranked] = [] var calendarItems: [FeedItem] = [] var poiItems: [FeedItem] = [] + var stockItems: [FeedItem] = [] var weatherNowItem: FeedItem? = nil var fetchFailed = false var wxDiagnostics: [String: String] = [:] @@ -385,13 +392,26 @@ final class ContextOrchestrator: NSObject, ObservableObject { } } + switch stockRes { + case .success(let snapshot): + for quote in snapshot.data.quotes.prefix(3) { + let item = stockQuoteToFeedItem(quote: quote, now: nowEpoch) + stockItems.append(item) + } + if !snapshot.data.quotes.isEmpty { + logger.info("stock quotes fetched count=\(snapshot.data.quotes.count)") + } + case .failure(let error): + logger.warning("stock fetch failed: \(String(describing: error), privacy: .public)") + } + let elapsedMs = Int(Date().timeIntervalSince(start) * 1000) lastPipelineElapsedMs = elapsedMs lastFetchFailed = fetchFailed lastWeatherDiagnostics = wxDiagnostics lastCalendarDiagnostics = calDiagnostics - logger.info("pipeline right_now_candidates=\(rightNowCandidates.count) calendar_items=\(calendarItems.count) poi_items=\(poiItems.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) fetchFailed=\(fetchFailed) elapsed_ms=\(elapsedMs)") if fetchFailed, rightNowCandidates.isEmpty, calendarItems.isEmpty, weatherNowItem == nil { let fallbackFeed = store.getFeed(now: nowEpoch) @@ -459,6 +479,27 @@ final class ContextOrchestrator: NSObject, ObservableObject { fyi.append(weatherNowItem) } + let fyiStocks = stockItems + .filter { $0.id != winnerItem.id } + .filter { !store.isSuppressed(id: $0.id, type: $0.type, now: nowEpoch) } + .prefix(3) + + fyi.append(contentsOf: fyiStocks.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 feedEnvelope = FeedEnvelope( schema: 1, @@ -532,6 +573,34 @@ final class ContextOrchestrator: NSObject, ObservableObject { } return "\(Int(meters.rounded())) m" } + + private func stockQuoteToFeedItem(quote: StockDataSource.StockQuote, now: Int) -> FeedItem { + let direction = quote.change >= 0 ? "+" : "" + let priceStr: String + if quote.price >= 100000 { + priceStr = String(format: "%.1fK", quote.price / 1000) + } else if quote.price >= 1000 { + priceStr = String(format: "%.0f", quote.price) + } else { + priceStr = String(format: "%.2f", quote.price) + } + let title = "\(quote.symbol) $\(priceStr)" + let subtitle = "\(direction)\(String(format: "%.2f", quote.change)) (\(direction)\(String(format: "%.2f", quote.changePercent))%)" + + return FeedItem( + id: "stock:\(quote.symbol):\(now / 300)", + type: .stock, + title: title.truncated(maxLength: TextConstraints.titleMax), + subtitle: subtitle.truncated(maxLength: TextConstraints.subtitleMax), + priority: 0.3, + ttlSec: 600, + condition: nil, + startsAt: nil, + poiType: nil, + bucket: .fyi, + actions: ["DISMISS"] + ) + } } extension ContextOrchestrator: CLLocationManagerDelegate { diff --git a/IrisCompanion/iris/Views/StockSettingsView.swift b/IrisCompanion/iris/Views/StockSettingsView.swift new file mode 100644 index 0000000..562aeb3 --- /dev/null +++ b/IrisCompanion/iris/Views/StockSettingsView.swift @@ -0,0 +1,104 @@ +// +// StockSettingsView.swift +// iris +// + +import SwiftUI + +struct StockSettingsView: View { + @ObservedObject var store: StockSettingsStore + @State private var newSymbol: String = "" + @State private var showError: Bool = false + @State private var errorMessage: String = "" + + var body: some View { + Form { + Section { + ForEach(store.symbols, id: \.self) { symbol in + HStack { + Text(symbol) + .font(.body.monospaced()) + Spacer() + Button(role: .destructive) { + store.removeSymbol(symbol) + } label: { + Image(systemName: "trash") + .foregroundStyle(.red) + } + .buttonStyle(.plain) + } + } + .onDelete { indexSet in + for index in indexSet { + store.removeSymbol(store.symbols[index]) + } + } + + if store.symbols.count < 5 { + HStack { + TextField("Symbol (e.g. AAPL)", text: $newSymbol) + .textInputAutocapitalization(.characters) + .autocorrectionDisabled() + .font(.body.monospaced()) + Button("Add") { + addSymbol() + } + .disabled(newSymbol.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) + } + } + } header: { + Text("Stock Symbols") + } footer: { + Text("Enter up to 5 stock symbols. Cards appear in the FYI section.") + } + + Section { + VStack(alignment: .leading, spacing: 8) { + Text("Examples:") + .font(.subheadline.bold()) + Text("AAPL - Apple Inc.") + Text("GOOGL - Alphabet Inc.") + Text("^GSPC - S&P 500 Index") + Text("^DJI - Dow Jones") + Text("^IXIC - NASDAQ Composite") + } + .font(.caption) + .foregroundStyle(.secondary) + } + } + .navigationTitle("Stocks") + .alert("Error", isPresented: $showError) { + Button("OK") {} + } message: { + Text(errorMessage) + } + } + + private func addSymbol() { + let cleaned = newSymbol.trimmingCharacters(in: .whitespacesAndNewlines).uppercased() + guard !cleaned.isEmpty else { return } + + if store.symbols.contains(cleaned) { + errorMessage = "Symbol '\(cleaned)' already exists." + showError = true + return + } + + if store.symbols.count >= 5 { + errorMessage = "Maximum of 5 symbols allowed." + showError = true + return + } + + store.addSymbol(cleaned) + newSymbol = "" + } +} + +struct StockSettingsView_Previews: PreviewProvider { + static var previews: some View { + NavigationStack { + StockSettingsView(store: StockSettingsStore()) + } + } +}