2021-02-01 11:05:34 +01:00
|
|
|
//
|
|
|
|
// History.swift
|
|
|
|
// CoreDataStack
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/2/1.
|
|
|
|
//
|
|
|
|
|
|
|
|
import CoreData
|
2021-02-02 07:10:25 +01:00
|
|
|
import Foundation
|
2021-02-01 11:05:34 +01:00
|
|
|
|
2021-02-02 07:10:25 +01:00
|
|
|
public final class History: NSManagedObject {
|
|
|
|
public typealias ID = UUID
|
2021-02-01 11:05:34 +01:00
|
|
|
@NSManaged public private(set) var identifier: ID
|
2021-02-02 07:10:25 +01:00
|
|
|
@NSManaged public private(set) var createAt: Date
|
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
@NSManaged public private(set) var day: Date
|
2021-04-01 05:49:38 +02:00
|
|
|
@NSManaged public private(set) var uses: String
|
|
|
|
@NSManaged public private(set) var accounts: String
|
2021-02-02 07:10:25 +01:00
|
|
|
|
|
|
|
// many-to-one relationship
|
|
|
|
@NSManaged public private(set) var tag: Tag
|
2021-02-01 11:05:34 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:10:25 +01:00
|
|
|
public extension History {
|
|
|
|
override func awakeFromInsert() {
|
|
|
|
super.awakeFromInsert()
|
2021-03-06 06:04:30 +01:00
|
|
|
setPrimitiveValue(UUID(), forKey: #keyPath(History.identifier))
|
2021-02-02 07:10:25 +01:00
|
|
|
}
|
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
@discardableResult
|
2021-02-02 07:10:25 +01:00
|
|
|
static func insert(
|
2021-02-01 11:05:34 +01:00
|
|
|
into context: NSManagedObjectContext,
|
2021-02-02 07:10:25 +01:00
|
|
|
property: Property
|
2021-02-01 11:05:34 +01:00
|
|
|
) -> History {
|
2021-02-02 07:10:25 +01:00
|
|
|
let history: History = context.insertObject()
|
|
|
|
history.day = property.day
|
|
|
|
history.uses = property.uses
|
|
|
|
history.accounts = property.accounts
|
2021-02-01 11:05:34 +01:00
|
|
|
return history
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 06:22:05 +02:00
|
|
|
public extension History {
|
|
|
|
func update(day: Date) {
|
|
|
|
if self.day != day {
|
|
|
|
self.day = day
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func update(uses: String) {
|
|
|
|
if self.uses != uses {
|
|
|
|
self.uses = uses
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func update(accounts: String) {
|
|
|
|
if self.accounts != accounts {
|
|
|
|
self.accounts = accounts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:10:25 +01:00
|
|
|
public extension History {
|
|
|
|
struct Property {
|
2021-02-01 11:05:34 +01:00
|
|
|
public let day: Date
|
2021-04-01 05:49:38 +02:00
|
|
|
public let uses: String
|
|
|
|
public let accounts: String
|
2021-02-02 07:10:25 +01:00
|
|
|
|
2021-04-01 05:49:38 +02:00
|
|
|
public init(day: Date, uses: String, accounts: String) {
|
2021-02-01 11:05:34 +01:00
|
|
|
self.day = day
|
|
|
|
self.uses = uses
|
|
|
|
self.accounts = accounts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 07:10:25 +01:00
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
extension History: Managed {
|
|
|
|
public static var defaultSortDescriptors: [NSSortDescriptor] {
|
2021-02-02 07:10:25 +01:00
|
|
|
return [NSSortDescriptor(keyPath: \History.createAt, ascending: false)]
|
2021-02-01 11:05:34 +01:00
|
|
|
}
|
|
|
|
}
|