forked from MacMagazine/app-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeedDB.swift
More file actions
72 lines (66 loc) · 2.25 KB
/
FeedDB.swift
File metadata and controls
72 lines (66 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import Foundation
import MacMagazineLibrary
import SwiftData
@Model
public final class FeedDB {
public var postId: String = ""
public var title: String = ""
public var subtitle: String = ""
public var pubDate: Date = Date()
public var creator: String = ""
public var artworkURL: String = ""
public var link: String = ""
public var categories: [String] = []
public var excerpt: String = ""
public var fullContent: String = ""
public var favorite: Bool = false
public var modifiedAt: Date = Date()
public init(
postId: String = "",
title: String = "",
subtitle: String = "",
pubDate: Date = Date(),
creator: String = "",
artworkURL: String = "",
link: String = "",
categories: [String] = [],
excerpt: String = "",
fullContent: String = "",
favorite: Bool = false,
modifiedAt: Date = Date()
) {
self.postId = postId
self.title = title
self.subtitle = subtitle
self.pubDate = pubDate
self.creator = creator
self.artworkURL = artworkURL
self.link = link
self.categories = categories
self.excerpt = excerpt
self.fullContent = fullContent
self.favorite = favorite
self.modifiedAt = modifiedAt
}
}
extension FeedDB: ModelFavoritable {
public static func deleteNonFavorites(using context: ModelContext?) {
let descriptor = FetchDescriptor(predicate: #Predicate<FeedDB> { !$0.favorite })
guard let context,
let data = try? context.fetch(descriptor) else { return }
data.forEach { context.delete($0) }
try? context.save()
}
}
extension FeedDB: ModelDuplicable {
public static func deduplicate(using context: ModelContext?) {
let descriptor = FetchDescriptor<FeedDB>(sortBy: [SortDescriptor(\FeedDB.pubDate, order: .reverse)])
guard let context,
let data = try? context.fetch(descriptor) else { return }
let recordsToDelete = Dictionary(grouping: data, by: \.postId)
.values
.flatMap { $0.sorted { $0.modifiedAt > $1.modifiedAt }.dropFirst() }
recordsToDelete.forEach { context.delete($0) }
try? context.save()
}
}