feat(settings): sort '我的股票' with held stocks first

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.
This commit is contained in:
vgearen
2026-05-14 10:46:59 +08:00
parent b4033df831
commit 2a13e1976f
+18 -2
View File
@@ -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