Remove poll-option-related persistence (IOS-182)

This commit is contained in:
Nathan Mattes 2024-06-06 15:43:27 +02:00
parent d954d72c7c
commit 5264f13ef8
7 changed files with 2 additions and 416 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,7 +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">
@ -137,20 +136,9 @@
<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"/>
<attribute name="domain" attributeType="String"/>

View File

@ -72,7 +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>

View File

@ -39,10 +39,7 @@ public final class PollLegacy: NSManagedObject {
// 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>?
}
@ -316,17 +313,4 @@ extension PollLegacy: AutoUpdatableObject {
}
}
}
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

@ -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

@ -124,81 +124,12 @@ extension Persistence.Poll {
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,78 +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
}
}
}
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
}