diff --git a/AIStudyApp/AIStudyApp/Features/Library/LibrarySubpages.swift b/AIStudyApp/AIStudyApp/Features/Library/LibrarySubpages.swift index 5c1859b..bd63eb7 100644 --- a/AIStudyApp/AIStudyApp/Features/Library/LibrarySubpages.swift +++ b/AIStudyApp/AIStudyApp/Features/Library/LibrarySubpages.swift @@ -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)} }