Skip to content

Commit 82f1682

Browse files
authored
Extract Core Data to ShareExtensionCore (#24275)
* Move Core Data entities to ShareExtensionCore * Update how momd is loaded
1 parent b28d4e9 commit 82f1682

File tree

14 files changed

+55
-154
lines changed

14 files changed

+55
-154
lines changed

Modules/Package.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ let package = Package(
8787
"WordPressShared",
8888
.product(name: "CocoaLumberjackSwift", package: "CocoaLumberjack"),
8989
.product(name: "WordPressKit", package: "WordPressKit-iOS"),
90-
]
90+
],
91+
resources: [.process("Resources/Extensions.xcdatamodeld")]
9192
),
9293
.target(
9394
name: "NotificationServiceExtensionCore",

WordPress/WordPressShareExtension/MediaUploadOperation.swift Modules/Sources/ShareExtensionCore/Data/MediaUploadOperation.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class MediaUploadOperation: UploadOperation {
3838
extension MediaUploadOperation {
3939
/// Returns a RemoteMedia object based on this MediaUploadOperation
4040
///
41-
var remoteMedia: RemoteMedia {
41+
public var remoteMedia: RemoteMedia {
4242
let remoteMedia = RemoteMedia()
4343
remoteMedia.mediaID = NSNumber(value: remoteMediaID)
4444
remoteMedia.mimeType = mimeType
@@ -60,7 +60,7 @@ extension MediaUploadOperation {
6060
extension MediaUploadOperation {
6161
/// Updates the local fields with the new values stored in a given RemoteMedia
6262
///
63-
func updateWithMedia(remote: RemoteMedia) {
63+
public func updateWithMedia(remote: RemoteMedia) {
6464
if let mediaId = remote.mediaID?.int64Value {
6565
remoteMediaID = mediaId
6666
}

WordPress/WordPressShareExtension/PostUploadOperation.swift Modules/Sources/ShareExtensionCore/Data/PostUploadOperation.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class PostUploadOperation: UploadOperation {
3838
extension PostUploadOperation {
3939
/// Returns a RemotePost object based on this PostUploadOperation
4040
///
41-
var remotePost: RemotePost {
41+
public var remotePost: RemotePost {
4242
let remotePost = RemotePost()
4343
remotePost.postID = NSNumber(value: remotePostID)
4444
remotePost.content = postContent
@@ -58,7 +58,7 @@ extension PostUploadOperation {
5858
extension PostUploadOperation {
5959
/// Updates the local fields with the new values stored in a given RemotePost
6060
///
61-
func updateWithPost(remote: RemotePost) {
61+
public func updateWithPost(remote: RemotePost) {
6262

6363
if let postId = remote.postID?.int64Value {
6464
remotePostID = postId

WordPress/WordPressShareExtension/SharedCoreDataStack.swift Modules/Sources/ShareExtensionCore/Data/SharedCoreDataStack.swift

+28-32
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,23 @@ final class SharedPersistentContainer: NSPersistentContainer {
2020
extension SharedPersistentContainer: @unchecked Sendable {}
2121
#endif
2222

23-
class SharedCoreDataStack {
23+
public final class SharedCoreDataStack {
2424

2525
// MARK: - Private Properties
2626

27-
fileprivate let modelName: String
27+
/// - warning: Has to be loaded exactly once per process.
28+
nonisolated(unsafe) static let model: NSManagedObjectModel = {
29+
guard let modelURL = Bundle.module.url(forResource: "Extensions", withExtension: "momd") else {
30+
fatalError("Core Data model missing")
31+
}
32+
guard let model = NSManagedObjectModel(contentsOf: modelURL) else {
33+
fatalError("failed to load model")
34+
}
35+
return model
36+
}()
2837

2938
fileprivate lazy var storeContainer: SharedPersistentContainer = {
30-
let container = SharedPersistentContainer(name: self.modelName)
39+
let container = SharedPersistentContainer(name: "SharedCoreDataStack", managedObjectModel: SharedCoreDataStack.model)
3140
container.loadPersistentStores { (storeDescription, error) in
3241
if let error = error as NSError? {
3342
DDLogError("Error loading persistent stores: \(error), \(error.userInfo)")
@@ -40,34 +49,21 @@ class SharedCoreDataStack {
4049

4150
/// Returns the managed context associated with the main queue
4251
///
43-
lazy var managedContext: NSManagedObjectContext = {
52+
public lazy var managedContext: NSManagedObjectContext = {
4453
return self.storeContainer.viewContext
4554
}()
4655

4756
// MARK: - Initializers
4857

4958
/// Initialize the SharedPersistentContainer using the standard Extensions model.
5059
///
51-
convenience init() {
52-
self.init(modelName: Constants.sharedModelName)
53-
}
54-
55-
/// Initialize the core data stack with the given model name.
56-
///
57-
/// This initializer is meant for testing. You probably want to use the convenience `init()` that uses the standard Extensions model
58-
///
59-
/// - Parameters:
60-
/// - modelName: Name of the model to initialize the SharedPersistentContainer with.
61-
///
62-
init(modelName: String) {
63-
self.modelName = modelName
64-
}
60+
public init() {}
6561

6662
// MARK: - Public Funcntions
6763

6864
/// Commit unsaved changes (if any exist) using the main queue's managed context
6965
///
70-
func saveContext() {
66+
public func saveContext() {
7167
guard managedContext.hasChanges else {
7268
return
7369
}
@@ -89,7 +85,7 @@ extension SharedCoreDataStack {
8985
/// - Parameter sessionID: the session ID
9086
/// - Returns: group ID or nil if session does not have an associated group
9187
///
92-
func fetchGroupID(for sessionID: String) -> String? {
88+
public func fetchGroupID(for sessionID: String) -> String? {
9389
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "UploadOperation")
9490
request.predicate = NSPredicate(format: "(backgroundSessionIdentifier == %@)", sessionID)
9591
request.fetchLimit = 1
@@ -104,7 +100,7 @@ extension SharedCoreDataStack {
104100
/// - Parameter objectID: Managed object ID string for a given post upload op
105101
/// - Returns: PostUploadOperation or nil
106102
///
107-
func fetchPostUploadOp(withObjectID objectID: String) -> PostUploadOperation? {
103+
public func fetchPostUploadOp(withObjectID objectID: String) -> PostUploadOperation? {
108104
guard let storeCoordinator = managedContext.persistentStoreCoordinator,
109105
let url = URL(string: objectID),
110106
let managedObjectID = storeCoordinator.managedObjectID(forURIRepresentation: url) else {
@@ -119,7 +115,7 @@ extension SharedCoreDataStack {
119115
/// - Parameter postUploadOpObjectID: Managed object ID for a given post upload op
120116
/// - Returns: PostUploadOperation or nil
121117
///
122-
func fetchPostUploadOp(withObjectID postUploadOpObjectID: NSManagedObjectID) -> PostUploadOperation? {
118+
public func fetchPostUploadOp(withObjectID postUploadOpObjectID: NSManagedObjectID) -> PostUploadOperation? {
123119
var postUploadOp: PostUploadOperation?
124120
do {
125121
postUploadOp = try managedContext.existingObject(with: postUploadOpObjectID) as? PostUploadOperation
@@ -136,7 +132,7 @@ extension SharedCoreDataStack {
136132
/// - Parameter groupID: group ID for a set of upload ops
137133
/// - Returns: post PostUploadOperation or nil
138134
///
139-
func fetchPostUploadOp(for groupID: String) -> PostUploadOperation? {
135+
public func fetchPostUploadOp(for groupID: String) -> PostUploadOperation? {
140136
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "PostUploadOperation")
141137
request.predicate = NSPredicate(format: "(groupID == %@)", groupID)
142138
request.fetchLimit = 1
@@ -151,7 +147,7 @@ extension SharedCoreDataStack {
151147
/// - Parameter groupID: group ID for a set of upload ops
152148
/// - Returns: An array of MediaUploadOperations or nil
153149
///
154-
func fetchMediaUploadOps(for groupID: String) -> [MediaUploadOperation]? {
150+
public func fetchMediaUploadOps(for groupID: String) -> [MediaUploadOperation]? {
155151
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "MediaUploadOperation")
156152
request.predicate = NSPredicate(format: "(groupID == %@)", groupID)
157153
guard let results = (try? managedContext.fetch(request)) as? [MediaUploadOperation] else {
@@ -168,7 +164,7 @@ extension SharedCoreDataStack {
168164
/// - sessionID: background session ID
169165
/// - Returns: MediaUploadOperation or nil
170166
///
171-
func fetchMediaUploadOp(for fileName: String, with sessionID: String) -> MediaUploadOperation? {
167+
public func fetchMediaUploadOp(for fileName: String, with sessionID: String) -> MediaUploadOperation? {
172168
guard let fileNameWithoutExtension = URL(string: fileName)?.deletingPathExtension().lastPathComponent else {
173169
return nil
174170
}
@@ -192,7 +188,7 @@ extension SharedCoreDataStack {
192188
/// - sessionID: background session ID
193189
/// - Returns: An array of UploadOperations or nil
194190
///
195-
func fetchSessionUploadOps(for taskIdentifier: Int, with sessionID: String) -> [UploadOperation]? {
191+
public func fetchSessionUploadOps(for taskIdentifier: Int, with sessionID: String) -> [UploadOperation]? {
196192
var uploadOps: [UploadOperation]?
197193
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "UploadOperation")
198194
request.predicate = NSPredicate(format: "(backgroundSessionTaskID == %d AND backgroundSessionIdentifier == %@)", taskIdentifier, sessionID)
@@ -216,7 +212,7 @@ extension SharedCoreDataStack {
216212
/// - status: New status
217213
/// - uploadOpObjectID: Managed object ID for a given upload op
218214
///
219-
func updateStatus(_ status: UploadOperation.UploadStatus, forUploadOpWithObjectID uploadOpObjectID: NSManagedObjectID) {
215+
public func updateStatus(_ status: UploadOperation.UploadStatus, forUploadOpWithObjectID uploadOpObjectID: NSManagedObjectID) {
220216
var uploadOp: UploadOperation?
221217
do {
222218
uploadOp = try managedContext.existingObject(with: uploadOpObjectID) as? UploadOperation
@@ -238,7 +234,7 @@ extension SharedCoreDataStack {
238234
/// - status: New status
239235
/// - Returns: Managed object ID of newly saved media upload operation object
240236
///
241-
func saveMediaOperation(_ remoteMedia: RemoteMedia, sessionID: String, groupIdentifier: String, siteID: NSNumber, with status: UploadOperation.UploadStatus) -> NSManagedObjectID {
237+
public func saveMediaOperation(_ remoteMedia: RemoteMedia, sessionID: String, groupIdentifier: String, siteID: NSNumber, with status: UploadOperation.UploadStatus) -> NSManagedObjectID {
242238
let mediaUploadOp = MediaUploadOperation(context: managedContext)
243239
mediaUploadOp.updateWithMedia(remote: remoteMedia)
244240
mediaUploadOp.backgroundSessionIdentifier = sessionID
@@ -262,7 +258,7 @@ extension SharedCoreDataStack {
262258
/// - remoteMediaID: remote media ID
263259
/// - remoteURL: remote media URL string
264260
///
265-
func updateMediaOperation(for fileName: String, with sessionID: String, remoteMediaID: Int64?, remoteURL: String?, width: Int32?, height: Int32?) {
261+
public func updateMediaOperation(for fileName: String, with sessionID: String, remoteMediaID: Int64?, remoteURL: String?, width: Int32?, height: Int32?) {
266262
guard let mediaUploadOp = fetchMediaUploadOp(for: fileName, with: sessionID) else {
267263
DDLogError("Error loading UploadOperation Object with File Name: \(fileName)")
268264
return
@@ -289,7 +285,7 @@ extension SharedCoreDataStack {
289285
/// - status: New status
290286
/// - Returns: Managed object ID of newly saved post upload operation object
291287
///
292-
func savePostOperation(_ remotePost: RemotePost, groupIdentifier: String, with status: UploadOperation.UploadStatus) -> NSManagedObjectID {
288+
public func savePostOperation(_ remotePost: RemotePost, groupIdentifier: String, with status: UploadOperation.UploadStatus) -> NSManagedObjectID {
293289
let postUploadOp = PostUploadOperation(context: managedContext)
294290
postUploadOp.updateWithPost(remote: remotePost)
295291
postUploadOp.groupID = groupIdentifier
@@ -307,7 +303,7 @@ extension SharedCoreDataStack {
307303
/// - remotePostID: New remote post ID
308304
/// - postUploadOpObjectID: Managed object ID for a given post upload op
309305
///
310-
func updatePostOperation(with status: UploadOperation.UploadStatus, remotePostID: Int64, forPostUploadOpWithObjectID postUploadOpObjectID: NSManagedObjectID) {
306+
public func updatePostOperation(with status: UploadOperation.UploadStatus, remotePostID: Int64, forPostUploadOpWithObjectID postUploadOpObjectID: NSManagedObjectID) {
311307
guard let postUploadOp = (try? managedContext.existingObject(with: postUploadOpObjectID)) as? PostUploadOperation else {
312308
DDLogError("Error loading PostUploadOperation Object with ID: \(postUploadOpObjectID)")
313309
return
@@ -323,7 +319,7 @@ extension SharedCoreDataStack {
323319
/// - taskID: New background session task ID
324320
/// - uploadOpObjectID: Managed object ID for a given upload op
325321
///
326-
func updateTaskID(_ taskID: NSNumber, forUploadOpWithObjectID uploadOpObjectID: NSManagedObjectID) {
322+
public func updateTaskID(_ taskID: NSNumber, forUploadOpWithObjectID uploadOpObjectID: NSManagedObjectID) {
327323
var uploadOp: UploadOperation?
328324
do {
329325
uploadOp = try managedContext.existingObject(with: uploadOpObjectID) as? UploadOperation

WordPress/WordPressShareExtension/UploadOperation.swift Modules/Sources/ShareExtensionCore/Data/UploadOperation.swift

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import WordPressKit
66
public class UploadOperation: NSManagedObject {
77
/// Curent status for this upload op
88
///
9-
var currentStatus: UploadStatus {
9+
public var currentStatus: UploadStatus {
1010
get {
1111
return UploadStatus(rawValue: Int(self.uploadStatus))!
1212
}
1313
set {
1414
self.uploadStatus = Int32(newValue.rawValue)
1515
}
1616
}
17+
1718
@NSManaged private var uploadStatus: Int32
1819

1920
/// ID which is unique to a group of upload operations within WPiOS (and its extensions)
@@ -42,7 +43,7 @@ public class UploadOperation: NSManagedObject {
4243
extension UploadOperation {
4344
/// Status types for a given upload operation
4445
///
45-
enum UploadStatus: Int {
46+
public enum UploadStatus: Int {
4647
/// Upload has been queued, but not started
4748
///
4849
case pending
@@ -61,10 +62,10 @@ extension UploadOperation {
6162

6263
var stringValue: String {
6364
switch self {
64-
case .pending: return "Pending"
65-
case .inProgress: return "In Progress"
66-
case .complete: return "Complete"
67-
case .error: return "Error"
65+
case .pending: "Pending"
66+
case .inProgress: "In Progress"
67+
case .complete: "Complete"
68+
case .error: "Error"
6869
}
6970
}
7071
}

WordPress/JetpackStatsWidgets/Views/Cards/ListRow.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
21
import SwiftUI
2+
import DesignSystem
33
import WidgetKit
44
import WordPressUI
55

0 commit comments

Comments
 (0)