mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-22 05:25:41 +00:00
App/ 根目录原有 73 个散落源文件,已无法从目录读出任何分组信息。 按「目录即分层」重组为 Composition / Application / Presentation / UI / PlatformEffects / Resources 六个目录,UI 层再按子视图分目录(Workspace、 Home、Servers(含 Plugin)、Subscriptions、Rules、Settings、Diagnostics、 StatusMenu)。纯移动:类型名、模块名与 import 均未改动,App/ 根层不再有 散落文件。 配套改动: - project.yml:Core 测试镜像的 excludes 由裸文件名改为 `**/` 递归形式。 XcodeGen 的 exclude 不跨 `/`,裸名只匹配 App 根层;MainApp.swift 下移后 旧写法会静默失效,把 @main 组合根编进测试镜像。info.path 与 entitlements.path 指向 App/Resources/;并排除 `**/*.md`——App/ 下任何 非源码文件都会被 XcodeGen 收进 Resources 阶段并封进签名后的 bundle。 - SkeletonTests 的架构守卫由「默认扫描全部 App/ + 7 个豁免文件 + 1 个豁免 前缀 + 4 个豁免目录」改为正面圈定 App/UI/,豁免登记全部删除;新增运行时 适配器放进 Application/ 即自动落在扫描范围外,无需再登记。 - README 工程结构说明同步;根 .gitignore 的生成物路径随 Resources/ 归位。 验证:clean build 0 error;task test 1022 用例全通过(含两个架构守卫); task format 无改动(证明纯移动);task lint 与移动前同为 21 条内容型 warning;packaging-gate.sh ALL CHECKS PASSED,确认 plist/entitlements 归位 后签名与 entitlements 语义不变。 注意:迁移时须先删旧的生成物再 xcodegen generate,否则遗留的 App/Info.plist 会被当作普通资源扫进 Resources 阶段。
156 lines
5.0 KiB
Swift
156 lines
5.0 KiB
Swift
import Combine
|
|
import Foundation
|
|
|
|
/// 首页独立的浏览状态。选择与折叠不改变目录或运行时活动目标。
|
|
@MainActor
|
|
final class HomeServerListState: ObservableObject {
|
|
enum Navigation { case upward, downward, left, right }
|
|
@Published private(set) var tree = CatalogTreeSnapshot(roots: [])
|
|
@Published private(set) var selection: NodeID?
|
|
@Published private(set) var collapsedGroupIDs: Set<NodeID> = []
|
|
private var knownGroupIDs: Set<NodeID> = []
|
|
private var didInitializeSelection = false
|
|
private let defaults: UserDefaults
|
|
private static let expansionKey = "homeServerList.expansion"
|
|
|
|
private struct ExpansionPreferences: Codable {
|
|
let known: Set<NodeID>
|
|
let collapsed: Set<NodeID>
|
|
}
|
|
|
|
init(defaults: UserDefaults = .standard) {
|
|
self.defaults = defaults
|
|
if let data = defaults.data(forKey: Self.expansionKey),
|
|
let saved = try? JSONDecoder().decode(ExpansionPreferences.self, from: data)
|
|
{
|
|
knownGroupIDs = saved.known
|
|
collapsedGroupIDs = saved.collapsed
|
|
}
|
|
}
|
|
|
|
/// 可见行投影委托树快照;折叠集合由本状态的持久化策略持有。
|
|
var visibleRows: [CatalogTreeRow] {
|
|
tree.visibleRows(collapsed: collapsedGroupIDs)
|
|
}
|
|
|
|
func update(tree: CatalogTreeSnapshot, activeTargetID: NodeID?) {
|
|
let previousTree = self.tree
|
|
self.tree = tree
|
|
var currentGroups: Set<NodeID> = []
|
|
func walk(_ nodes: [CatalogTreeNode], depth: Int) {
|
|
for node in nodes where node.isGroup {
|
|
currentGroups.insert(node.id)
|
|
if knownGroupIDs.insert(node.id).inserted, depth > 0 {
|
|
collapsedGroupIDs.insert(node.id)
|
|
}
|
|
walk(node.childNodes, depth: depth + 1)
|
|
}
|
|
}
|
|
walk(tree.roots, depth: 0)
|
|
knownGroupIDs.formIntersection(currentGroups)
|
|
collapsedGroupIDs.formIntersection(currentGroups)
|
|
if !didInitializeSelection, !tree.isEmpty {
|
|
selection = activeTargetID.flatMap { tree.containsNode($0) ? $0 : nil }
|
|
didInitializeSelection = true
|
|
} else if let selection, !tree.containsNode(selection) {
|
|
self.selection = nil
|
|
} else if let selection, tree != previousTree,
|
|
let visibleAncestor = ancestorIDs(for: selection).reversed().first(where: {
|
|
collapsedGroupIDs.contains($0)
|
|
})
|
|
{
|
|
self.selection = visibleAncestor
|
|
}
|
|
saveExpansion()
|
|
}
|
|
|
|
func select(_ id: NodeID) {
|
|
guard visibleRows.contains(where: { $0.id == id }) else { return }
|
|
selection = id
|
|
}
|
|
|
|
/// 行内按钮与 Return 共用门禁;隐藏、无效和已激活目标均不发出命令。
|
|
func activationTarget(
|
|
activeTargetID: NodeID?, eligibility: ActivationEligibility?
|
|
) -> NodeID? {
|
|
guard let selection, selection != activeTargetID,
|
|
eligibility?.canActivate == true,
|
|
visibleRows.contains(where: { $0.id == selection })
|
|
else { return nil }
|
|
return selection
|
|
}
|
|
|
|
/// 明确定位才展开祖先;启动时隐藏的活动项保持选中而不自动展开。
|
|
func locate(_ id: NodeID) {
|
|
guard tree.containsNode(id) else { return }
|
|
collapsedGroupIDs.subtract(ancestorIDs(for: id))
|
|
selection = id
|
|
saveExpansion()
|
|
}
|
|
|
|
func toggleGroup(_ id: NodeID) {
|
|
guard tree.node(withID: id)?.isGroup == true else { return }
|
|
if !collapsedGroupIDs.insert(id).inserted {
|
|
collapsedGroupIDs.remove(id)
|
|
} else if let selection, ancestorIDs(for: selection).contains(id) {
|
|
self.selection = id
|
|
}
|
|
saveExpansion()
|
|
}
|
|
|
|
func ancestorIDs(for id: NodeID) -> [NodeID] {
|
|
var result: [NodeID] = []
|
|
var parent = tree.node(withID: id)?.parentID
|
|
while let id = parent {
|
|
result.append(id)
|
|
parent = tree.node(withID: id)?.parentID
|
|
}
|
|
return result
|
|
}
|
|
|
|
func navigate(_ direction: Navigation) {
|
|
let rows = visibleRows
|
|
guard !rows.isEmpty else { return }
|
|
guard let selection, let index = rows.firstIndex(where: { $0.id == selection }) else {
|
|
if direction == .upward || direction == .downward {
|
|
self.selection = direction == .upward ? rows.last?.id : rows.first?.id
|
|
}
|
|
return
|
|
}
|
|
let node = rows[index].node
|
|
switch direction {
|
|
case .upward:
|
|
self.selection = rows[max(0, index - 1)].id
|
|
case .downward:
|
|
self.selection = rows[min(rows.count - 1, index + 1)].id
|
|
case .left:
|
|
collapseOrSelectParent(node)
|
|
case .right:
|
|
expandOrSelectChild(node)
|
|
}
|
|
}
|
|
|
|
private func collapseOrSelectParent(_ node: CatalogTreeNode) {
|
|
if node.isGroup, !collapsedGroupIDs.contains(node.id) {
|
|
toggleGroup(node.id)
|
|
} else if let parent = node.parentID {
|
|
selection = parent
|
|
}
|
|
}
|
|
|
|
private func expandOrSelectChild(_ node: CatalogTreeNode) {
|
|
if node.isGroup, collapsedGroupIDs.contains(node.id) {
|
|
toggleGroup(node.id)
|
|
} else if let child = node.childNodes.first {
|
|
selection = child.id
|
|
}
|
|
}
|
|
|
|
private func saveExpansion() {
|
|
let preferences = ExpansionPreferences(known: knownGroupIDs, collapsed: collapsedGroupIDs)
|
|
if let data = try? JSONEncoder().encode(preferences) {
|
|
defaults.set(data, forKey: Self.expansionKey)
|
|
}
|
|
}
|
|
}
|