mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-22 13:34:00 +00:00
建立目录工作流深 module(App/CatalogWorkflow/),收窄主窗口与配置 UI 对 配置目录、凭据存储、订阅快照、Legacy 导入与运行时收敛的访问: - UI-facing interface:非敏感树/编辑面/订阅 projection、opaque NodeID 查询、 typed command(ServerEditDraft 等)、typed error 与 structured outcome (BatchImportOutcome 逐行点名、RemovalOutcome selection invalidation); 秘密值只在显式编辑/分享/订阅 URL 命令中出现。 - 服务器编辑:配置与凭据作为一个逻辑变更,CredentialWriteJournal 保证目录 持久化失败时旧配置与旧凭据都保持有效(story 12/13,含测试)。 - 提交与收敛分离:目录提交成功与 runtimeSync 收敛阶段是独立观察面,运行时 失败不回滚已提交目录(story 38/39,含测试)。 - 订阅:创建/刷新/换址/删除沿用原子快照、last-good 保留与凭据回滚;卡片 projection 只含 host,结构化刷新状态由呈现层本地化。 - Legacy 导入端到端编排:不驱动运行时收敛、不自动重复、显式再导入创建独立 分组;handoff 仍经组合根注入的独立边界。 - Deletion test:删除 CatalogViewModel/Types/+Subscriptions 旧 facade,全部 视图与状态菜单迁移到新 seam;选择、pane、sheet、alert 状态收归 UI 持有 (ErrorAlertPresenter)。导入失败行以行号点名,不再回显 ss:// 行内容。 测试经 module interface 观察(395 项全部通过):持久化失败凭据回滚、提交/ 收敛阶段分离、收敛失败不回滚、导入不触发运行时等新增场景;既有 Domain、 协调器与运行时测试保持不变。 Out of scope(spec #41):设置编辑、ProxyRuntimeController、Legacy handoff、 新视觉设计、独立编译 target。
250 lines
7.8 KiB
Swift
250 lines
7.8 KiB
Swift
import SwiftUI
|
|
import UniformTypeIdentifiers
|
|
|
|
/// 「通过 URL 导入」表单(issue #32 添加入口之二):粘贴 ss:// 链接(每行一条),
|
|
/// 与剪贴板入口共用工作流的批量落点;逐行结构化结果在呈现层格式化。
|
|
struct ImportURLSheet: View {
|
|
let workflow: CatalogWorkflow
|
|
let errors: ErrorAlertPresenter
|
|
@Binding var selection: NodeID?
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
@State private var text = ""
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 12) {
|
|
Text("粘贴 ss:// 链接,每行一条;无法解析的行会点名报告。")
|
|
.font(.callout)
|
|
.foregroundStyle(.secondary)
|
|
TextEditor(text: $text)
|
|
.font(.monospaced(.callout)())
|
|
.frame(minWidth: 460, minHeight: 140)
|
|
.overlay(
|
|
RoundedRectangle(cornerRadius: 6)
|
|
.strokeBorder(.separator)
|
|
)
|
|
HStack {
|
|
Button("粘贴自剪贴板") {
|
|
if let clipboard = NSPasteboard.general.string(forType: .string) {
|
|
text = clipboard
|
|
}
|
|
}
|
|
Spacer()
|
|
Button("取消", role: .cancel) { dismiss() }
|
|
Button("导入") { importText() }
|
|
.keyboardShortcut(.defaultAction)
|
|
.disabled(text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
|
|
}
|
|
}
|
|
.padding(20)
|
|
.frame(minWidth: 520, minHeight: 260)
|
|
}
|
|
|
|
private func importText() {
|
|
let parent = workflow.importTargetParent(for: selection)
|
|
Task {
|
|
do {
|
|
let outcome = try await workflow.createServers(fromURIs: text, into: parent)
|
|
if let message = ImportOutcomePresentation.failureMessage(outcome) {
|
|
errors.present(text: message)
|
|
}
|
|
dismiss()
|
|
} catch {
|
|
errors.present(error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 「从二维码图片识别」表单(issue #32 添加入口之三):文件选择 + 拖图入窗。
|
|
/// 屏幕扫码明确不做(spec #21);识别结果按全新身份导入,不与既有节点合并去重。
|
|
struct QRImportSheet: View {
|
|
let workflow: CatalogWorkflow
|
|
let errors: ErrorAlertPresenter
|
|
@Binding var selection: NodeID?
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
@State private var detectedURIs: [String] = []
|
|
@State private var decodeFailures: [String] = []
|
|
@State private var showFileImporter = false
|
|
@State private var hovering = false
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 12) {
|
|
dropZone
|
|
Button("选择图片文件…") { showFileImporter = true }
|
|
if !decodeFailures.isEmpty {
|
|
Text("无法识别:\(decodeFailures.joined(separator: "、"))")
|
|
.font(.footnote)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
if !detectedURIs.isEmpty {
|
|
Text("识别到 \(detectedURIs.count) 条 ss:// 链接:")
|
|
.font(.callout)
|
|
ForEach(detectedURIs, id: \.self) { uri in
|
|
Text(uri)
|
|
.font(.monospaced(.footnote)())
|
|
.lineLimit(1)
|
|
.truncationMode(.middle)
|
|
.help(uri)
|
|
}
|
|
HStack {
|
|
Spacer()
|
|
Button("清除") { detectedURIs = [] }
|
|
Button("导入 \(detectedURIs.count) 台服务器") { importDetected() }
|
|
.keyboardShortcut(.defaultAction)
|
|
}
|
|
}
|
|
Spacer()
|
|
}
|
|
.padding(20)
|
|
.frame(minWidth: 480, minHeight: 300)
|
|
.fileImporter(
|
|
isPresented: $showFileImporter,
|
|
allowedContentTypes: [.image],
|
|
allowsMultipleSelection: true
|
|
) { result in
|
|
let urls = (try? result.get()) ?? []
|
|
processImageSources(urls.map { try? Data(contentsOf: $0) })
|
|
}
|
|
}
|
|
|
|
private var dropZone: some View {
|
|
VStack(spacing: 8) {
|
|
Image(systemName: "photo.on.rectangle.angled")
|
|
.font(.system(size: 36))
|
|
.foregroundStyle(hovering ? .orange : .secondary)
|
|
Text("把二维码图片拖到这里")
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
.frame(maxWidth: .infinity)
|
|
.frame(height: 120)
|
|
.background(
|
|
RoundedRectangle(cornerRadius: 10)
|
|
.fill(hovering ? Color.orange.opacity(0.08) : Color.primary.opacity(0.03))
|
|
)
|
|
.overlay(
|
|
RoundedRectangle(cornerRadius: 10)
|
|
.strokeBorder(style: StrokeStyle(lineWidth: 1, dash: [5]))
|
|
.foregroundStyle(hovering ? AnyShapeStyle(Color.orange) : AnyShapeStyle(.separator))
|
|
)
|
|
.dropDestination(for: Data.self) { payloads, _ in
|
|
processImageSources(payloads)
|
|
return !payloads.isEmpty
|
|
} isTargeted: {
|
|
hovering = $0
|
|
}
|
|
}
|
|
|
|
/// 逐张识别二维码文本负载,仅保留 ss:// 行;非图片或无负载的来源点名。
|
|
private func processImageSources(_ sources: [Data?]) {
|
|
Task.detached(priority: .userInitiated) {
|
|
var uris: [String] = []
|
|
var failures: [String] = []
|
|
for (index, data) in sources.enumerated() {
|
|
guard let data else {
|
|
failures.append("来源 \(index + 1)(不是可用图片)")
|
|
continue
|
|
}
|
|
let payloads = (try? QrCodeCodec.detectPayloads(in: data)) ?? []
|
|
let ssPayloads = payloads.filter { $0.lowercased().hasPrefix("ss://") }
|
|
if ssPayloads.isEmpty {
|
|
failures.append("来源 \(index + 1)(未识别到 ss:// 二维码)")
|
|
} else {
|
|
uris.append(contentsOf: ssPayloads)
|
|
}
|
|
}
|
|
await MainActor.run {
|
|
detectedURIs.append(contentsOf: uris)
|
|
decodeFailures = failures
|
|
}
|
|
}
|
|
}
|
|
|
|
private func importDetected() {
|
|
let parent = workflow.importTargetParent(for: selection)
|
|
Task {
|
|
do {
|
|
let text = detectedURIs.joined(separator: "\n")
|
|
let outcome = try await workflow.createServers(fromURIs: text, into: parent)
|
|
if let message = ImportOutcomePresentation.failureMessage(outcome) {
|
|
errors.present(text: message)
|
|
}
|
|
dismiss()
|
|
} catch {
|
|
errors.present(error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 「移动到」表单(issue #41):目的地由树 projection 派生——目录根与除自身
|
|
/// 子树外的全部手动组;跨来源与成环由领域层最终拒绝。
|
|
struct MoveNodeSheet: View {
|
|
let workflow: CatalogWorkflow
|
|
let errors: ErrorAlertPresenter
|
|
let nodeID: NodeID
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
@State private var destination: NodeID?
|
|
|
|
private struct Destination: Identifiable {
|
|
let id: NodeID?
|
|
let title: String
|
|
}
|
|
|
|
private var destinations: [Destination] {
|
|
var result = [Destination(id: nil, title: "目录根")]
|
|
let excluded = workflow.tree.node(withID: nodeID)?.subtreeIDs ?? [nodeID]
|
|
func walk(_ nodes: [CatalogTreeNode], depth: Int) {
|
|
for node in nodes {
|
|
guard node.isGroup, node.isManual else { continue }
|
|
guard !excluded.contains(node.id) else { continue }
|
|
let indent = String(repeating: " ", count: depth)
|
|
result.append(Destination(id: node.id, title: indent + node.name))
|
|
walk(node.children ?? [], depth: depth + 1)
|
|
}
|
|
}
|
|
walk(workflow.tree.roots, depth: 0)
|
|
return result
|
|
}
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 12) {
|
|
Text("把「\(workflow.displayName(for: nodeID))」移动到:")
|
|
.font(.callout)
|
|
Picker(
|
|
"目的地",
|
|
selection: Binding(
|
|
get: { destination ?? destinations.first?.id },
|
|
set: { destination = $0 }
|
|
)
|
|
) {
|
|
ForEach(destinations) { target in
|
|
Text(target.title).tag(target.id as NodeID?)
|
|
}
|
|
}
|
|
.pickerStyle(.radioGroup)
|
|
HStack {
|
|
Spacer()
|
|
Button("取消", role: .cancel) { dismiss() }
|
|
Button("移动") { move() }
|
|
.keyboardShortcut(.defaultAction)
|
|
}
|
|
}
|
|
.padding(20)
|
|
.frame(minWidth: 380)
|
|
}
|
|
|
|
private func move() {
|
|
Task {
|
|
do {
|
|
try await workflow.move(nodeID, to: destination)
|
|
dismiss()
|
|
} catch {
|
|
errors.present(error)
|
|
}
|
|
}
|
|
}
|
|
}
|