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.

357 lines
12 KiB

import SwiftUI
import CoreData
struct BarcodeDetailView: View {
let historyItem: HistoryItem
@EnvironmentObject var coreDataManager: CoreDataManager
@State private var barcodeImage: UIImage?
@State private var showingShareSheet = false
@State private var showingAlert = false
@State private var alertMessage = ""
var body: some View {
ScrollView {
VStack(spacing: 20) {
//
barcodeImageView
//
barcodeTypeSection
//
barcodeContentSection
//
originalContentSection
//
actionButtonsSection
}
.padding()
}
.navigationTitle("barcode_detail".localized)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
showingShareSheet = true
}) {
Image(systemName: "square.and.arrow.up")
}
.disabled(barcodeImage == nil)
}
}
.onAppear {
generateBarcodeImage()
}
.sheet(isPresented: $showingShareSheet) {
if let barcodeImage = barcodeImage {
ShareSheet(activityItems: [barcodeImage])
} else {
ShareSheet(activityItems: [historyItem.content ?? ""])
}
}
.alert("tip".localized, isPresented: $showingAlert) {
Button("confirm".localized) { }
} message: {
Text(alertMessage)
}
}
// MARK: -
private var barcodeImageView: some View {
VStack(spacing: 16) {
if let barcodeImage = barcodeImage {
Image(uiImage: barcodeImage)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(height: 120)
.cornerRadius(12)
.shadow(radius: 8)
} else {
RoundedRectangle(cornerRadius: 12)
.fill(Color.gray.opacity(0.3))
.frame(height: 120)
.overlay(
ProgressView()
.scaleEffect(1.5)
)
}
Text("scan_this_barcode".localized)
.font(.caption)
.foregroundColor(.secondary)
}
}
// MARK: -
private var barcodeTypeSection: some View {
VStack(alignment: .leading, spacing: 12) {
HStack {
Image(systemName: "barcode")
.font(.title2)
.foregroundColor(.green)
Text("barcode_type".localized)
.font(.headline)
Spacer()
}
if let barcodeTypeString = historyItem.barcodeType {
HStack {
Image(systemName: getBarcodeIcon(for: barcodeTypeString))
.font(.title3)
.foregroundColor(.green)
Text(barcodeTypeString)
.font(.title3)
.fontWeight(.medium)
Spacer()
}
.padding()
.background(Color.green.opacity(0.1))
.cornerRadius(8)
}
}
.padding()
.background(Color(.systemBackground))
.cornerRadius(12)
.shadow(radius: 2)
}
// MARK: -
private var barcodeContentSection: some View {
VStack(alignment: .leading, spacing: 12) {
HStack {
Image(systemName: "info.circle")
.font(.title2)
.foregroundColor(.blue)
Text("barcode_content".localized)
.font(.headline)
Spacer()
}
if let content = historyItem.content {
VStack(alignment: .leading, spacing: 8) {
HStack {
Image(systemName: "number")
.font(.title3)
.foregroundColor(.blue)
Text(String(format: "content_length".localized, content.count))
.font(.title3)
.fontWeight(.medium)
Spacer()
}
Text("data_content".localized)
.font(.subheadline)
.foregroundColor(.secondary)
.padding(.top, 4)
}
.padding()
.background(Color.blue.opacity(0.1))
.cornerRadius(8)
}
}
.padding()
.background(Color(.systemBackground))
.cornerRadius(12)
.shadow(radius: 2)
}
// MARK: -
private var originalContentSection: some View {
VStack(alignment: .leading, spacing: 12) {
HStack {
Image(systemName: "doc.text")
.font(.title2)
.foregroundColor(.purple)
Text("original_content".localized)
.font(.headline)
Spacer()
}
if let content = historyItem.content {
ScrollView {
Text(content)
.font(.system(.body, design: .monospaced))
.foregroundColor(.secondary)
.multilineTextAlignment(.leading)
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
}
.frame(maxHeight: 200)
.background(Color.purple.opacity(0.1))
.cornerRadius(8)
}
}
.padding()
.background(Color(.systemBackground))
.cornerRadius(12)
.shadow(radius: 2)
}
// MARK: -
private var actionButtonsSection: some View {
VStack(spacing: 12) {
//
Button(action: toggleFavorite) {
HStack {
Image(systemName: historyItem.isFavorite ? "heart.fill" : "heart")
.foregroundColor(historyItem.isFavorite ? .red : .gray)
Text(historyItem.isFavorite ? "unfavorite".localized : "favorite".localized)
.fontWeight(.medium)
}
.frame(maxWidth: .infinity)
.padding()
.background(historyItem.isFavorite ? Color.red.opacity(0.1) : Color.gray.opacity(0.1))
.foregroundColor(historyItem.isFavorite ? .red : .gray)
.cornerRadius(10)
}
//
Button(action: copyContent) {
HStack {
Image(systemName: "doc.on.doc")
.foregroundColor(.blue)
Text("copy_content".localized)
.fontWeight(.medium)
}
.frame(maxWidth: .infinity)
.padding()
.background(Color.blue.opacity(0.1))
.foregroundColor(.blue)
.cornerRadius(10)
}
//
if barcodeImage != nil {
Button(action: {
showingShareSheet = true
}) {
HStack {
Image(systemName: "photo")
.foregroundColor(.green)
Text("share_barcode_image".localized)
.fontWeight(.medium)
}
.frame(maxWidth: .infinity)
.padding()
.background(Color.green.opacity(0.1))
.foregroundColor(.green)
.cornerRadius(10)
}
}
}
.padding()
.background(Color(.systemBackground))
.cornerRadius(12)
.shadow(radius: 2)
}
// MARK: -
private func generateBarcodeImage() {
guard let content = historyItem.content else { return }
// 使
let barcodeType = historyItem.barcodeType ?? ""
let size = CGSize(width: 300, height: 120)
if let image = BarcodeGenerator.shared.generateBarcode(from: content, type: barcodeType, size: size) {
self.barcodeImage = image
}
}
// MARK: -
private func getBarcodeIcon(for type: String) -> String {
return BarcodeGenerator.shared.getBarcodeIcon(for: type)
}
// MARK: -
private func toggleFavorite() {
historyItem.isFavorite.toggle()
coreDataManager.save()
let message = historyItem.isFavorite ? "added_to_favorites".localized : "removed_from_favorites".localized
alertMessage = message
showingAlert = true
}
// MARK: -
private func copyContent() {
if let content = historyItem.content {
UIPasteboard.general.string = content
alertMessage = "content_copied_to_clipboard".localized
showingAlert = true
}
}
}
// MARK: -
struct BarcodeShareSheet: UIViewControllerRepresentable {
let activityItems: [Any]
func makeUIViewController(context: Context) -> UIActivityViewController {
let controller = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
return controller
}
func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {}
}
#Preview("EAN-13") {
let ctx = PreviewData.context
let item = PreviewData.ean13Sample(in: ctx)
return NavigationView { BarcodeDetailView(historyItem: item) }
}
#Preview("Code 128") {
let ctx = PreviewData.context
let item = PreviewData.code128Sample(in: ctx)
return NavigationView { BarcodeDetailView(historyItem: item) }
}
// MARK: - Preview Data
private enum PreviewData {
static let context: NSManagedObjectContext = {
let container = NSPersistentContainer(name: "MyQrCode")
let description = NSPersistentStoreDescription()
description.type = NSInMemoryStoreType
container.persistentStoreDescriptions = [description]
container.loadPersistentStores { _, _ in }
return container.viewContext
}()
private static func makeBaseItem(in context: NSManagedObjectContext, content: String, barcodeType: BarcodeType, favorite: Bool = false) -> HistoryItem {
let item = HistoryItem(context: context)
item.id = UUID()
item.content = content
item.dataType = DataType.barcode.rawValue
item.dataSource = DataSource.created.rawValue
item.createdAt = Date()
item.isFavorite = favorite
item.barcodeType = barcodeType.rawValue
return item
}
static func ean13Sample(in context: NSManagedObjectContext) -> HistoryItem {
let content = "1234567890128"
return makeBaseItem(in: context, content: content, barcodeType: .ean13, favorite: true)
}
static func code128Sample(in context: NSManagedObjectContext) -> HistoryItem {
let content = "ABC123"
return makeBaseItem(in: context, content: content, barcodeType: .code128)
}
}