Refactor CreateCodeView to enhance barcode input validation and user experience; implement dynamic input handling, visual feedback, and improved layout for barcode type selection and content entry.
parent
dab1808934
commit
f048426f6b
@ -0,0 +1,95 @@
|
|||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
// MARK: - 条形码字符提示组件
|
||||||
|
struct BarcodeCharacterHintView: View {
|
||||||
|
let barcodeType: BarcodeType
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
VStack(alignment: .leading, spacing: 8) {
|
||||||
|
Text("字符类型:")
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
|
||||||
|
LazyVGrid(columns: [
|
||||||
|
GridItem(.adaptive(minimum: 80), spacing: 8)
|
||||||
|
], spacing: 6) {
|
||||||
|
ForEach(getCharacterTypes(), id: \.self) { charType in
|
||||||
|
HStack(spacing: 4) {
|
||||||
|
Image(systemName: charType.icon)
|
||||||
|
.font(.caption2)
|
||||||
|
.foregroundColor(charType.color)
|
||||||
|
|
||||||
|
Text(charType.name)
|
||||||
|
.font(.caption2)
|
||||||
|
.foregroundColor(charType.color)
|
||||||
|
}
|
||||||
|
.padding(.horizontal, 8)
|
||||||
|
.padding(.vertical, 4)
|
||||||
|
.background(
|
||||||
|
RoundedRectangle(cornerRadius: 6)
|
||||||
|
.fill(charType.color.opacity(0.1))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func getCharacterTypes() -> [CharacterType] {
|
||||||
|
switch barcodeType {
|
||||||
|
case .ean13, .ean8, .upce, .itf14:
|
||||||
|
return [
|
||||||
|
CharacterType(name: "数字", icon: "number", color: .blue)
|
||||||
|
]
|
||||||
|
|
||||||
|
case .code39:
|
||||||
|
return [
|
||||||
|
CharacterType(name: "字母", icon: "character", color: .green),
|
||||||
|
CharacterType(name: "数字", icon: "number", color: .blue),
|
||||||
|
CharacterType(name: "特殊字符", icon: "textformat", color: .orange)
|
||||||
|
]
|
||||||
|
|
||||||
|
case .code128:
|
||||||
|
return [
|
||||||
|
CharacterType(name: "字母", icon: "character", color: .green),
|
||||||
|
CharacterType(name: "数字", icon: "number", color: .blue),
|
||||||
|
CharacterType(name: "符号", icon: "textformat", color: .purple),
|
||||||
|
CharacterType(name: "控制字符", icon: "command", color: .red)
|
||||||
|
]
|
||||||
|
|
||||||
|
case .pdf417:
|
||||||
|
return [
|
||||||
|
CharacterType(name: "字母", icon: "character", color: .green),
|
||||||
|
CharacterType(name: "数字", icon: "number", color: .blue),
|
||||||
|
CharacterType(name: "符号", icon: "textformat", color: .purple),
|
||||||
|
CharacterType(name: "所有ASCII", icon: "globe", color: .indigo)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - 字符类型结构
|
||||||
|
struct CharacterType: Hashable {
|
||||||
|
let name: String
|
||||||
|
let icon: String
|
||||||
|
let color: Color
|
||||||
|
|
||||||
|
// 实现 Hashable 协议
|
||||||
|
func hash(into hasher: inout Hasher) {
|
||||||
|
hasher.combine(name)
|
||||||
|
hasher.combine(icon)
|
||||||
|
}
|
||||||
|
|
||||||
|
static func == (lhs: CharacterType, rhs: CharacterType) -> Bool {
|
||||||
|
return lhs.name == rhs.name && lhs.icon == rhs.icon
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#Preview {
|
||||||
|
VStack(spacing: 20) {
|
||||||
|
BarcodeCharacterHintView(barcodeType: .ean13)
|
||||||
|
BarcodeCharacterHintView(barcodeType: .code39)
|
||||||
|
BarcodeCharacterHintView(barcodeType: .code128)
|
||||||
|
BarcodeCharacterHintView(barcodeType: .pdf417)
|
||||||
|
}
|
||||||
|
.padding()
|
||||||
|
}
|
@ -0,0 +1,124 @@
|
|||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
// MARK: - 条形码验证信息显示组件
|
||||||
|
struct BarcodeValidationInfoView: View {
|
||||||
|
let validationResult: BarcodeValidator.ValidationResult?
|
||||||
|
let barcodeType: BarcodeType
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
VStack(alignment: .leading, spacing: 8) {
|
||||||
|
if let result = validationResult {
|
||||||
|
// 验证状态指示器
|
||||||
|
HStack(spacing: 8) {
|
||||||
|
Image(systemName: result.isValid ? "checkmark.circle.fill" : "xmark.circle.fill")
|
||||||
|
.foregroundColor(result.isValid ? .green : .red)
|
||||||
|
|
||||||
|
Text(result.isValid ? "格式正确" : "格式错误")
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(result.isValid ? .green : .red)
|
||||||
|
.fontWeight(.medium)
|
||||||
|
|
||||||
|
Spacer()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 错误信息
|
||||||
|
if let errorMessage = result.errorMessage {
|
||||||
|
Text(errorMessage)
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.red)
|
||||||
|
.padding(.leading, 24)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 格式要求信息
|
||||||
|
VStack(alignment: .leading, spacing: 4) {
|
||||||
|
if let expectedLength = result.expectedLength {
|
||||||
|
HStack {
|
||||||
|
Image(systemName: "number")
|
||||||
|
.foregroundColor(.blue)
|
||||||
|
.font(.caption)
|
||||||
|
Text("长度要求: \(expectedLength) 位")
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.blue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let allowedCharacters = result.allowedCharacters {
|
||||||
|
HStack {
|
||||||
|
Image(systemName: "character")
|
||||||
|
.foregroundColor(.blue)
|
||||||
|
.font(.caption)
|
||||||
|
Text("允许字符: \(allowedCharacters)")
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.blue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.padding(.leading, 24)
|
||||||
|
|
||||||
|
// 格式化后的内容
|
||||||
|
if result.isValid && result.formattedContent != "" {
|
||||||
|
HStack {
|
||||||
|
Image(systemName: "textformat")
|
||||||
|
.foregroundColor(.green)
|
||||||
|
.font(.caption)
|
||||||
|
Text("格式化: \(result.formattedContent)")
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.green)
|
||||||
|
.fontWeight(.medium)
|
||||||
|
}
|
||||||
|
.padding(.leading, 24)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 默认提示信息
|
||||||
|
HStack {
|
||||||
|
Image(systemName: "info.circle")
|
||||||
|
.foregroundColor(.blue)
|
||||||
|
.font(.caption)
|
||||||
|
Text("请输入符合 \(barcodeType.displayName) 格式的内容")
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.blue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.padding(12)
|
||||||
|
.background(
|
||||||
|
RoundedRectangle(cornerRadius: 8)
|
||||||
|
.fill(Color(.systemGray6))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#Preview {
|
||||||
|
VStack(spacing: 20) {
|
||||||
|
// 有效格式示例
|
||||||
|
BarcodeValidationInfoView(
|
||||||
|
validationResult: BarcodeValidator.ValidationResult(
|
||||||
|
isValid: true,
|
||||||
|
formattedContent: "123 4567 8901 2",
|
||||||
|
errorMessage: nil,
|
||||||
|
expectedLength: 13,
|
||||||
|
allowedCharacters: "数字 (0-9)"
|
||||||
|
),
|
||||||
|
barcodeType: .ean13
|
||||||
|
)
|
||||||
|
|
||||||
|
// 无效格式示例
|
||||||
|
BarcodeValidationInfoView(
|
||||||
|
validationResult: BarcodeValidator.ValidationResult(
|
||||||
|
isValid: false,
|
||||||
|
formattedContent: "12345",
|
||||||
|
errorMessage: "EAN-13必须是13位数字",
|
||||||
|
expectedLength: 13,
|
||||||
|
allowedCharacters: "数字 (0-9)"
|
||||||
|
),
|
||||||
|
barcodeType: .ean13
|
||||||
|
)
|
||||||
|
|
||||||
|
// 默认状态示例
|
||||||
|
BarcodeValidationInfoView(
|
||||||
|
validationResult: nil,
|
||||||
|
barcodeType: .code39
|
||||||
|
)
|
||||||
|
}
|
||||||
|
.padding()
|
||||||
|
}
|
Loading…
Reference in new issue