You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
6.2 KiB
160 lines
6.2 KiB
import SwiftUI
|
|
import CoreData
|
|
|
|
struct CreateCodeView: View {
|
|
@Environment(\.dismiss) private var dismiss
|
|
@StateObject private var coreDataManager = CoreDataManager.shared
|
|
|
|
@State private var selectedDataType: DataType = .qrcode
|
|
@State private var selectedBarcodeType: BarcodeType = .ean13
|
|
@State private var selectedQRCodeType: QRCodeType = .text
|
|
@State private var content = ""
|
|
@State private var showingAlert = false
|
|
@State private var alertMessage = ""
|
|
|
|
var body: some View {
|
|
NavigationView {
|
|
Form {
|
|
// 数据类型选择
|
|
Section("数据类型") {
|
|
Picker("数据类型", selection: $selectedDataType) {
|
|
ForEach(DataType.allCases, id: \.self) { type in
|
|
HStack {
|
|
Image(systemName: type.icon)
|
|
Text(type.displayName)
|
|
}
|
|
.tag(type)
|
|
}
|
|
}
|
|
.pickerStyle(SegmentedPickerStyle())
|
|
}
|
|
|
|
// 具体类型选择
|
|
if selectedDataType == .barcode {
|
|
Section("条形码类型") {
|
|
Picker("条形码类型", selection: $selectedBarcodeType) {
|
|
ForEach(BarcodeType.allCases, id: \.self) { type in
|
|
HStack {
|
|
Image(systemName: type.icon)
|
|
Text(type.displayName)
|
|
}
|
|
.tag(type)
|
|
}
|
|
}
|
|
.pickerStyle(WheelPickerStyle())
|
|
}
|
|
} else {
|
|
Section("二维码类型") {
|
|
Picker("二维码类型", selection: $selectedQRCodeType) {
|
|
ForEach(QRCodeType.allCases, id: \.self) { type in
|
|
HStack {
|
|
Image(systemName: type.icon)
|
|
Text(type.displayName)
|
|
}
|
|
.tag(type)
|
|
}
|
|
}
|
|
.pickerStyle(WheelPickerStyle())
|
|
}
|
|
}
|
|
|
|
// 内容输入
|
|
Section("内容") {
|
|
TextField("请输入内容", text: $content)
|
|
.frame(minHeight: 80)
|
|
}
|
|
|
|
// 预览
|
|
if !content.isEmpty {
|
|
Section("预览") {
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
HStack {
|
|
Image(systemName: selectedDataType.icon)
|
|
Text(selectedDataType.displayName)
|
|
Spacer()
|
|
if selectedDataType == .barcode {
|
|
Text(selectedBarcodeType.displayName)
|
|
.font(.caption)
|
|
.padding(.horizontal, 8)
|
|
.padding(.vertical, 2)
|
|
.background(Color.green.opacity(0.1))
|
|
.foregroundColor(.green)
|
|
.cornerRadius(8)
|
|
} else {
|
|
Text(selectedQRCodeType.displayName)
|
|
.font(.caption)
|
|
.padding(.horizontal, 8)
|
|
.padding(.vertical, 2)
|
|
.background(Color.orange.opacity(0.1))
|
|
.foregroundColor(.orange)
|
|
.cornerRadius(8)
|
|
}
|
|
}
|
|
|
|
Text(content)
|
|
.font(.body)
|
|
.foregroundColor(.secondary)
|
|
}
|
|
.padding()
|
|
.background(Color(.systemGray6))
|
|
.cornerRadius(8)
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle("创建\(selectedDataType.displayName)")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar {
|
|
ToolbarItem(placement: .navigationBarLeading) {
|
|
Button("取消") {
|
|
dismiss()
|
|
}
|
|
}
|
|
|
|
ToolbarItem(placement: .navigationBarTrailing) {
|
|
Button("创建") {
|
|
createCode()
|
|
}
|
|
.disabled(content.isEmpty)
|
|
}
|
|
}
|
|
.alert("提示", isPresented: $showingAlert) {
|
|
Button("确定") { }
|
|
} message: {
|
|
Text(alertMessage)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func createCode() {
|
|
guard !content.isEmpty else { return }
|
|
|
|
let context = coreDataManager.container.viewContext
|
|
let historyItem = HistoryItem(context: context)
|
|
|
|
historyItem.id = UUID()
|
|
historyItem.content = content
|
|
historyItem.dataType = selectedDataType.rawValue
|
|
historyItem.dataSource = DataSource.created.rawValue
|
|
historyItem.createdAt = Date()
|
|
historyItem.isFavorite = false
|
|
|
|
if selectedDataType == .barcode {
|
|
historyItem.barcodeType = selectedBarcodeType.rawValue
|
|
} else {
|
|
historyItem.qrCodeType = selectedQRCodeType.rawValue
|
|
}
|
|
|
|
coreDataManager.addHistoryItem(historyItem)
|
|
alertMessage = "\(selectedDataType.displayName)创建成功!"
|
|
showingAlert = true
|
|
|
|
// 延迟关闭
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
CreateCodeView()
|
|
} |