fix(ios): 创建知识库页面 - 创建后自动返回 + 成功/失败反馈

- 新增 @Environment(\.dismiss) 创建成功后自动返回列表
- 新增 isCreating 加载态,按钮显示 ProgressView 并禁用
- 空名称时按钮灰色不可点击
- 成功 toast "知识库已创建" + 自动 dismiss
- 失败 toast "创建失败,请重试"

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
wangdl 2026-05-27 21:28:40 +08:00
parent 9aa9fd76c7
commit f90f90ee41

View File

@ -1,15 +1,38 @@
import SwiftUI
struct CreateLibraryPage: View {
@State private var name = ""; @State private var desc = ""
@Environment(\.dismiss) private var dismiss
@State private var name = ""; @State private var desc = ""; @State private var isCreating = false
var body: some View {
ZStack { Color.zxBg0.ignoresSafeArea(); VStack(spacing: 0) {
ScrollView { VStack(spacing: 20) {
VStack(alignment: .leading, spacing: 8) { Text("知识库名称").font(.system(size: 12, weight: .semibold)).foregroundColor(Color.zxF035); TextField("例如:机器学习", text: $name).font(.system(size: 15)).tint(Color.zxPurple).padding(.horizontal, 16).frame(height: 52).background(Color.zxFill004).clipShape(RoundedRectangle(cornerRadius: 14)).overlay(RoundedRectangle(cornerRadius: 14).stroke(Color.zxBorder008, lineWidth: 1)) }
VStack(alignment: .leading, spacing: 8) { Text("描述(可选)").font(.system(size: 12, weight: .semibold)).foregroundColor(Color.zxF035); TextField("简单描述这个知识库的内容", text: $desc).font(.system(size: 15)).tint(Color.zxPurple).padding(.horizontal, 16).frame(height: 52).background(Color.zxFill004).clipShape(RoundedRectangle(cornerRadius: 14)).overlay(RoundedRectangle(cornerRadius: 14).stroke(Color.zxBorder008, lineWidth: 1)) }
Button {
Task { _ = try? await KnowledgeBaseService.shared.create(title: name, description: desc.isEmpty ? nil : desc) }
} label: { Text("创建").font(.system(size: 14, weight: .bold)).foregroundColor(.white).frame(maxWidth: .infinity).frame(height: 52).background(ZXGradient.ctaPurple).clipShape(RoundedRectangle(cornerRadius: 16)) }
guard !name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return }
isCreating = true
Task {
let kb = try? await KnowledgeBaseService.shared.create(title: name, description: desc.isEmpty ? nil : desc)
await MainActor.run {
isCreating = false
if kb != nil {
ZXToastManager.shared.success("知识库已创建")
dismiss()
} else {
ZXToastManager.shared.error("创建失败,请重试")
}
}
}
} label: {
HStack(spacing: 8) {
if isCreating { ProgressView().tint(.white) }
Text("创建").font(.system(size: 14, weight: .bold))
}
.foregroundColor(.white).frame(maxWidth: .infinity).frame(height: 52)
.background(name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ? Color.zxHairlineStrong : ZXGradient.ctaPurple)
.clipShape(RoundedRectangle(cornerRadius: 16))
}
.disabled(isCreating || name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
}.padding(.horizontal, 20).padding(.top, 20) }.scrollIndicators(.hidden) }
}.navigationBarTitleDisplayMode(.inline).toolbarBackground(.hidden, for: .navigationBar)}
}