-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move xcdatamodeld
into WordPressData
#24166
base: trunk
Are you sure you want to change the base?
Changes from all commits
62a7eed
2985a0b
b3b2000
fddc971
f211909
2d08677
758008b
ff36a7e
81890b9
946af4f
dcfecfa
ebe494b
b94d69d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import CoreData | ||
import WordPressDataObjC | ||
|
||
public protocol CoreDataStackSwift: CoreDataStack { | ||
|
||
/// Execute the given block with a background context and save the changes. | ||
/// | ||
/// This function _does not block_ its running thread. The block is executed in background and its return value | ||
/// is passed onto the `completion` block which is executed on the given `queue`. | ||
/// | ||
/// - Parameters: | ||
/// - block: A closure which uses the given `NSManagedObjectContext` to make Core Data model changes. | ||
/// - completion: A closure which is called with the return value of the `block`, after the changed made | ||
/// by the `block` is saved. | ||
/// - queue: A queue on which to execute the completion block. | ||
func performAndSave<T>(_ block: @escaping (NSManagedObjectContext) -> T, completion: ((T) -> Void)?, on queue: DispatchQueue) | ||
|
||
/// Execute the given block with a background context and save the changes _if the block does not throw an error_. | ||
/// | ||
/// This function _does not block_ its running thread. The block is executed in background and the return value | ||
/// (or an error) is passed onto the `completion` block which is executed on the given `queue`. | ||
/// | ||
/// - Parameters: | ||
/// - block: A closure that uses the given `NSManagedObjectContext` to make Core Data model changes. The changes | ||
/// are only saved if the block does not throw an error. | ||
/// - completion: A closure which is called with the `block`'s execution result, which is either an error thrown | ||
/// by the `block` or the return value of the `block`. | ||
/// - queue: A queue on which to execute the completion block. | ||
func performAndSave<T>(_ block: @escaping (NSManagedObjectContext) throws -> T, completion: ((Result<T, Error>) -> Void)?, on queue: DispatchQueue) | ||
|
||
/// Execute the given block with a background context and save the changes _if the block does not throw an error_. | ||
/// | ||
/// - Parameter block: A closure that uses the given `NSManagedObjectContext` to make Core Data model changes. | ||
/// The changes are only saved if the block does not throw an error. | ||
/// - Returns: The value returned by the `block` | ||
/// - Throws: The error thrown by the `block`, in which case the Core Data changes made by the `block` is discarded. | ||
func performAndSave<T>(_ block: @escaping (NSManagedObjectContext) throws -> T) async throws -> T | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import CoreData | ||
import WordPressSharedObjC | ||
|
||
public class BloggingPrompt: NSManagedObject { | ||
|
||
/// The unique ID for the prompt, received from the server. | ||
@NSManaged public var promptID: Int32 | ||
|
||
/// The site ID for the prompt. | ||
@NSManaged public var siteID: Int32 | ||
|
||
/// The prompt content to be displayed at entry points. | ||
@NSManaged public var text: String | ||
|
||
/// The attribution source for the prompt. | ||
@NSManaged public var attribution: String | ||
|
||
/// The prompt date. Time information should be ignored. | ||
@NSManaged public var date: Date | ||
|
||
/// Whether the current user has answered the prompt in `siteID`. | ||
@NSManaged public var answered: Bool | ||
|
||
/// The number of users that has answered the prompt. | ||
@NSManaged public var answerCount: Int32 | ||
|
||
/// Contains avatar URLs of some users that have answered the prompt. | ||
@NSManaged public var displayAvatarURLs: [URL] | ||
|
||
/// Contains additional tags that should be appended to the post for this prompt's answer. | ||
@NSManaged public var additionalPostTags: [String]? | ||
|
||
@nonobjc public class func fetchRequest() -> NSFetchRequest<BloggingPrompt> { | ||
return NSFetchRequest<BloggingPrompt>(entityName: Self.classNameWithoutNamespaces()) | ||
} | ||
|
||
@nonobjc public class func newObject(in context: NSManagedObjectContext) -> BloggingPrompt? { | ||
return NSEntityDescription.insertNewObject(forEntityName: Self.classNameWithoutNamespaces(), into: context) as? BloggingPrompt | ||
} | ||
|
||
public override func awakeFromInsert() { | ||
self.date = .init(timeIntervalSince1970: 0) | ||
self.displayAvatarURLs = [] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
@_exported import WordPressDataObjC | ||
|
||
public let modelURL: URL = { | ||
guard let url = Bundle.module.url(forResource: "WordPress", withExtension: "momd") else { | ||
fatalError("Cannot find model file.") | ||
} | ||
return url | ||
}() | ||
|
||
public func urlForModel(name: String, in directory: String?) -> URL? { | ||
let bundle = Bundle(for: TemporaryDummyClassToPickUpModule.self) | ||
var url = bundle.url(forResource: name, withExtension: "mom", subdirectory: directory) | ||
|
||
if url != nil { | ||
return url | ||
} | ||
|
||
let momdPaths = bundle.paths(forResourcesOfType: "momd", inDirectory: directory) | ||
momdPaths.forEach { (path) in | ||
if url != nil { | ||
return | ||
} | ||
url = bundle.url(forResource: name, withExtension: "mom", subdirectory: URL(fileURLWithPath: path).lastPathComponent) | ||
} | ||
|
||
return url | ||
} | ||
Comment on lines
+3
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These free functions should go into an object at some point. It was just simpler to chuck them in here at this stage while I figure things out. Also, I'd like to think there will be no need to have them |
||
|
||
private class TemporaryDummyClassToPickUpModule {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FOUNDATION_EXPORT double WordPressDataVersionNumber; | ||
|
||
FOUNDATION_EXPORT const unsigned char WordPressDataVersionString[]; | ||
|
||
#import <WordPressData/CoreDataService.h> | ||
#import <WordPressData/CoreDataStack.h> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
#import <Foundation/Foundation.h> | ||
|
||
#import "CoreDataStack.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#import "NSObject+ClassName.h" | ||
|
||
@implementation NSObject (ClassName) | ||
|
||
+ (NSString *)classNameWithoutNamespaces | ||
{ | ||
// Note that Swift prepends the module name to the class name itself | ||
return [[NSStringFromClass(self) componentsSeparatedByString:@"."] lastObject]; | ||
} | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface NSObject (ClassName) | ||
|
||
+ (NSString *)classNameWithoutNamespaces; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import Foundation | ||
import CoreData | ||
import WordPressData | ||
|
||
extension Media { | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import Foundation | ||
import WordPressData | ||
|
||
extension Post { | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,4 @@ | ||
import Foundation | ||
import CoreData | ||
|
||
extension BloggingPrompt { | ||
/// The unique ID for the prompt, received from the server. | ||
@NSManaged public var promptID: Int32 | ||
|
||
/// The site ID for the prompt. | ||
@NSManaged public var siteID: Int32 | ||
|
||
/// The prompt content to be displayed at entry points. | ||
@NSManaged public var text: String | ||
|
||
/// The attribution source for the prompt. | ||
@NSManaged public var attribution: String | ||
|
||
/// The prompt date. Time information should be ignored. | ||
@NSManaged public var date: Date | ||
|
||
/// Whether the current user has answered the prompt in `siteID`. | ||
@NSManaged public var answered: Bool | ||
|
||
/// The number of users that has answered the prompt. | ||
@NSManaged public var answerCount: Int32 | ||
|
||
/// Contains avatar URLs of some users that have answered the prompt. | ||
@NSManaged public var displayAvatarURLs: [URL] | ||
|
||
/// Contains additional tags that should be appended to the post for this prompt's answer. | ||
@NSManaged public var additionalPostTags: [String]? | ||
} | ||
// FIXME: Delete |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before
After