-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
2,148 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// | ||
// EvolutionCommandEntity+Fetch.swift | ||
// OnlySwitch | ||
// | ||
// Created by Jacklandrin on 2023/6/4. | ||
// | ||
|
||
import CoreData | ||
import Foundation | ||
|
||
extension EvolutionCommandEntity { | ||
static var defaultFetchRequest:NSFetchRequest<EvolutionCommandEntity> { | ||
let request:NSFetchRequest<EvolutionCommandEntity> = EvolutionCommandEntity.fetchRequest() | ||
request.sortDescriptors = [NSSortDescriptor(keyPath: \EvolutionCommandEntity.timestamp, ascending: true)] | ||
print("fetched the evolution command") | ||
return request | ||
} | ||
|
||
static func fetchResult() throws -> [EvolutionCommandEntity] { | ||
try PersistenceController | ||
.shared | ||
.container | ||
.viewContext | ||
.fetch(EvolutionCommandEntity.defaultFetchRequest) | ||
} | ||
|
||
static func fetchRequest(by id: UUID) throws -> EvolutionCommandEntity? { | ||
let predicate = NSPredicate( | ||
format: "%K = %@", "id" , "\(id)" | ||
) | ||
|
||
let request = EvolutionCommandEntity.fetchRequest() | ||
request.sortDescriptors = [NSSortDescriptor(keyPath: \EvolutionCommandEntity.timestamp, ascending: true)] | ||
request.predicate = predicate | ||
|
||
return try PersistenceController | ||
.shared | ||
.container | ||
.viewContext | ||
.fetch(request) | ||
.first | ||
} | ||
|
||
static func addItem(item: EvolutionItem) throws { | ||
let context = PersistenceController | ||
.shared | ||
.container | ||
.viewContext | ||
|
||
let entity = try EvolutionCommandEntity.fetchRequest(by: item.id) ?? EvolutionCommandEntity(context: context) | ||
|
||
if item.controlType == .Button { | ||
guard | ||
let singleCommand = item.singleCommand?.commandString, | ||
!singleCommand.isEmpty, | ||
let singleCommandTypeStr = item.singleCommand?.executeType.rawValue, | ||
item.singleCommand?.debugStatus == .success else { | ||
context.reset() | ||
throw EvolutionError.wrongCommand | ||
} | ||
entity.singleCommand = singleCommand | ||
entity.singleCommandType = singleCommandTypeStr | ||
} else { | ||
guard | ||
let onCommand = item.onCommand?.commandString, | ||
!onCommand.isEmpty, | ||
let onCommandTypeStr = item.onCommand?.executeType.rawValue, | ||
item.onCommand?.debugStatus == .success else { | ||
context.reset() | ||
throw EvolutionError.wrongCommand | ||
} | ||
entity.turnOnCommand = onCommand | ||
entity.turnOnCommandType = onCommandTypeStr | ||
|
||
guard | ||
let offCommand = item.offCommand?.commandString, | ||
!offCommand.isEmpty, | ||
let offCommandTypeStr = item.offCommand?.executeType.rawValue, | ||
item.offCommand?.debugStatus == .success else { | ||
context.reset() | ||
throw EvolutionError.wrongCommand | ||
} | ||
|
||
entity.turnOffCommand = offCommand | ||
entity.turnOffCommandType = offCommandTypeStr | ||
|
||
guard | ||
let statusCommand = item.statusCommand?.commandString, | ||
!statusCommand.isEmpty, | ||
let statusCommandTypeStr = item.statusCommand?.executeType.rawValue, | ||
let trueCondition = item.statusCommand?.trueCondition else { | ||
context.reset() | ||
throw EvolutionError.wrongCommand | ||
} | ||
entity.statusCommand = statusCommand | ||
entity.statusCommandType = statusCommandTypeStr | ||
entity.trueCondition = trueCondition | ||
} | ||
|
||
entity.name = item.name | ||
entity.itemType = item.controlType.rawValue | ||
entity.timestamp = Date() | ||
entity.id = item.id | ||
|
||
|
||
try context.save() | ||
} | ||
|
||
static func removeItem(by id: UUID) throws { | ||
let context = PersistenceController | ||
.shared | ||
.container | ||
.viewContext | ||
|
||
guard let entity = try fetchRequest(by: id) else { | ||
throw EvolutionError.deleteFailed | ||
} | ||
|
||
context.delete(entity) | ||
} | ||
} |
File renamed without changes.
24 changes: 24 additions & 0 deletions
24
OnlySwitch/CoreData/OnlySwitch.xcdatamodeld/OnlySwitch.xcdatamodel/contents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="21754" systemVersion="22F66" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithCloudKit="YES" userDefinedModelVersionIdentifier=""> | ||
<entity name="EvolutionCommandEntity" representedClassName="EvolutionCommandEntity" syncable="YES" codeGenerationType="class"> | ||
<attribute name="id" optional="YES" attributeType="UUID" usesScalarValueType="NO"/> | ||
<attribute name="itemType" optional="YES" attributeType="String"/> | ||
<attribute name="name" optional="YES" attributeType="String"/> | ||
<attribute name="singleCommand" optional="YES" attributeType="String"/> | ||
<attribute name="singleCommandType" optional="YES" attributeType="String"/> | ||
<attribute name="statusCommand" optional="YES" attributeType="String"/> | ||
<attribute name="statusCommandType" optional="YES" attributeType="String"/> | ||
<attribute name="timestamp" optional="YES" attributeType="Date" usesScalarValueType="NO"/> | ||
<attribute name="trueCondition" optional="YES" attributeType="String"/> | ||
<attribute name="turnOffCommand" optional="YES" attributeType="String"/> | ||
<attribute name="turnOffCommandType" optional="YES" attributeType="String"/> | ||
<attribute name="turnOnCommand" optional="YES" attributeType="String"/> | ||
<attribute name="turnOnCommandType" optional="YES" attributeType="String"/> | ||
</entity> | ||
<entity name="RadioStations" representedClassName="RadioStations" syncable="YES" codeGenerationType="class"> | ||
<attribute name="id" optional="YES" attributeType="UUID" usesScalarValueType="NO"/> | ||
<attribute name="timestamp" optional="YES" attributeType="Date" usesScalarValueType="NO"/> | ||
<attribute name="title" optional="YES" attributeType="String"/> | ||
<attribute name="url" optional="YES" attributeType="String"/> | ||
</entity> | ||
</model> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// Persistence.swift | ||
// Test | ||
// | ||
// Created by Jacklandrin on 2021/12/9. | ||
// | ||
|
||
import CoreData | ||
|
||
struct PersistenceController { | ||
static let shared = PersistenceController() | ||
|
||
let container: NSPersistentCloudKitContainer | ||
|
||
init(inMemory: Bool = false) { | ||
container = NSPersistentCloudKitContainer(name: "OnlySwitch") | ||
if inMemory { | ||
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null") | ||
} | ||
container.loadPersistentStores(completionHandler: { (storeDescription, error) in | ||
if let error = error as NSError? { | ||
// Replace this implementation with code to handle the error appropriately. | ||
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. | ||
|
||
/* | ||
Typical reasons for an error here include: | ||
* The parent directory does not exist, cannot be created, or disallows writing. | ||
* The persistent store is not accessible, due to permissions or data protection when the device is locked. | ||
* The device is out of space. | ||
* The store could not be migrated to the current model version. | ||
Check the error message to determine what the actual problem was. | ||
*/ | ||
fatalError("Unresolved error \(error), \(error.userInfo)") | ||
} | ||
}) | ||
} | ||
|
||
func saveContext() { | ||
let context = self.container.viewContext | ||
if context.hasChanges { | ||
do { | ||
try context.save() | ||
} catch { | ||
let nserror = error as NSError | ||
fatalError("Unresolved error \(nserror), \(nserror.userInfo)") | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.