2021-03-02 08:51:16 +01:00
|
|
|
//
|
|
|
|
// Poll.swift
|
|
|
|
// CoreDataStack
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-2.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import CoreData
|
|
|
|
|
|
|
|
public final class Poll: NSManagedObject {
|
|
|
|
public typealias ID = String
|
|
|
|
|
|
|
|
@NSManaged public private(set) var id: ID
|
|
|
|
@NSManaged public private(set) var expiresAt: Date?
|
|
|
|
@NSManaged public private(set) var expired: Bool
|
|
|
|
@NSManaged public private(set) var multiple: Bool
|
|
|
|
@NSManaged public private(set) var votesCount: NSNumber
|
|
|
|
@NSManaged public private(set) var votersCount: NSNumber?
|
|
|
|
|
|
|
|
@NSManaged public private(set) var createdAt: Date
|
|
|
|
@NSManaged public private(set) var updatedAt: Date
|
|
|
|
|
|
|
|
// one-to-one relationship
|
2021-04-01 08:39:15 +02:00
|
|
|
@NSManaged public private(set) var status: Status
|
2021-03-02 08:51:16 +01:00
|
|
|
|
|
|
|
// one-to-many relationship
|
|
|
|
@NSManaged public private(set) var options: Set<PollOption>
|
2021-03-03 12:34:29 +01:00
|
|
|
|
|
|
|
// many-to-many relationship
|
|
|
|
@NSManaged public private(set) var votedBy: Set<MastodonUser>?
|
2021-03-02 08:51:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension Poll {
|
|
|
|
|
|
|
|
public override func awakeFromInsert() {
|
|
|
|
super.awakeFromInsert()
|
2021-03-06 06:04:30 +01:00
|
|
|
setPrimitiveValue(Date(), forKey: #keyPath(Poll.createdAt))
|
2021-03-02 08:51:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
public static func insert(
|
|
|
|
into context: NSManagedObjectContext,
|
|
|
|
property: Property,
|
2021-03-03 12:34:29 +01:00
|
|
|
votedBy: MastodonUser?,
|
2021-03-02 08:51:16 +01:00
|
|
|
options: [PollOption]
|
|
|
|
) -> Poll {
|
|
|
|
let poll: Poll = context.insertObject()
|
|
|
|
|
|
|
|
poll.id = property.id
|
|
|
|
poll.expiresAt = property.expiresAt
|
|
|
|
poll.expired = property.expired
|
|
|
|
poll.multiple = property.multiple
|
|
|
|
poll.votesCount = property.votesCount
|
|
|
|
poll.votersCount = property.votersCount
|
|
|
|
|
2021-03-03 12:34:29 +01:00
|
|
|
|
2021-03-02 08:51:16 +01:00
|
|
|
poll.updatedAt = property.networkDate
|
2021-03-03 12:34:29 +01:00
|
|
|
|
|
|
|
if let votedBy = votedBy {
|
|
|
|
poll.mutableSetValue(forKey: #keyPath(Poll.votedBy)).add(votedBy)
|
|
|
|
}
|
2021-03-02 08:51:16 +01:00
|
|
|
poll.mutableSetValue(forKey: #keyPath(Poll.options)).addObjects(from: options)
|
|
|
|
|
|
|
|
return poll
|
|
|
|
}
|
|
|
|
|
2021-03-03 09:12:48 +01:00
|
|
|
public func update(expiresAt: Date?) {
|
|
|
|
if self.expiresAt != expiresAt {
|
|
|
|
self.expiresAt = expiresAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func update(expired: Bool) {
|
|
|
|
if self.expired != expired {
|
|
|
|
self.expired = expired
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func update(votesCount: Int) {
|
|
|
|
if self.votesCount.intValue != votesCount {
|
|
|
|
self.votesCount = NSNumber(value: votesCount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func update(votersCount: Int?) {
|
|
|
|
if self.votersCount?.intValue != votersCount {
|
|
|
|
self.votersCount = votersCount.flatMap { NSNumber(value: $0) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 12:34:29 +01:00
|
|
|
public func update(voted: Bool, by: MastodonUser) {
|
|
|
|
if voted {
|
|
|
|
if !(votedBy ?? Set()).contains(by) {
|
|
|
|
mutableSetValue(forKey: #keyPath(Poll.votedBy)).add(by)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (votedBy ?? Set()).contains(by) {
|
|
|
|
mutableSetValue(forKey: #keyPath(Poll.votedBy)).remove(by)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 09:12:48 +01:00
|
|
|
public func didUpdate(at networkDate: Date) {
|
|
|
|
self.updatedAt = networkDate
|
|
|
|
}
|
|
|
|
|
2021-03-02 08:51:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension Poll {
|
|
|
|
public struct Property {
|
|
|
|
public let id: ID
|
|
|
|
public let expiresAt: Date?
|
|
|
|
public let expired: Bool
|
|
|
|
public let multiple: Bool
|
|
|
|
public let votesCount: NSNumber
|
|
|
|
public let votersCount: NSNumber?
|
|
|
|
|
|
|
|
public let networkDate: Date
|
|
|
|
|
|
|
|
public init(
|
|
|
|
id: Poll.ID,
|
|
|
|
expiresAt: Date?,
|
|
|
|
expired: Bool,
|
|
|
|
multiple: Bool,
|
|
|
|
votesCount: Int,
|
|
|
|
votersCount: Int?,
|
|
|
|
networkDate: Date
|
|
|
|
) {
|
|
|
|
self.id = id
|
|
|
|
self.expiresAt = expiresAt
|
|
|
|
self.expired = expired
|
|
|
|
self.multiple = multiple
|
|
|
|
self.votesCount = NSNumber(value: votesCount)
|
|
|
|
self.votersCount = votersCount.flatMap { NSNumber(value: $0) }
|
|
|
|
self.networkDate = networkDate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Poll: Managed {
|
|
|
|
public static var defaultSortDescriptors: [NSSortDescriptor] {
|
|
|
|
return [NSSortDescriptor(keyPath: \Poll.createdAt, ascending: false)]
|
|
|
|
}
|
|
|
|
}
|