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
|
|
|
|
@NSManaged public private(set) var uses: Int
|
|
|
|
@NSManaged public private(set) var accounts: Int
|
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-02-02 07:10:25 +01:00
|
|
|
public extension History {
|
|
|
|
struct Property {
|
2021-02-01 11:05:34 +01:00
|
|
|
public let day: Date
|
|
|
|
public let uses: Int
|
|
|
|
public let accounts: Int
|
2021-02-02 07:10:25 +01:00
|
|
|
|
2021-02-01 11:05:34 +01:00
|
|
|
public init(day: Date, uses: Int, accounts: Int) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|