Compare commits
2 Commits
move-pois-
...
25cdebd1b2
| Author | SHA1 | Date | |
|---|---|---|---|
| 25cdebd1b2 | |||
| 9528b0b57e |
@@ -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() }
|
||||
}
|
||||
|
||||
177
IrisCompanion/iris/DataSources/StockDataSource.swift
Normal file
177
IrisCompanion/iris/DataSources/StockDataSource.swift
Normal file
@@ -0,0 +1,177 @@
|
||||
//
|
||||
// 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)
|
||||
}
|
||||
|
||||
// Fetch each symbol using v8 chart API (supports one symbol per request)
|
||||
var quotes: [StockQuote] = []
|
||||
var fetchErrors: [String] = []
|
||||
|
||||
for symbol in limitedSymbols {
|
||||
do {
|
||||
if let quote = try await fetchQuote(symbol: symbol) {
|
||||
quotes.append(quote)
|
||||
}
|
||||
} catch {
|
||||
fetchErrors.append("\(symbol): \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
|
||||
diagnostics["source"] = "network"
|
||||
diagnostics["quotes_returned"] = String(quotes.count)
|
||||
if !fetchErrors.isEmpty {
|
||||
diagnostics["fetch_errors"] = fetchErrors.joined(separator: "; ")
|
||||
}
|
||||
|
||||
let stockData = StockData(quotes: quotes)
|
||||
cache = (timestamp: now, data: stockData)
|
||||
|
||||
return Snapshot(data: stockData, diagnostics: diagnostics)
|
||||
}
|
||||
|
||||
private func fetchQuote(symbol: String) async throws -> StockQuote? {
|
||||
guard let encodedSymbol = symbol.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
|
||||
let url = URL(string: "https://query1.finance.yahoo.com/v8/finance/chart/\(encodedSymbol)?interval=1d&range=1d") else {
|
||||
return nil
|
||||
}
|
||||
|
||||
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, response) = try await URLSession.shared.data(for: request)
|
||||
|
||||
if let httpResponse = response as? HTTPURLResponse {
|
||||
guard (200..<300).contains(httpResponse.statusCode) else {
|
||||
throw StockError.networkFailed(message: "HTTP \(httpResponse.statusCode)", diagnostics: [:])
|
||||
}
|
||||
}
|
||||
|
||||
let chartResponse = try JSONDecoder().decode(YahooChartResponse.self, from: data)
|
||||
|
||||
guard let result = chartResponse.chart.result?.first,
|
||||
let meta = result.meta,
|
||||
let price = meta.regularMarketPrice else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let previousClose = meta.chartPreviousClose ?? meta.previousClose ?? price
|
||||
let change = price - previousClose
|
||||
let changePercent = previousClose > 0 ? (change / previousClose) * 100 : 0
|
||||
|
||||
return StockQuote(
|
||||
symbol: meta.symbol ?? symbol,
|
||||
shortName: meta.shortName ?? meta.symbol ?? symbol,
|
||||
price: price,
|
||||
change: change,
|
||||
changePercent: changePercent,
|
||||
marketState: meta.marketState ?? "CLOSED"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Yahoo Finance v8 Chart API Response Models
|
||||
|
||||
private struct YahooChartResponse: Codable {
|
||||
let chart: Chart
|
||||
|
||||
struct Chart: Codable {
|
||||
let result: [ChartResult]?
|
||||
let error: ChartError?
|
||||
}
|
||||
|
||||
struct ChartResult: Codable {
|
||||
let meta: ChartMeta?
|
||||
}
|
||||
|
||||
struct ChartMeta: Codable {
|
||||
let symbol: String?
|
||||
let shortName: String?
|
||||
let regularMarketPrice: Double?
|
||||
let chartPreviousClose: Double?
|
||||
let previousClose: Double?
|
||||
let marketState: String?
|
||||
}
|
||||
|
||||
struct ChartError: Codable {
|
||||
let code: String?
|
||||
let description: String?
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
48
IrisCompanion/iris/Models/StockSettingsStore.swift
Normal file
48
IrisCompanion/iris/Models/StockSettingsStore.swift
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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] = [:]
|
||||
@@ -327,7 +334,6 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
||||
if pois.isEmpty {
|
||||
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) {
|
||||
let subtitle = poiSubtitle(for: poi)
|
||||
let confidence = min(max(poi.confidence, 0.0), 1.0)
|
||||
@@ -345,6 +351,7 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
||||
actions: ["DISMISS"]
|
||||
)
|
||||
poiItems.append(item)
|
||||
rightNowCandidates.append(.init(item: item, confidence: confidence, isEligibleForRightNow: true))
|
||||
}
|
||||
case .failure(let error):
|
||||
fetchFailed = true
|
||||
@@ -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)
|
||||
@@ -402,17 +422,10 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
||||
return
|
||||
}
|
||||
|
||||
let poiCandidateCount = rightNowCandidates.filter { $0.item.type == .poiNearby }.count
|
||||
if poiCandidateCount > 0 {
|
||||
logger.warning("dropping poi candidates from right-now ranking count=\(poiCandidateCount)")
|
||||
let eligibleUnsuppressed = rightNowCandidates.filter { ranked in
|
||||
!store.isSuppressed(id: ranked.item.id, type: ranked.item.type, now: nowEpoch)
|
||||
}
|
||||
|
||||
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 winnerItem = winnerSelection?.item ?? FeedEnvelope.allQuiet(now: nowEpoch).feed[0]
|
||||
|
||||
@@ -466,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,
|
||||
@@ -539,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 {
|
||||
|
||||
@@ -45,7 +45,7 @@ struct OrchestratorView: View {
|
||||
Button("Recompute Now") { orchestrator.recomputeNow() }
|
||||
}
|
||||
|
||||
Section("Winner") {
|
||||
Section("Feed") {
|
||||
if let feed = orchestrator.lastFeed, let winner = feed.winnerItem() {
|
||||
Text(winner.title)
|
||||
.font(.headline)
|
||||
@@ -54,56 +54,36 @@ struct OrchestratorView: View {
|
||||
.font(.subheadline)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
LabeledContent("Type") { Text(winner.type.rawValue) }
|
||||
LabeledContent("Bucket") { Text(winner.bucket.rawValue) }
|
||||
LabeledContent("Priority") { Text(String(format: "%.2f", winner.priority)) }
|
||||
LabeledContent("TTL") { Text("\(winner.ttlSec)s") }
|
||||
if let poiType = winner.poiType {
|
||||
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")
|
||||
Text("type \(winner.type.rawValue) • prio \(String(format: "%.2f", winner.priority)) • ttl \(winner.ttlSec)s")
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
|
||||
Section("Feed") {
|
||||
if let feed = orchestrator.lastFeed {
|
||||
if feed.feed.isEmpty {
|
||||
Text("No feed items yet")
|
||||
.foregroundStyle(.secondary)
|
||||
} else {
|
||||
ForEach(feed.feed, id: \.id) { item in
|
||||
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")
|
||||
if feed.feed.count > 1 {
|
||||
Divider()
|
||||
}
|
||||
|
||||
ForEach(feed.feed, id: \.id) { item in
|
||||
VStack(alignment: .leading, spacing: 6) {
|
||||
HStack {
|
||||
Text(item.title)
|
||||
.font(.headline)
|
||||
.lineLimit(1)
|
||||
Spacer()
|
||||
Text(item.type.rawValue)
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
.padding(.vertical, 4)
|
||||
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)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
.padding(.vertical, 4)
|
||||
}
|
||||
} else {
|
||||
Text("No feed yet")
|
||||
|
||||
104
IrisCompanion/iris/Views/StockSettingsView.swift
Normal file
104
IrisCompanion/iris/Views/StockSettingsView.swift
Normal file
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user