2
2
mirror of https://github.com/mastodon/mastodon-ios synced 2025-04-11 22:58:02 +02:00

Remove Poll Option and Poll Persistence (IOS-182) (#1308)

Removes persistence for Poll Option (and also Polls, while I was at it)
This commit is contained in:
Nathan Mattes 2024-06-10 10:34:44 +02:00 committed by GitHub
commit fc9366c0ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 2 additions and 1004 deletions

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22522" systemVersion="23C71" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22757" systemVersion="23C71" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="Application" representedClassName="CoreDataStack.Application" syncable="YES">
<attribute name="identifier" optional="YES" attributeType="UUID" usesScalarValueType="NO"/>
<attribute name="name" attributeType="String"/>
@ -123,33 +123,6 @@
<relationship name="showingReblogs" toMany="YES" deletionRule="Nullify" destinationEntity="MastodonUser" inverseName="showingReblogsBy" inverseEntity="MastodonUser"/>
<relationship name="showingReblogsBy" toMany="YES" deletionRule="Nullify" destinationEntity="MastodonUser" inverseName="showingReblogs" inverseEntity="MastodonUser"/>
<relationship name="statuses" toMany="YES" deletionRule="Nullify" destinationEntity="Status" inverseName="author" inverseEntity="Status"/>
<relationship name="votePollOptions" toMany="YES" deletionRule="Nullify" destinationEntity="PollOption" inverseName="votedBy" inverseEntity="PollOption"/>
<relationship name="votePolls" toMany="YES" deletionRule="Nullify" destinationEntity="Poll" inverseName="votedBy" inverseEntity="Poll"/>
</entity>
<entity name="Poll" representedClassName="CoreDataStack.Poll" syncable="YES">
<attribute name="createdAt" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="domain" attributeType="String" defaultValueString=""/>
<attribute name="expired" attributeType="Boolean" usesScalarValueType="YES"/>
<attribute name="expiresAt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="id" attributeType="String"/>
<attribute name="isVoting" transient="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="multiple" attributeType="Boolean" usesScalarValueType="YES"/>
<attribute name="updatedAt" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="votersCount" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="votesCount" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
<relationship name="options" toMany="YES" deletionRule="Cascade" destinationEntity="PollOption" inverseName="poll" inverseEntity="PollOption"/>
<relationship name="status" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Status" inverseName="poll" inverseEntity="Status"/>
<relationship name="votedBy" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="MastodonUser" inverseName="votePolls" inverseEntity="MastodonUser"/>
</entity>
<entity name="PollOption" representedClassName="CoreDataStack.PollOption" syncable="YES">
<attribute name="createdAt" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="index" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="isSelected" transient="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="title" attributeType="String"/>
<attribute name="updatedAt" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="votesCount" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
<relationship name="poll" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Poll" inverseName="options" inverseEntity="Poll"/>
<relationship name="votedBy" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="MastodonUser" inverseName="votePollOptions" inverseEntity="MastodonUser"/>
</entity>
<entity name="Setting" representedClassName="CoreDataStack.Setting" syncable="YES">
<attribute name="createdAt" attributeType="Date" usesScalarValueType="NO"/>
@ -194,7 +167,6 @@
<relationship name="feeds" toMany="YES" deletionRule="Cascade" destinationEntity="Feed" inverseName="status" inverseEntity="Feed"/>
<relationship name="mutedBy" toMany="YES" deletionRule="Nullify" destinationEntity="MastodonUser" inverseName="muted" inverseEntity="MastodonUser"/>
<relationship name="pinnedBy" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="MastodonUser" inverseName="pinnedStatus" inverseEntity="MastodonUser"/>
<relationship name="poll" optional="YES" maxCount="1" deletionRule="Cascade" destinationEntity="Poll" inverseName="status" inverseEntity="Poll"/>
<relationship name="reblog" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Status" inverseName="reblogFrom" inverseEntity="Status"/>
<relationship name="reblogFrom" toMany="YES" deletionRule="Cascade" destinationEntity="Status" inverseName="reblog" inverseEntity="Status"/>
<relationship name="rebloggedBy" toMany="YES" deletionRule="Nullify" destinationEntity="MastodonUser" inverseName="reblogged" inverseEntity="MastodonUser"/>

View File

@ -72,8 +72,6 @@ final public class MastodonUser: NSManagedObject {
@NSManaged public private(set) var reblogged: Set<Status>
@NSManaged public private(set) var muted: Set<Status>
@NSManaged public private(set) var bookmarked: Set<Status>
@NSManaged public private(set) var votePollOptions: Set<PollOptionLegacy>
@NSManaged public private(set) var votePolls: Set<PollLegacy>
// relationships
@NSManaged public private(set) var following: Set<MastodonUser>
@NSManaged public private(set) var followingBy: Set<MastodonUser>

View File

@ -1,332 +0,0 @@
//
// Poll.swift
// CoreDataStack
//
// Created by MainasuK Cirno on 2021-3-2.
//
import Foundation
import CoreData
public final class PollLegacy: NSManagedObject {
public typealias ID = String
// sourcery: autoGenerateProperty
@NSManaged public private(set) var domain: String
// sourcery: autoGenerateProperty
@NSManaged public private(set) var id: ID
// sourcery: autoUpdatableObject, autoGenerateProperty
@NSManaged public private(set) var expiresAt: Date?
// sourcery: autoUpdatableObject, autoGenerateProperty
@NSManaged public private(set) var expired: Bool
// sourcery: autoGenerateProperty
@NSManaged public private(set) var multiple: Bool
// sourcery: autoUpdatableObject, autoGenerateProperty
@NSManaged public private(set) var votesCount: Int64
// sourcery: autoUpdatableObject, autoGenerateProperty
@NSManaged public private(set) var votersCount: Int64
// sourcery: autoGenerateProperty
@NSManaged public private(set) var createdAt: Date
// sourcery: autoUpdatableObject, autoGenerateProperty
@NSManaged public private(set) var updatedAt: Date
// sourcery: autoUpdatableObject
@NSManaged public private(set) var isVoting: Bool
// one-to-one relationship
@NSManaged public private(set) var status: Status?
// one-to-many relationship
@NSManaged public private(set) var options: Set<PollOptionLegacy>
// many-to-many relationship
@NSManaged public private(set) var votedBy: Set<MastodonUser>?
}
extension PollLegacy {
@discardableResult
public static func insert(
into context: NSManagedObjectContext,
property: Property
) -> PollLegacy {
let object: PollLegacy = context.insertObject()
object.configure(property: property)
return object
}
}
extension PollLegacy: Managed {
public static var defaultSortDescriptors: [NSSortDescriptor] {
return [NSSortDescriptor(keyPath: \PollLegacy.createdAt, ascending: false)]
}
}
extension PollLegacy {
static func predicate(domain: String) -> NSPredicate {
return NSPredicate(format: "%K == %@", #keyPath(PollLegacy.domain), domain)
}
static func predicate(id: ID) -> NSPredicate {
return NSPredicate(format: "%K == %@", #keyPath(PollLegacy.id), id)
}
static func predicate(ids: [ID]) -> NSPredicate {
return NSPredicate(format: "%K IN %@", #keyPath(PollLegacy.id), ids)
}
public static func predicate(domain: String, id: ID) -> NSPredicate {
return NSCompoundPredicate(andPredicateWithSubpredicates: [
predicate(domain: domain),
predicate(id: id)
])
}
public static func predicate(domain: String, ids: [ID]) -> NSPredicate {
return NSCompoundPredicate(andPredicateWithSubpredicates: [
predicate(domain: domain),
predicate(ids: ids)
])
}
}
//extension Poll {
//
// public override func awakeFromInsert() {
// super.awakeFromInsert()
// setPrimitiveValue(Date(), forKey: #keyPath(Poll.createdAt))
// }
//
// @discardableResult
// public static func insert(
// into context: NSManagedObjectContext,
// property: Property,
// votedBy: MastodonUser?,
// 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
//
//
// poll.updatedAt = property.networkDate
//
// if let votedBy = votedBy {
// poll.mutableSetValue(forKey: #keyPath(Poll.votedBy)).add(votedBy)
// }
// poll.mutableSetValue(forKey: #keyPath(Poll.options)).addObjects(from: options)
//
// return poll
// }
//
// 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) }
// }
// }
//
// 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)
// }
// }
// }
//
// public func didUpdate(at networkDate: Date) {
// self.updatedAt = networkDate
// }
//
//}
//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
// }
// }
//}
// MARK: - AutoGenerateProperty
extension PollLegacy: AutoGenerateProperty {
// sourcery:inline:Poll.AutoGenerateProperty
// Generated using Sourcery
// DO NOT EDIT
public struct Property {
public let domain: String
public let id: ID
public let expiresAt: Date?
public let expired: Bool
public let multiple: Bool
public let votesCount: Int64
public let votersCount: Int64
public let createdAt: Date
public let updatedAt: Date
public init(
domain: String,
id: ID,
expiresAt: Date?,
expired: Bool,
multiple: Bool,
votesCount: Int64,
votersCount: Int64,
createdAt: Date,
updatedAt: Date
) {
self.domain = domain
self.id = id
self.expiresAt = expiresAt
self.expired = expired
self.multiple = multiple
self.votesCount = votesCount
self.votersCount = votersCount
self.createdAt = createdAt
self.updatedAt = updatedAt
}
}
public func configure(property: Property) {
self.domain = property.domain
self.id = property.id
self.expiresAt = property.expiresAt
self.expired = property.expired
self.multiple = property.multiple
self.votesCount = property.votesCount
self.votersCount = property.votersCount
self.createdAt = property.createdAt
self.updatedAt = property.updatedAt
}
public func update(property: Property) {
update(expiresAt: property.expiresAt)
update(expired: property.expired)
update(votesCount: property.votesCount)
update(votersCount: property.votersCount)
update(updatedAt: property.updatedAt)
}
// sourcery:end
}
// MARK: - AutoUpdatableObject
extension PollLegacy: AutoUpdatableObject {
// sourcery:inline:Poll.AutoUpdatableObject
// Generated using Sourcery
// DO NOT EDIT
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: Int64) {
if self.votesCount != votesCount {
self.votesCount = votesCount
}
}
public func update(votersCount: Int64) {
if self.votersCount != votersCount {
self.votersCount = votersCount
}
}
public func update(updatedAt: Date) {
if self.updatedAt != updatedAt {
self.updatedAt = updatedAt
}
}
public func update(isVoting: Bool) {
if self.isVoting != isVoting {
self.isVoting = isVoting
}
}
// sourcery:end
public func update(voted: Bool, by: MastodonUser) {
if voted {
if !(votedBy ?? Set()).contains(by) {
mutableSetValue(forKey: #keyPath(PollLegacy.votedBy)).add(by)
}
} else {
if (votedBy ?? Set()).contains(by) {
mutableSetValue(forKey: #keyPath(PollLegacy.votedBy)).remove(by)
}
}
}
public func attach(options: [PollOptionLegacy]) {
for option in options {
guard !self.options.contains(option) else { continue }
self.mutableSetValue(forKey: #keyPath(PollLegacy.options)).add(option)
}
}
}
public extension Set<PollOptionLegacy> {
func sortedByIndex() -> [PollOptionLegacy] {
sorted(by: { lhs, rhs in lhs.index < rhs.index })
}
}

View File

@ -1,210 +0,0 @@
//
// PollOption.swift
// CoreDataStack
//
// Created by MainasuK Cirno on 2021-3-2.
//
import Foundation
import CoreData
public final class PollOptionLegacy: NSManagedObject {
// sourcery: autoGenerateProperty
@NSManaged public private(set) var index: Int64
// sourcery: autoUpdatableObject, autoGenerateProperty
@NSManaged public private(set) var title: String
// sourcery: autoUpdatableObject, autoGenerateProperty
@NSManaged public private(set) var votesCount: Int64
// sourcery: autoGenerateProperty
@NSManaged public private(set) var createdAt: Date
// sourcery: autoUpdatableObject, autoGenerateProperty
@NSManaged public private(set) var updatedAt: Date
// sourcery: autoUpdatableObject
@NSManaged public private(set) var isSelected: Bool
// many-to-one relationship
// sourcery: autoUpdatableObject, autoGenerateProperty
@NSManaged public private(set) var poll: PollLegacy?
// many-to-many relationship
@NSManaged public private(set) var votedBy: Set<MastodonUser>?
}
extension PollOptionLegacy {
@discardableResult
public static func insert(
into context: NSManagedObjectContext,
property: Property
) -> PollOptionLegacy {
let object: PollOptionLegacy = context.insertObject()
object.configure(property: property)
return object
}
}
extension PollOptionLegacy: Managed {
public static var defaultSortDescriptors: [NSSortDescriptor] {
return [NSSortDescriptor(keyPath: \PollOptionLegacy.createdAt, ascending: false)]
}
}
//extension PollOption {
//
// public override func awakeFromInsert() {
// super.awakeFromInsert()
// setPrimitiveValue(Date(), forKey: #keyPath(PollOption.createdAt))
// }
//
// @discardableResult
// public static func insert(
// into context: NSManagedObjectContext,
// property: Property,
// votedBy: MastodonUser?
// ) -> PollOption {
// let option: PollOption = context.insertObject()
//
// option.index = property.index
// option.title = property.title
// option.votesCount = property.votesCount
// option.updatedAt = property.networkDate
//
// if let votedBy = votedBy {
// option.mutableSetValue(forKey: #keyPath(PollOption.votedBy)).add(votedBy)
// }
//
// return option
// }
//
// public func update(votesCount: Int?) {
// if self.votesCount?.intValue != votesCount {
// self.votesCount = votesCount.flatMap { NSNumber(value: $0) }
// }
// }
//
// public func didUpdate(at networkDate: Date) {
// self.updatedAt = networkDate
// }
//
//}
//extension PollOption {
// public struct Property {
// public let index: NSNumber
// public let title: String
// public let votesCount: NSNumber?
//
// public let networkDate: Date
//
// public init(index: Int, title: String, votesCount: Int?, networkDate: Date) {
// self.index = NSNumber(value: index)
// self.title = title
// self.votesCount = votesCount.flatMap { NSNumber(value: $0) }
// self.networkDate = networkDate
// }
// }
//}
//
// MARK: - AutoGenerateProperty
extension PollOptionLegacy: AutoGenerateProperty {
// sourcery:inline:PollOption.AutoGenerateProperty
// Generated using Sourcery
// DO NOT EDIT
public struct Property {
public let index: Int64
public let title: String
public let votesCount: Int64
public let createdAt: Date
public let updatedAt: Date
public let poll: PollLegacy?
public init(
index: Int64,
title: String,
votesCount: Int64,
createdAt: Date,
updatedAt: Date,
poll: PollLegacy?
) {
self.index = index
self.title = title
self.votesCount = votesCount
self.createdAt = createdAt
self.updatedAt = updatedAt
self.poll = poll
}
}
public func configure(property: Property) {
self.index = property.index
self.title = property.title
self.votesCount = property.votesCount
self.createdAt = property.createdAt
self.updatedAt = property.updatedAt
self.poll = property.poll
}
public func update(property: Property) {
update(title: property.title)
update(votesCount: property.votesCount)
update(updatedAt: property.updatedAt)
update(poll: property.poll)
}
// sourcery:end
}
// MARK: - AutoUpdatableObject
extension PollOptionLegacy: AutoUpdatableObject {
// sourcery:inline:PollOption.AutoUpdatableObject
// Generated using Sourcery
// DO NOT EDIT
public func update(title: String) {
if self.title != title {
self.title = title
}
}
public func update(votesCount: Int64) {
if self.votesCount != votesCount {
self.votesCount = votesCount
}
}
public func update(updatedAt: Date) {
if self.updatedAt != updatedAt {
self.updatedAt = updatedAt
}
}
public func update(isSelected: Bool) {
if self.isSelected != isSelected {
self.isSelected = isSelected
}
}
public func update(poll: PollLegacy?) {
if self.poll != poll {
self.poll = poll
}
}
// sourcery:end
public func update(voted: Bool, by: MastodonUser) {
if voted {
if !(self.votedBy ?? Set()).contains(by) {
self.mutableSetValue(forKey: #keyPath(PollOptionLegacy.votedBy)).add(by)
}
} else {
if (self.votedBy ?? Set()).contains(by) {
self.mutableSetValue(forKey: #keyPath(PollOptionLegacy.votedBy)).remove(by)
}
}
}
}

View File

@ -77,8 +77,6 @@ public final class Status: NSManagedObject {
// sourcery: autoUpdatableObject
@NSManaged public private(set) var replyTo: Status?
// sourcery: autoGenerateRelationship
@NSManaged public private(set) var poll: PollLegacy?
// sourcery: autoGenerateRelationship
@NSManaged public private(set) var card: Card?
@ -379,18 +377,15 @@ extension Status: AutoGenerateRelationship {
public struct Relationship {
public let application: Application?
public let reblog: Status?
public let poll: PollLegacy?
public let card: Card?
public init(
application: Application?,
reblog: Status?,
poll: PollLegacy?,
card: Card?
) {
self.application = application
self.reblog = reblog
self.poll = poll
self.card = card
}
}
@ -398,7 +393,6 @@ extension Status: AutoGenerateRelationship {
public func configure(relationship: Relationship) {
self.application = relationship.application
self.reblog = relationship.reblog
self.poll = relationship.poll
self.card = relationship.card
}
// sourcery:end

View File

@ -1,30 +0,0 @@
//
// MastodonPoll.swift
//
//
// Created by MainasuK on 2021-12-9.
//
import Foundation
import CoreDataStack
import MastodonSDK
extension PollLegacy.Property {
public init(
entity: Mastodon.Entity.Poll,
domain: String,
networkDate: Date
) {
self.init(
domain: domain,
id: entity.id,
expiresAt: entity.expiresAt,
expired: entity.expired,
multiple: entity.multiple,
votesCount: Int64(entity.votesCount),
votersCount: Int64(entity.votersCount ?? 0),
createdAt: networkDate,
updatedAt: networkDate
)
}
}

View File

@ -1,28 +0,0 @@
//
// MastodonPollOption+Property.swift
//
//
// Created by MainasuK on 2021-12-9.
//
import Foundation
import MastodonSDK
import CoreDataStack
extension PollOptionLegacy.Property {
public init(
poll: PollLegacy,
index: Int,
entity: Mastodon.Entity.Poll.Option,
networkDate: Date
) {
self.init(
index: Int64(index),
title: entity.title,
votesCount: Int64(entity.votesCount ?? 0),
createdAt: networkDate,
updatedAt: networkDate,
poll: poll
)
}
}

View File

@ -1,204 +0,0 @@
//
// Persistence+MastodonPoll.swift
//
//
// Created by MainasuK on 2021-12-9.
//
import CoreData
import CoreDataStack
import Foundation
import MastodonSDK
extension Persistence.Poll {
public struct PersistContext {
public let domain: String
public let entity: Mastodon.Entity.Poll
public let me: MastodonUser?
public let networkDate: Date
public init(
domain: String,
entity: Mastodon.Entity.Poll,
me: MastodonUser?,
networkDate: Date
) {
self.domain = domain
self.entity = entity
self.me = me
self.networkDate = networkDate
}
}
public struct PersistResult {
public let poll: PollLegacy
public let isNewInsertion: Bool
public init(
poll: PollLegacy,
isNewInsertion: Bool
) {
self.poll = poll
self.isNewInsertion = isNewInsertion
}
}
public static func createOrMerge(
in managedObjectContext: NSManagedObjectContext,
context: PersistContext
) -> PersistResult {
if let old = fetch(in: managedObjectContext, context: context) {
merge(in: managedObjectContext, poll: old, context: context)
return PersistResult(
poll: old,
isNewInsertion: false
)
} else {
let poll = create(
in: managedObjectContext,
context: context
)
return PersistResult(
poll: poll,
isNewInsertion: true
)
}
}
}
extension Persistence.Poll {
public static func fetch(
in managedObjectContext: NSManagedObjectContext,
context: PersistContext
) -> PollLegacy? {
let request = PollLegacy.sortedFetchRequest
request.predicate = PollLegacy.predicate(domain: context.domain, id: context.entity.id)
request.fetchLimit = 1
do {
return try managedObjectContext.fetch(request).first
} catch {
assertionFailure(error.localizedDescription)
return nil
}
}
@discardableResult
public static func create(
in managedObjectContext: NSManagedObjectContext,
context: PersistContext
) -> PollLegacy {
let property = PollLegacy.Property(
entity: context.entity,
domain: context.domain,
networkDate: context.networkDate
)
let poll = PollLegacy.insert(
into: managedObjectContext,
property: property
)
update(in: managedObjectContext, poll: poll, context: context)
return poll
}
public static func merge(
in managedObjectContext: NSManagedObjectContext,
poll: PollLegacy,
context: PersistContext
) {
guard context.networkDate > poll.updatedAt else { return }
let property = PollLegacy.Property(
entity: context.entity,
domain: context.domain,
networkDate: context.networkDate
)
poll.update(property: property)
update(in: managedObjectContext, poll: poll, context: context)
}
public static func update(
in managedObjectContext: NSManagedObjectContext,
poll: PollLegacy,
context: PersistContext
) {
let optionEntities = context.entity.options
let options = poll.options.sorted(by: { $0.index < $1.index })
for (option, entity) in zip(options, optionEntities) {
Persistence.PollOption.merge(
option: option,
context: Persistence.PollOption.PersistContext(
index: Int(option.index),
poll: poll,
entity: entity,
me: context.me,
networkDate: context.networkDate
)
)
} // end for in
if let me = context.me {
if let voted = context.entity.voted {
poll.update(voted: voted, by: me)
}
let ownVotes = context.entity.ownVotes ?? []
for option in options {
let index = Int(option.index)
let isVote = ownVotes.contains(index)
option.update(voted: isVote, by: me)
}
}
// update options
if needsPollOptionsUpdate(context: context, poll: poll) {
// options differ, update them
for option in poll.options {
option.update(poll: nil)
managedObjectContext.delete(option)
}
var attachableOptions = [PollOptionLegacy]()
for (index, option) in context.entity.options.enumerated() {
attachableOptions.append(
Persistence.PollOption.create(
in: managedObjectContext,
context: Persistence.PollOption.PersistContext(
index: index,
poll: poll,
entity: option,
me: context.me,
networkDate: context.networkDate
)
)
)
}
poll.attach(options: attachableOptions)
}
poll.update(updatedAt: context.networkDate)
}
private static func needsPollOptionsUpdate(context: PersistContext, poll: PollLegacy) -> Bool {
let entityPollOptions = context.entity.options.map { (title: $0.title, votes: $0.votesCount) }
let pollOptions = poll.options.sortedByIndex().map { (title: $0.title, votes: Int($0.votesCount)) }
guard entityPollOptions.count == pollOptions.count else {
// poll definitely needs to be updated due to differences in count of options
return true
}
for (entityPollOption, pollOption) in zip(entityPollOptions, pollOptions) {
guard entityPollOption.title == pollOption.title else {
// update poll because at least one title differs
return true
}
guard entityPollOption.votes == pollOption.votes else {
// update poll because at least one vote count differs
return true
}
}
return false
}
}

View File

@ -1,102 +0,0 @@
//
// Persistence+MastodonPollOption.swift
//
//
// Created by MainasuK on 2021-12-9.
//
import CoreData
import CoreDataStack
import Foundation
import MastodonSDK
extension Persistence.PollOption {
public struct PersistContext {
public let index: Int
public let poll: PollLegacy
public let entity: Mastodon.Entity.Poll.Option
public let me: MastodonUser?
public let networkDate: Date
public init(
index: Int,
poll: PollLegacy,
entity: Mastodon.Entity.Poll.Option,
me: MastodonUser?,
networkDate: Date
) {
self.index = index
self.poll = poll
self.entity = entity
self.me = me
self.networkDate = networkDate
}
}
public struct PersistResult {
public let option: PollOptionLegacy
public let isNewInsertion: Bool
public init(
option: PollOptionLegacy,
isNewInsertion: Bool
) {
self.option = option
self.isNewInsertion = isNewInsertion
}
}
// the bare Poll.Option entity not supports merge from entity.
// use merge entry on MastodonPoll with exists option objects
public static func persist(
in managedObjectContext: NSManagedObjectContext,
context: PersistContext
) -> PersistResult {
let option = create(in: managedObjectContext, context: context)
return PersistResult(option: option, isNewInsertion: true)
}
}
extension Persistence.PollOption {
@discardableResult
public static func create(
in managedObjectContext: NSManagedObjectContext,
context: PersistContext
) -> PollOptionLegacy {
let property = PollOptionLegacy.Property(
poll: context.poll,
index: context.index,
entity: context.entity,
networkDate: context.networkDate
)
let option = PollOptionLegacy.insert(into: managedObjectContext, property: property)
update(option: option, context: context)
return option
}
public static func merge(
option: PollOptionLegacy,
context: PersistContext
) {
guard context.networkDate > option.updatedAt else { return }
let property = PollOptionLegacy.Property(
poll: context.poll,
index: context.index,
entity: context.entity,
networkDate: context.networkDate
)
option.update(property: property)
update(option: option, context: context)
}
private static func update(
option: PollOptionLegacy,
context: PersistContext
) {
// Do nothing
} // end func update
}

View File

@ -51,6 +51,7 @@ extension Persistence.Status {
}
}
@available(*, deprecated, message: "old")
public static func createOrMerge(
in managedObjectContext: NSManagedObjectContext,
context: PersistContext
@ -78,19 +79,6 @@ extension Persistence.Status {
isNewInsertion: false
)
} else {
let poll: PollLegacy? = {
guard let entity = context.entity.poll else { return nil }
let result = Persistence.Poll.createOrMerge(
in: managedObjectContext,
context: Persistence.Poll.PersistContext(
domain: context.domain,
entity: entity,
me: context.me,
networkDate: context.networkDate
)
)
return result.poll
}()
let card = createCard(in: managedObjectContext, context: context)
@ -99,7 +87,6 @@ extension Persistence.Status {
let relationship = Status.Relationship(
application: application,
reblog: reblog,
poll: poll,
card: card
)
let status = create(
@ -169,51 +156,6 @@ extension Persistence.Status {
networkDate: context.networkDate
)
status.update(property: property)
if let poll = status.poll, let entity = context.entity.poll {
// update poll
Persistence.Poll.update(
in: managedObjectContext,
poll: poll,
context: Persistence.Poll.PersistContext(
domain: context.domain,
entity: entity,
me: context.me,
networkDate: context.networkDate
)
)
} else if let entity = context.entity.poll {
// add poll
let result = Persistence.Poll.createOrMerge(
in: managedObjectContext,
context: Persistence.Poll.PersistContext(
domain: context.domain,
entity: entity,
me: context.me,
networkDate: context.networkDate
)
)
status.configure(
relationship:
Status.Relationship(
application: status.application,
reblog: status.reblog,
poll: result.poll,
card: status.card
)
)
} else if status.poll != nil, context.entity.poll == nil {
// remove poll
status.configure(
relationship:
Status.Relationship(
application: status.application,
reblog: status.reblog,
poll: nil,
card: status.card
)
)
}
if status.card == nil, context.entity.card != nil {
let card = createCard(in: managedObjectContext, context: context)

View File

@ -45,9 +45,7 @@ public enum Persistence {
extension Persistence {
public enum MastodonUser { }
public enum Status { }
public enum Poll { }
public enum Card { }
public enum PollOption { }
public enum SearchHistory { }
public enum Notification { }
}