diff --git a/Mastodon/Persistence/FileManager+SearchHistory.swift b/Mastodon/Persistence/FileManager+SearchHistory.swift index a9da200fd..e84de6abe 100644 --- a/Mastodon/Persistence/FileManager+SearchHistory.swift +++ b/Mastodon/Persistence/FileManager+SearchHistory.swift @@ -15,7 +15,7 @@ extension FileManager { do { let searchItems = try jsonDecoder.decode([Persistence.SearchHistory.Item].self, from: data) .filter { $0.userID == userID } - .sorted { $0.updatedAt < $1.updatedAt } + .sorted { $0.updatedAt > $1.updatedAt } return searchItems } catch { diff --git a/MastodonSDK/Sources/CoreDataStack/CoreData.xcdatamodeld/CoreData 9.xcdatamodel/contents b/MastodonSDK/Sources/CoreDataStack/CoreData.xcdatamodeld/CoreData 9.xcdatamodel/contents index c5bbd1485..062ae9d7b 100644 --- a/MastodonSDK/Sources/CoreDataStack/CoreData.xcdatamodeld/CoreData 9.xcdatamodel/contents +++ b/MastodonSDK/Sources/CoreDataStack/CoreData.xcdatamodeld/CoreData 9.xcdatamodel/contents @@ -125,7 +125,6 @@ - @@ -176,16 +175,6 @@ - - - - - - - - - - @@ -236,7 +225,6 @@ - @@ -271,6 +259,5 @@ - \ No newline at end of file diff --git a/MastodonSDK/Sources/CoreDataStack/Entity/Mastodon/MastodonUser.swift b/MastodonSDK/Sources/CoreDataStack/Entity/Mastodon/MastodonUser.swift index 83f94fd22..31ed535a9 100644 --- a/MastodonSDK/Sources/CoreDataStack/Entity/Mastodon/MastodonUser.swift +++ b/MastodonSDK/Sources/CoreDataStack/Entity/Mastodon/MastodonUser.swift @@ -68,7 +68,6 @@ final public class MastodonUser: NSManagedObject { // one-to-many relationship @NSManaged public private(set) var statuses: Set @NSManaged public private(set) var notifications: Set - @NSManaged public private(set) var searchHistories: Set // many-to-many relationship @NSManaged public private(set) var favourite: Set @@ -216,28 +215,6 @@ extension MastodonUser { } - -extension MastodonUser { - - public func findSearchHistory( - domain: String, - userID: MastodonUser.ID - ) -> SearchHistory? { - return searchHistories.first { searchHistory in - return searchHistory.domain == domain - && searchHistory.userID == userID - } - } - - public func findSearchHistory(for user: MastodonUser) -> SearchHistory? { - return searchHistories.first { searchHistory in - return searchHistory.domain == user.domain - && searchHistory.userID == user.id - } - } - -} - // MARK: - AutoGenerateProperty extension MastodonUser: AutoGenerateProperty { // sourcery:inline:MastodonUser.AutoGenerateProperty diff --git a/MastodonSDK/Sources/CoreDataStack/Entity/Mastodon/SearchHistory.swift b/MastodonSDK/Sources/CoreDataStack/Entity/Mastodon/SearchHistory.swift deleted file mode 100644 index c3c6d28c3..000000000 --- a/MastodonSDK/Sources/CoreDataStack/Entity/Mastodon/SearchHistory.swift +++ /dev/null @@ -1,158 +0,0 @@ -// -// SearchHistory.swift -// CoreDataStack -// -// Created by sxiaojian on 2021/4/7. -// - -import Foundation -import CoreData - -public final class SearchHistory: NSManagedObject { - public typealias ID = UUID - - // sourcery: autoGenerateProperty - @NSManaged public private(set) var identifier: ID - // sourcery: autoGenerateProperty - @NSManaged public private(set) var domain: String - // sourcery: autoGenerateProperty - @NSManaged public private(set) var userID: MastodonUser.ID - // sourcery: autoGenerateProperty - @NSManaged public private(set) var createAt: Date - // sourcery: autoUpdatableObject, autoGenerateProperty - @NSManaged public private(set) var updatedAt: Date - - // many-to-one relationship - // sourcery: autoGenerateRelationship - @NSManaged public private(set) var account: MastodonUser? - // sourcery: autoGenerateRelationship - @NSManaged public private(set) var hashtag: Tag? - // sourcery: autoGenerateRelationship - @NSManaged public private(set) var status: Status? - -} - -extension SearchHistory { - @discardableResult - public static func insert( - into context: NSManagedObjectContext, - property: Property, - relationship: Relationship - ) -> SearchHistory { - let object: SearchHistory = context.insertObject() - - object.configure(property: property) - object.configure(relationship: relationship) - - return object - } -} - -extension SearchHistory: Managed { - public static var defaultSortDescriptors: [NSSortDescriptor] { - return [NSSortDescriptor(keyPath: \SearchHistory.updatedAt, ascending: false)] - } -} - -extension SearchHistory { - static func predicate(domain: String) -> NSPredicate { - return NSPredicate(format: "%K == %@", #keyPath(SearchHistory.domain), domain) - } - - static func predicate(userID: String) -> NSPredicate { - return NSPredicate(format: "%K == %@", #keyPath(SearchHistory.userID), userID) - } - - public static func predicate(domain: String, userID: String) -> NSPredicate { - return NSCompoundPredicate(andPredicateWithSubpredicates: [ - predicate(domain: domain), - predicate(userID: userID) - ]) - } -} - -// MARK: - AutoGenerateProperty -extension SearchHistory: AutoGenerateProperty { - // sourcery:inline:SearchHistory.AutoGenerateProperty - - // Generated using Sourcery - // DO NOT EDIT - public struct Property { - public let identifier: ID - public let domain: String - public let userID: MastodonUser.ID - public let createAt: Date - public let updatedAt: Date - - public init( - identifier: ID, - domain: String, - userID: MastodonUser.ID, - createAt: Date, - updatedAt: Date - ) { - self.identifier = identifier - self.domain = domain - self.userID = userID - self.createAt = createAt - self.updatedAt = updatedAt - } - } - - public func configure(property: Property) { - self.identifier = property.identifier - self.domain = property.domain - self.userID = property.userID - self.createAt = property.createAt - self.updatedAt = property.updatedAt - } - - public func update(property: Property) { - update(updatedAt: property.updatedAt) - } - // sourcery:end -} - -// MARK: - AutoGenerateRelationship -extension SearchHistory: AutoGenerateRelationship { - // sourcery:inline:SearchHistory.AutoGenerateRelationship - - // Generated using Sourcery - // DO NOT EDIT - public struct Relationship { - public let account: MastodonUser? - public let hashtag: Tag? - public let status: Status? - - public init( - account: MastodonUser?, - hashtag: Tag?, - status: Status? - ) { - self.account = account - self.hashtag = hashtag - self.status = status - } - } - - public func configure(relationship: Relationship) { - self.account = relationship.account - self.hashtag = relationship.hashtag - self.status = relationship.status - } - // sourcery:end -} - -// MARK: - AutoUpdatableObject -extension SearchHistory: AutoUpdatableObject { - // sourcery:inline:SearchHistory.AutoUpdatableObject - - // Generated using Sourcery - // DO NOT EDIT - public func update(updatedAt: Date) { - if self.updatedAt != updatedAt { - self.updatedAt = updatedAt - } - } - // sourcery:end -} diff --git a/MastodonSDK/Sources/CoreDataStack/Entity/Mastodon/Status.swift b/MastodonSDK/Sources/CoreDataStack/Entity/Mastodon/Status.swift index 1bdd9410a..1b457f085 100644 --- a/MastodonSDK/Sources/CoreDataStack/Entity/Mastodon/Status.swift +++ b/MastodonSDK/Sources/CoreDataStack/Entity/Mastodon/Status.swift @@ -98,7 +98,6 @@ public final class Status: NSManagedObject { @NSManaged public private(set) var reblogFrom: Set @NSManaged public private(set) var replyFrom: Set @NSManaged public private(set) var notifications: Set - @NSManaged public private(set) var searchHistories: Set // sourcery: autoUpdatableObject, autoGenerateProperty @NSManaged public private(set) var updatedAt: Date diff --git a/MastodonSDK/Sources/CoreDataStack/Entity/Mastodon/Tag.swift b/MastodonSDK/Sources/CoreDataStack/Entity/Mastodon/Tag.swift index 8332f3d4c..d95c9dcb3 100644 --- a/MastodonSDK/Sources/CoreDataStack/Entity/Mastodon/Tag.swift +++ b/MastodonSDK/Sources/CoreDataStack/Entity/Mastodon/Tag.swift @@ -31,9 +31,6 @@ public final class Tag: NSManagedObject { // many-to-many relationship @NSManaged public private(set) var followedBy: Set - - // one-to-many relationship - @NSManaged public private(set) var searchHistories: Set } extension Tag { @@ -216,45 +213,3 @@ extension Tag: AutoUpdatableObject { } } - - -extension Tag { - - public func findSearchHistory(domain: String, userID: MastodonUser.ID) -> SearchHistory? { - return searchHistories.first { searchHistory in - return searchHistory.domain == domain - && searchHistory.userID == userID - } - } - - public func findSearchHistory(for user: MastodonUser) -> SearchHistory? { - return searchHistories.first { searchHistory in - return searchHistory.domain == user.domain - && searchHistory.userID == user.id - } - } - -} - -public extension Tag { -// func updateHistory(index: Int, day: Date, uses: String, account: String) { -// let histories = self.histories.sorted { -// $0.createAt.compare($1.createAt) == .orderedAscending -// } -// guard index < histories.count else { return } -// let history = histories[index] -// history.update(day: day) -// history.update(uses: uses) -// history.update(accounts: account) -// } -// -// func appendHistory(history: History) { -// self.mutableSetValue(forKeyPath: #keyPath(Tag.histories)).add(history) -// } -// -// func update(url: String) { -// if self.url != url { -// self.url = url -// } -// } -} diff --git a/MastodonSDK/Sources/MastodonCore/Persistence/Persistence+SearchHistory.swift b/MastodonSDK/Sources/MastodonCore/Persistence/Persistence+SearchHistory.swift deleted file mode 100644 index ef47448cc..000000000 --- a/MastodonSDK/Sources/MastodonCore/Persistence/Persistence+SearchHistory.swift +++ /dev/null @@ -1,113 +0,0 @@ -// -// Persistence+SearchHistory.swift -// Mastodon -// -// Created by MainasuK on 2022-1-20. -// - -import CoreData -import CoreDataStack -import Foundation -import MastodonSDK - -extension Persistence.SearchHistory { - - public struct PersistContext { - public let entity: Entity - public let me: MastodonUser - public let now: Date - public init( - entity: Entity, - me: MastodonUser, - now: Date - ) { - self.entity = entity - self.me = me - self.now = now - } - - public enum Entity: Hashable { - case user(MastodonUser) - case hashtag(Tag) - } - } - - public struct PersistResult { - public let searchHistory: SearchHistory - public let isNewInsertion: Bool - - public init( - searchHistory: SearchHistory, - isNewInsertion: Bool - ) { - self.searchHistory = searchHistory - self.isNewInsertion = isNewInsertion - } - } - - public static func createOrMerge( - in managedObjectContext: NSManagedObjectContext, - context: PersistContext - ) -> PersistResult { - if let old = fetch(in: managedObjectContext, context: context) { - update(searchHistory: old, context: context) - return PersistResult(searchHistory: old, isNewInsertion: false) - } else { - let object = create(in: managedObjectContext, context: context) - return PersistResult(searchHistory: object, isNewInsertion: true) - } - } - -} - -extension Persistence.SearchHistory { - - public static func fetch( - in managedObjectContext: NSManagedObjectContext, - context: PersistContext - ) -> SearchHistory? { - switch context.entity { - case .user(let user): - return user.findSearchHistory(for: context.me) - case .hashtag(let hashtag): - return hashtag.findSearchHistory(for: context.me) - } - } - - @discardableResult - public static func create( - in managedObjectContext: NSManagedObjectContext, - context: PersistContext - ) -> SearchHistory { - let property = SearchHistory.Property( - identifier: UUID(), - domain: context.me.domain, - userID: context.me.id, - createAt: context.now, - updatedAt: context.now - ) - let relationship: SearchHistory.Relationship = { - switch context.entity { - case .user(let user): - return SearchHistory.Relationship(account: user, hashtag: nil, status: nil) - case .hashtag(let hashtag): - return SearchHistory.Relationship(account: nil, hashtag: hashtag, status: nil) - } - }() - let searchHistory = SearchHistory.insert( - into: managedObjectContext, - property: property, - relationship: relationship - ) - update(searchHistory: searchHistory, context: context) - return searchHistory - } - - private static func update( - searchHistory: SearchHistory, - context: PersistContext - ) { - searchHistory.update(updatedAt: context.now) - } - -}