Add Yahoo Finance stock data integration

- 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 <noreply@anthropic.com>
This commit is contained in:
2026-01-10 20:30:18 +00:00
parent c13a4f3247
commit 9528b0b57e
7 changed files with 415 additions and 1 deletions

View File

@@ -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 {