Skip to content

Commit cb9162d

Browse files
committed
Declare a sharedInstance in CoreDataStack
This is to decouple the Objective-C implementation from knowing about components (i.e. `ContextManager`) in the Swift layer.
1 parent d02b1a9 commit cb9162d

File tree

13 files changed

+40
-21
lines changed

13 files changed

+40
-21
lines changed

Modules/Sources/WordPressDataObjC/include/CoreDataStack.h

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ NS_ASSUME_NONNULL_BEGIN
77

88
@property (nonatomic, readonly, strong) NSManagedObjectContext *mainContext;
99

10+
+ (id<CoreDataStack>)sharedInstance;
11+
1012
- (NSManagedObjectContext *const)newDerivedContext DEPRECATED_MSG_ATTRIBUTE("Use `performAndSave` instead");
1113

1214
- (void)saveContextAndWait:(NSManagedObjectContext *)context;

WordPress/Classes/Models/Blog/Blog+SelfHosted.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import CryptoKit
33
import WordPressAPI
4+
import WordPressData
45

56
extension Blog {
67

@@ -17,7 +18,7 @@ extension Blog {
1718

1819
static func createRestApiBlog(
1920
with details: WpApiApplicationPasswordDetails,
20-
in contextManager: ContextManager,
21+
in contextManager: CoreDataStackSwift,
2122
using keychainImplementation: KeychainAccessible = KeychainUtils()
2223
) async throws -> String {
2324
try await contextManager.performAndSave { context in

WordPress/Classes/Services/JetpackSocialService.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ import WordPressKit
1919

2020
/// Init method for Objective-C.
2121
///
22-
@objc init(contextManager: ContextManager) {
23-
self.coreDataStack = contextManager
22+
@objc init(contextManager: CoreDataStack) {
23+
guard let typeCastStack = contextManager as? CoreDataStackSwift else {
24+
fatalError("Expected a CoreDataStackSwift-conforming type even though this initializer is marked @objc.")
25+
}
26+
self.coreDataStack = typeCastStack
2427
}
2528

2629
init(coreDataStack: CoreDataStackSwift = ContextManager.shared) {

WordPress/Classes/Services/SharingService.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import WordPressKit
1212
private let coreDataStack: CoreDataStackSwift
1313

1414
/// The initialiser for Objective-C code.
15-
///
16-
/// Using `ContextManager` as the argument becuase `CoreDataStackSwift` is not accessible from Objective-C code.
1715
@objc
18-
init(contextManager: ContextManager) {
19-
self.coreDataStack = contextManager
16+
init(contextManager: CoreDataStack) {
17+
guard let typeCastStack = contextManager as? CoreDataStackSwift else {
18+
fatalError("Expected a CoreDataStackSwift-conforming type even though this initializer is marked @objc.")
19+
}
20+
self.coreDataStack = typeCastStack
2021
}
2122

2223
init(coreDataStack: CoreDataStackSwift) {

WordPress/Classes/System/Root View/SplitViewRootPresenter+Welcome.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import UIKit
33
import SwiftUI
4+
import WordPressData
45

56
class WelcomeSplitViewContent: SplitViewDisplayable {
67
let supplementary: UINavigationController
@@ -9,7 +10,7 @@ class WelcomeSplitViewContent: SplitViewDisplayable {
910
init(addSite: @escaping (AddSiteMenuViewModel.Selection) -> Void) {
1011
supplementary = UINavigationController(rootViewController: UnifiedPrologueViewController())
1112

12-
let addSiteViewModel = AddSiteMenuViewModel(context: .shared, onSelection: addSite)
13+
let addSiteViewModel = AddSiteMenuViewModel(context: ContextManager.shared, onSelection: addSite)
1314
let noSitesViewModel = NoSitesViewModel(appUIType: JetpackFeaturesRemovalCoordinator.currentAppUIType, account: nil)
1415
let noSiteView = NoSitesView(addSiteViewModel: addSiteViewModel, viewModel: noSitesViewModel)
1516
let noSitesVC = UIHostingController(rootView: noSiteView)

WordPress/Classes/System/UITesting/UITestConfigurator.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ struct UITestConfigurator {
3737

3838
private static func resetEverything() {
3939
// Remove CoreData DB
40-
ContextManager.shared.resetEverything()
40+
//
41+
// We can afford to force cast here because we are in a test-specific code
42+
(ContextManager.shared as! ContextManager).resetEverything()
4143

4244
// Clear user defaults.
4345
for key in UserDefaults.standard.dictionaryRepresentation().keys {

WordPress/Classes/Utility/BackgroundTasks/WeeklyRoundupBackgroundTask.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,14 @@ class WeeklyRoundupBackgroundTask: BackgroundTask {
330330
private let eventTracker: NotificationEventTracker
331331
let runDateComponents: DateComponents
332332
let notificationScheduler: WeeklyRoundupNotificationScheduler
333-
let coreDataStack: ContextManager
333+
let coreDataStack: CoreDataStackSwift
334334

335335
init(
336336
eventTracker: NotificationEventTracker = NotificationEventTracker(),
337337
runDateComponents: DateComponents? = nil,
338338
staticNotificationDateComponents: DateComponents? = nil,
339339
store: Store = Store(),
340-
coreDataStack: ContextManager = .shared
340+
coreDataStack: CoreDataStackSwift = ContextManager.shared
341341
) {
342342
self.coreDataStack = coreDataStack
343343
self.eventTracker = eventTracker

WordPress/Classes/Utility/CoreData/ContextManager.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -282,16 +282,16 @@ extension ContextManager {
282282
/// Tests purpose only
283283
static var overrideInstance: ContextManager?
284284

285-
@objc class func sharedInstance() -> ContextManager {
285+
@objc public class func sharedInstance() -> CoreDataStack {
286286
if let overrideInstance {
287287
return overrideInstance
288288
}
289289

290290
return ContextManager.internalSharedInstance
291291
}
292292

293-
static var shared: ContextManager {
294-
return sharedInstance()
293+
static var shared: CoreDataStackSwift {
294+
return sharedInstance() as! CoreDataStackSwift
295295
}
296296
}
297297

WordPress/Classes/Utility/Logging/WPCrashLoggingProvider.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import UIKit
22
import Combine
33
import AutomatticTracks
44
import AutomatticEncryptedLogs
5+
import WordPressData
56

67
/// A wrapper around the logging stack – provides shared initialization and configuration for Tracks Crash and Event Logging
78
struct WPLoggingStack {
@@ -38,9 +39,9 @@ struct WPLoggingStack {
3839
}
3940

4041
struct WPCrashLoggingDataProvider: CrashLoggingDataProvider {
41-
private let contextManager: ContextManager
42+
private let contextManager: CoreDataStackSwift
4243

43-
init(contextManager: ContextManager = .shared) {
44+
init(contextManager: CoreDataStackSwift = ContextManager.shared) {
4445
self.contextManager = contextManager
4546
}
4647

WordPress/Classes/ViewRelated/Blog/Site Picker/AddSiteController.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import UIKit
22
import SwiftUI
33
import WordPressAuthenticator
4+
import WordPressData
45

56
/// Manages the site creation flows.
67
struct AddSiteController {
@@ -80,7 +81,7 @@ struct AddSiteMenuViewModel {
8081
}
8182
}
8283

83-
init(context: ContextManager = .shared, onSelection: @escaping (Selection) -> Void) {
84+
init(context: CoreDataStackSwift = ContextManager.shared, onSelection: @escaping (Selection) -> Void) {
8485
let defaultAccount = try? WPAccount.lookupDefaultWordPressComAccount(in: context.mainContext)
8586
let canAddSelfHostedSite = AppConfiguration.showAddSelfHostedSiteButton
8687

WordPress/Classes/ViewRelated/Blog/Site Picker/BlogList/BlogListViewModel.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import CoreData
22
import SwiftUI
3+
import WordPressData
34
import WordPressShared
45

56
final class BlogListViewModel: NSObject, ObservableObject {
@@ -15,7 +16,7 @@ final class BlogListViewModel: NSObject, ObservableObject {
1516
private let configuration: BlogListConfiguration
1617
private var rawSites: [Blog] = []
1718
private let fetchedResultsController: NSFetchedResultsController<Blog>
18-
private let contextManager: ContextManager
19+
private let contextManager: CoreDataStackSwift
1920
private let blogService: BlogService
2021
private let eventTracker: EventTracker
2122
private let recentSitesService: RecentSitesService
@@ -24,7 +25,7 @@ final class BlogListViewModel: NSObject, ObservableObject {
2425
var onAddSiteTapped: (AddSiteMenuViewModel.Selection) -> Void = { _ in }
2526

2627
init(configuration: BlogListConfiguration = .defaultConfig,
27-
contextManager: ContextManager = ContextManager.shared,
28+
contextManager: CoreDataStackSwift = ContextManager.shared,
2829
recentSitesService: RecentSitesService = RecentSitesService(),
2930
eventTracker: EventTracker = DefaultEventTracker()) {
3031
self.configuration = configuration

WordPress/Classes/ViewRelated/EEUUSCompliance/CompliancePopoverViewModel.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22
import UIKit
3+
import WordPressData
34
import WordPressUI
45

56
class CompliancePopoverViewModel: ObservableObject {
@@ -13,12 +14,12 @@ class CompliancePopoverViewModel: ObservableObject {
1314

1415
private let analyticsTracker: PrivacySettingsAnalyticsTracking
1516
private let defaults: UserDefaults
16-
private let contextManager: ContextManager
17+
private let contextManager: CoreDataStackSwift
1718

1819
// MARK: - Init
1920

2021
init(defaults: UserDefaults,
21-
contextManager: ContextManager,
22+
contextManager: CoreDataStackSwift,
2223
analyticsTracker: PrivacySettingsAnalyticsTracking = PrivacySettingsAnalyticsTracker()) {
2324
self.defaults = defaults
2425
self.analyticsTracker = analyticsTracker

WordPress/WordPressTest/SharedDataIssueSolverTests.swift

+5
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,15 @@ private extension SharedDataIssueSolverTests {
129129
final class CoreDataStackMock: CoreDataStack {
130130
private(set) var mainContext: NSManagedObjectContext
131131

132+
private static var shared: CoreDataStack?
133+
132134
init(mainContext: NSManagedObjectContext) {
133135
self.mainContext = mainContext
136+
CoreDataStackMock.shared = self
134137
}
135138

139+
static func sharedInstance() -> any CoreDataStack { CoreDataStackMock.shared! }
140+
136141
func newDerivedContext() -> NSManagedObjectContext {
137142
return mainContext
138143
}

0 commit comments

Comments
 (0)