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.
MyQRCode/MyQrCode/Views/BarcodeValidationInfoView.s...

124 lines
4.5 KiB

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()
}