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.
147 lines
4.9 KiB
147 lines
4.9 KiB
import Foundation
|
|
import CoreData
|
|
import SwiftUI
|
|
import Combine
|
|
|
|
class CoreDataManager: ObservableObject {
|
|
static let shared = CoreDataManager()
|
|
|
|
let container: NSPersistentContainer
|
|
|
|
init() {
|
|
container = NSPersistentContainer(name: "MyQrCode")
|
|
|
|
container.loadPersistentStores { description, error in
|
|
if let error = error {
|
|
print("Core Data 加载失败: \(error.localizedDescription)")
|
|
}
|
|
}
|
|
|
|
// 启用自动合并更改
|
|
container.viewContext.automaticallyMergesChangesFromParent = true
|
|
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
|
|
}
|
|
|
|
// 保存上下文
|
|
func save() {
|
|
let context = container.viewContext
|
|
|
|
if context.hasChanges {
|
|
do {
|
|
try context.save()
|
|
} catch {
|
|
print("保存失败: \(error.localizedDescription)")
|
|
}
|
|
}
|
|
}
|
|
|
|
// 获取历史记录
|
|
func fetchHistoryItems() -> [HistoryItem] {
|
|
let request: NSFetchRequest<HistoryItem> = HistoryItem.fetchRequest()
|
|
request.sortDescriptors = [NSSortDescriptor(keyPath: \HistoryItem.createdAt, ascending: false)]
|
|
|
|
do {
|
|
return try container.viewContext.fetch(request)
|
|
} catch {
|
|
print("获取历史记录失败: \(error.localizedDescription)")
|
|
return []
|
|
}
|
|
}
|
|
|
|
// 添加历史记录
|
|
func addHistoryItem(_ item: HistoryItem) {
|
|
container.viewContext.insert(item)
|
|
save()
|
|
}
|
|
|
|
// 删除历史记录
|
|
func deleteHistoryItem(_ item: HistoryItem) {
|
|
container.viewContext.delete(item)
|
|
save()
|
|
}
|
|
|
|
// 清空所有历史记录
|
|
func clearAllHistory() {
|
|
let request: NSFetchRequest<NSFetchRequestResult> = HistoryItem.fetchRequest()
|
|
let deleteRequest = NSBatchDeleteRequest(fetchRequest: request)
|
|
|
|
do {
|
|
try container.viewContext.execute(deleteRequest)
|
|
save()
|
|
} catch {
|
|
print("清空历史记录失败: \(error.localizedDescription)")
|
|
}
|
|
}
|
|
|
|
// 搜索历史记录
|
|
func searchHistoryItems(query: String) -> [HistoryItem] {
|
|
let request: NSFetchRequest<HistoryItem> = HistoryItem.fetchRequest()
|
|
|
|
if !query.isEmpty {
|
|
let contentPredicate = NSPredicate(format: "content CONTAINS[cd] %@", query)
|
|
let barcodeTypePredicate = NSPredicate(format: "barcodeType CONTAINS[cd] %@", query)
|
|
let qrCodeTypePredicate = NSPredicate(format: "qrCodeType CONTAINS[cd] %@", query)
|
|
|
|
let compoundPredicate = NSCompoundPredicate(
|
|
orPredicateWithSubpredicates: [
|
|
contentPredicate,
|
|
barcodeTypePredicate,
|
|
qrCodeTypePredicate
|
|
]
|
|
)
|
|
|
|
request.predicate = compoundPredicate
|
|
}
|
|
|
|
request.sortDescriptors = [NSSortDescriptor(keyPath: \HistoryItem.createdAt, ascending: false)]
|
|
|
|
do {
|
|
return try container.viewContext.fetch(request)
|
|
} catch {
|
|
print("搜索历史记录失败: \(error.localizedDescription)")
|
|
return []
|
|
}
|
|
}
|
|
|
|
// 按类型过滤
|
|
func filterByType(_ type: DataType) -> [HistoryItem] {
|
|
let request: NSFetchRequest<HistoryItem> = HistoryItem.fetchRequest()
|
|
request.predicate = NSPredicate(format: "dataType == %@", type.rawValue)
|
|
request.sortDescriptors = [NSSortDescriptor(keyPath: \HistoryItem.createdAt, ascending: false)]
|
|
|
|
do {
|
|
return try container.viewContext.fetch(request)
|
|
} catch {
|
|
print("按类型过滤失败: \(error.localizedDescription)")
|
|
return []
|
|
}
|
|
}
|
|
|
|
// 按来源过滤
|
|
func filterBySource(_ source: DataSource) -> [HistoryItem] {
|
|
let request: NSFetchRequest<HistoryItem> = HistoryItem.fetchRequest()
|
|
request.predicate = NSPredicate(format: "dataSource == %@", source.rawValue)
|
|
request.sortDescriptors = [NSSortDescriptor(keyPath: \HistoryItem.createdAt, ascending: false)]
|
|
|
|
do {
|
|
return try container.viewContext.fetch(request)
|
|
} catch {
|
|
print("按来源过滤失败: \(error.localizedDescription)")
|
|
return []
|
|
}
|
|
}
|
|
|
|
// 获取收藏项目
|
|
func getFavoriteItems() -> [HistoryItem] {
|
|
let request: NSFetchRequest<HistoryItem> = HistoryItem.fetchRequest()
|
|
request.predicate = NSPredicate(format: "isFavorite == YES")
|
|
request.sortDescriptors = [NSSortDescriptor(keyPath: \HistoryItem.createdAt, ascending: false)]
|
|
|
|
do {
|
|
return try container.viewContext.fetch(request)
|
|
} catch {
|
|
print("获取收藏项目失败: \(error.localizedDescription)")
|
|
return []
|
|
}
|
|
}
|
|
} |