Rename PersistenceManager to LocalStorageManager

This commit is contained in:
Angelo Stavrow 2020-09-09 14:19:40 -04:00
parent bb3a7c3c74
commit dad0fef7f1
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
15 changed files with 39 additions and 37 deletions

View File

@ -6,7 +6,7 @@ import UIKit
import AppKit
#endif
class PersistenceManager {
class LocalStorageManager {
static let persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "LocalStorageModel")
container.loadPersistentStores { _, error in
@ -36,15 +36,17 @@ class PersistenceManager {
}
func saveContext() {
if PersistenceManager.persistentContainer.viewContext.hasChanges {
if LocalStorageManager.persistentContainer.viewContext.hasChanges {
do {
try PersistenceManager.persistentContainer.viewContext.save()
try LocalStorageManager.persistentContainer.viewContext.save()
} catch {
print("Error saving context: \(error)")
}
}
}
}
private extension LocalStorageManager {
func saveContextOnResignActive(_ notification: Notification) {
saveContext()
}

View File

@ -198,7 +198,7 @@ private extension WriteFreelyModel {
let fetchedCollections = try result.get()
for fetchedCollection in fetchedCollections {
DispatchQueue.main.async {
let localCollection = WFACollection(context: PersistenceManager.persistentContainer.viewContext)
let localCollection = WFACollection(context: LocalStorageManager.persistentContainer.viewContext)
localCollection.alias = fetchedCollection.alias
localCollection.blogDescription = fetchedCollection.description
localCollection.email = fetchedCollection.email
@ -209,7 +209,7 @@ private extension WriteFreelyModel {
}
}
DispatchQueue.main.async {
PersistenceManager().saveContext()
LocalStorageManager().saveContext()
}
} catch {
print(error)
@ -232,7 +232,7 @@ private extension WriteFreelyModel {
}
} else {
// If it doesn't exist, we create the managed object.
let managedPost = WFAPost(context: PersistenceManager.persistentContainer.viewContext)
let managedPost = WFAPost(context: LocalStorageManager.persistentContainer.viewContext)
managedPost.postId = fetchedPost.postId
managedPost.slug = fetchedPost.slug
managedPost.appearance = fetchedPost.appearance
@ -247,7 +247,7 @@ private extension WriteFreelyModel {
}
}
DispatchQueue.main.async {
PersistenceManager().saveContext()
LocalStorageManager().saveContext()
self.posts.loadCachedPosts()
}
} catch {
@ -275,7 +275,7 @@ private extension WriteFreelyModel {
cachedPost.title = fetchedPost.title ?? ""
cachedPost.updatedDate = fetchedPost.updatedDate
DispatchQueue.main.async {
PersistenceManager().saveContext()
LocalStorageManager().saveContext()
}
} catch {
print(error)
@ -299,7 +299,7 @@ private extension WriteFreelyModel {
cachedPost.updatedDate = fetchedPost.updatedDate
cachedPost.hasNewerRemoteCopy = false
DispatchQueue.main.async {
PersistenceManager().saveContext()
LocalStorageManager().saveContext()
}
} catch {
print(error)

View File

@ -18,7 +18,7 @@ struct ContentView: View {
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
let context = PersistenceManager.persistentContainer.viewContext
let context = LocalStorageManager.persistentContainer.viewContext
let model = WriteFreelyModel()
return ContentView()

View File

@ -8,7 +8,7 @@ struct SidebarView: View {
struct SidebarView_Previews: PreviewProvider {
static var previews: some View {
let context = PersistenceManager.persistentContainer.viewContext
let context = LocalStorageManager.persistentContainer.viewContext
let model = WriteFreelyModel()
return SidebarView()

View File

@ -15,7 +15,7 @@ class CollectionListModel: ObservableObject {
userCollections = []
do {
let cachedCollections = try PersistenceManager.persistentContainer.viewContext.fetch(request)
let cachedCollections = try LocalStorageManager.persistentContainer.viewContext.fetch(request)
userCollections.append(contentsOf: cachedCollections)
} catch {
print("Error: Failed to fetch cached user collections.")
@ -28,8 +28,8 @@ class CollectionListModel: ObservableObject {
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
do {
try PersistenceManager.persistentContainer.persistentStoreCoordinator.execute(
deleteRequest, with: PersistenceManager.persistentContainer.viewContext
try LocalStorageManager.persistentContainer.persistentStoreCoordinator.execute(
deleteRequest, with: LocalStorageManager.persistentContainer.viewContext
)
} catch {
print("Error: Failed to purge cached collections.")

View File

@ -34,7 +34,7 @@ struct CollectionListView: View {
struct CollectionListView_Previews: PreviewProvider {
static var previews: some View {
let context = PersistenceManager.persistentContainer.viewContext
let context = LocalStorageManager.persistentContainer.viewContext
let model = WriteFreelyModel()
return CollectionListView()

View File

@ -63,7 +63,7 @@ struct PostEditorStatusToolbarView: View {
struct PESTView_StandardPreviews: PreviewProvider {
static var previews: some View {
let context = PersistenceManager.persistentContainer.viewContext
let context = LocalStorageManager.persistentContainer.viewContext
let model = WriteFreelyModel()
let testPost = WFAPost(context: context)
testPost.status = PostStatus.published.rawValue
@ -75,7 +75,7 @@ struct PESTView_StandardPreviews: PreviewProvider {
struct PESTView_OutdatedLocalCopyPreviews: PreviewProvider {
static var previews: some View {
let context = PersistenceManager.persistentContainer.viewContext
let context = LocalStorageManager.persistentContainer.viewContext
let model = WriteFreelyModel()
let testPost = WFAPost(context: context)
testPost.status = PostStatus.published.rawValue

View File

@ -45,7 +45,7 @@ struct PostEditorView: View {
.onDisappear(perform: {
if post.status < PostStatus.published.rawValue {
DispatchQueue.main.async {
PersistenceManager().saveContext()
LocalStorageManager().saveContext()
}
}
})
@ -54,7 +54,7 @@ struct PostEditorView: View {
struct PostEditorView_Previews: PreviewProvider {
static var previews: some View {
let context = PersistenceManager.persistentContainer.viewContext
let context = LocalStorageManager.persistentContainer.viewContext
let testPost = WFAPost(context: context)
testPost.title = "Test Post Title"
testPost.body = "Here's some cool sample body text."

View File

@ -30,7 +30,7 @@ struct PostCellView: View {
struct PostCell_Previews: PreviewProvider {
static var previews: some View {
let context = PersistenceManager.persistentContainer.viewContext
let context = LocalStorageManager.persistentContainer.viewContext
let testPost = WFAPost(context: context)
testPost.title = "Test Post Title"
testPost.body = "Here's some cool sample body text."

View File

@ -37,7 +37,7 @@ struct PostListFilteredView: View {
struct PostListFilteredView_Previews: PreviewProvider {
static var previews: some View {
let context = PersistenceManager.persistentContainer.viewContext
let context = LocalStorageManager.persistentContainer.viewContext
return PostListFilteredView(filter: nil, showAllPosts: false)
.environment(\.managedObjectContext, context)

View File

@ -16,7 +16,7 @@ class PostListModel: ObservableObject {
userPosts = []
do {
let cachedPosts = try PersistenceManager.persistentContainer.viewContext.fetch(request)
let cachedPosts = try LocalStorageManager.persistentContainer.viewContext.fetch(request)
userPosts.append(contentsOf: cachedPosts)
} catch {
print("Error: Failed to fetch cached posts.")
@ -29,8 +29,8 @@ class PostListModel: ObservableObject {
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
do {
try PersistenceManager.persistentContainer.persistentStoreCoordinator.execute(
deleteRequest, with: PersistenceManager.persistentContainer.viewContext
try LocalStorageManager.persistentContainer.persistentStoreCoordinator.execute(
deleteRequest, with: LocalStorageManager.persistentContainer.viewContext
)
} catch {
print("Error: Failed to purge cached posts.")

View File

@ -113,20 +113,20 @@ struct PostListView: View {
}
private func createNewLocalDraft() {
let managedPost = WFAPost(context: PersistenceManager.persistentContainer.viewContext)
let managedPost = WFAPost(context: LocalStorageManager.persistentContainer.viewContext)
managedPost.createdDate = Date()
managedPost.title = ""
managedPost.body = ""
managedPost.status = PostStatus.local.rawValue
DispatchQueue.main.async {
PersistenceManager().saveContext()
LocalStorageManager().saveContext()
}
}
}
struct PostListView_Previews: PreviewProvider {
static var previews: some View {
let context = PersistenceManager.persistentContainer.viewContext
let context = LocalStorageManager.persistentContainer.viewContext
let model = WriteFreelyModel()
return PostListView()

View File

@ -38,7 +38,7 @@ struct PostStatusBadgeView: View {
struct PostStatusBadge_LocalDraftPreviews: PreviewProvider {
static var previews: some View {
let context = PersistenceManager.persistentContainer.viewContext
let context = LocalStorageManager.persistentContainer.viewContext
let testPost = WFAPost(context: context)
testPost.status = PostStatus.local.rawValue
@ -49,7 +49,7 @@ struct PostStatusBadge_LocalDraftPreviews: PreviewProvider {
struct PostStatusBadge_EditedPreviews: PreviewProvider {
static var previews: some View {
let context = PersistenceManager.persistentContainer.viewContext
let context = LocalStorageManager.persistentContainer.viewContext
let testPost = WFAPost(context: context)
testPost.status = PostStatus.edited.rawValue
@ -60,7 +60,7 @@ struct PostStatusBadge_EditedPreviews: PreviewProvider {
struct PostStatusBadge_PublishedPreviews: PreviewProvider {
static var previews: some View {
let context = PersistenceManager.persistentContainer.viewContext
let context = LocalStorageManager.persistentContainer.viewContext
let testPost = WFAPost(context: context)
testPost.status = PostStatus.published.rawValue

View File

@ -12,7 +12,7 @@ struct WriteFreely_MultiPlatformApp: App {
WindowGroup {
ContentView()
.environmentObject(model)
.environment(\.managedObjectContext, PersistenceManager.persistentContainer.viewContext)
.environment(\.managedObjectContext, LocalStorageManager.persistentContainer.viewContext)
// .preferredColorScheme(preferences.selectedColorScheme) // See PreferencesModel for info.
}

View File

@ -34,8 +34,8 @@
1756DBB424FECDBB00207AB8 /* PostEditorStatusToolbarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756DBB224FECDBB00207AB8 /* PostEditorStatusToolbarView.swift */; };
1756DBB724FED3A400207AB8 /* LocalStorageModel.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 1756DBB524FED3A400207AB8 /* LocalStorageModel.xcdatamodeld */; };
1756DBB824FED3A400207AB8 /* LocalStorageModel.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 1756DBB524FED3A400207AB8 /* LocalStorageModel.xcdatamodeld */; };
1756DBBA24FED45500207AB8 /* PersistenceManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756DBB924FED45500207AB8 /* PersistenceManager.swift */; };
1756DBBB24FED45500207AB8 /* PersistenceManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756DBB924FED45500207AB8 /* PersistenceManager.swift */; };
1756DBBA24FED45500207AB8 /* LocalStorageManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756DBB924FED45500207AB8 /* LocalStorageManager.swift */; };
1756DBBB24FED45500207AB8 /* LocalStorageManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756DBB924FED45500207AB8 /* LocalStorageManager.swift */; };
1756DC0124FEE18400207AB8 /* WFACollection+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756DBFF24FEE18400207AB8 /* WFACollection+CoreDataClass.swift */; };
1756DC0224FEE18400207AB8 /* WFACollection+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756DBFF24FEE18400207AB8 /* WFACollection+CoreDataClass.swift */; };
1756DC0324FEE18400207AB8 /* WFACollection+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756DC0024FEE18400207AB8 /* WFACollection+CoreDataProperties.swift */; };
@ -104,7 +104,7 @@
1756AE8024CB844500FD7257 /* View+Keyboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+Keyboard.swift"; sourceTree = "<group>"; };
1756DBB224FECDBB00207AB8 /* PostEditorStatusToolbarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostEditorStatusToolbarView.swift; sourceTree = "<group>"; };
1756DBB624FED3A400207AB8 /* LocalStorageModel.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = LocalStorageModel.xcdatamodel; sourceTree = "<group>"; };
1756DBB924FED45500207AB8 /* PersistenceManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersistenceManager.swift; sourceTree = "<group>"; };
1756DBB924FED45500207AB8 /* LocalStorageManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalStorageManager.swift; sourceTree = "<group>"; };
1756DBFF24FEE18400207AB8 /* WFACollection+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WFACollection+CoreDataClass.swift"; sourceTree = SOURCE_ROOT; };
1756DC0024FEE18400207AB8 /* WFACollection+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WFACollection+CoreDataProperties.swift"; sourceTree = SOURCE_ROOT; };
1762DCB224EB086C0019C4EB /* CollectionListModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionListModel.swift; sourceTree = "<group>"; };
@ -254,7 +254,7 @@
isa = PBXGroup;
children = (
17DF328124C87D3300BCE2E3 /* WriteFreely_MultiPlatformApp.swift */,
1756DBB924FED45500207AB8 /* PersistenceManager.swift */,
1756DBB924FED45500207AB8 /* LocalStorageManager.swift */,
17DF328324C87D3500BCE2E3 /* Assets.xcassets */,
17DF32D024C8B75C00BCE2E3 /* Account */,
1756AE7F24CB841200FD7257 /* Extensions */,
@ -571,7 +571,7 @@
files = (
17DF32AC24C87D3500BCE2E3 /* ContentView.swift in Sources */,
17C42E622507D8E600072984 /* PostStatus.swift in Sources */,
1756DBBA24FED45500207AB8 /* PersistenceManager.swift in Sources */,
1756DBBA24FED45500207AB8 /* LocalStorageManager.swift in Sources */,
1756AE8124CB844500FD7257 /* View+Keyboard.swift in Sources */,
17C42E652509237800072984 /* PostListFilteredView.swift in Sources */,
17120DAC24E1B99F002B9F6C /* AccountLoginView.swift in Sources */,
@ -607,7 +607,7 @@
files = (
17DF32AD24C87D3500BCE2E3 /* ContentView.swift in Sources */,
1765F62B24E18EA200C9EBF0 /* SidebarView.swift in Sources */,
1756DBBB24FED45500207AB8 /* PersistenceManager.swift in Sources */,
1756DBBB24FED45500207AB8 /* LocalStorageManager.swift in Sources */,
174D313324EC2831006CA9EE /* WriteFreelyModel.swift in Sources */,
1756AE7824CB2EDD00FD7257 /* PostEditorView.swift in Sources */,
17D435E924E3128F0036B539 /* PreferencesModel.swift in Sources */,