From 2a13e1976fcc3c8289367beb5082efdfa61c5831 Mon Sep 17 00:00:00 2001 From: vgearen Date: Thu, 14 May 2026 10:46:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(settings):=20sort=20'=E6=88=91=E7=9A=84?= =?UTF-8?q?=E8=82=A1=E7=A5=A8'=20with=20held=20stocks=20first?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Stocks with both costPrice and holdingShares set (effective values from real portfolio or active watchlist) appear at the top; the rest follow in original order. Makes managing larger watchlists easier. --- StockMonitor/Views/SettingsView.swift | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/StockMonitor/Views/SettingsView.swift b/StockMonitor/Views/SettingsView.swift index ff05d8a..9055ac8 100644 --- a/StockMonitor/Views/SettingsView.swift +++ b/StockMonitor/Views/SettingsView.swift @@ -247,10 +247,10 @@ struct SettingsView: View { } } - // 已有股票 + // 已有股票:设置了持仓的排在前面,未设置持仓的在后 if !appState.stocks.isEmpty { section("我的股票") { - ForEach(appState.stocks) { s in stockRow(s) } + ForEach(sortedStocksForSettings) { s in stockRow(s) } } } } // end else @@ -261,6 +261,22 @@ struct SettingsView: View { .frame(width: 284, height: 480) } + // MARK: - 排序辅助 + + /// 设置页"我的股票"排序:有持仓(成本 + 股数都设置了)的排在前面, + /// 其它的排在后面;组内保持原顺序。 + private var sortedStocksForSettings: [Stock] { + let withHolding = appState.stocks.filter { + appState.effectiveCostPrice(for: $0) != nil + && appState.effectiveShares(for: $0) != nil + } + let withoutHolding = appState.stocks.filter { + appState.effectiveCostPrice(for: $0) == nil + || appState.effectiveShares(for: $0) == nil + } + return withHolding + withoutHolding + } + // MARK: - 子视图 @ViewBuilder