Skip to content

Commit

Permalink
Merge pull request #19713 from wordpress-mobile/fix/blogging-reminder…
Browse files Browse the repository at this point in the history
…s-copy

Export and import blogging reminders using the blog's url
  • Loading branch information
twstokes authored Dec 2, 2022
2 parents 4ac3bb1 + 1ae1402 commit 43e56ac
Showing 1 changed file with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,30 +160,47 @@ class BloggingRemindersScheduler {

private static func copyStoreToSharedFile() {
guard let store = try? defaultStore(),
let fileUrl = try? defaultDataFileURL(),
let sharedFileUrl = sharedDataFileURL() else {
return
}

// Only copy the file if we have at least one reminder schedule
if store.configuration.count > 0 {
try? FileManager.default.copyItem(at: fileUrl, to: sharedFileUrl)
ContextManager.shared.performAndSave { context in
var configuration = [String: ScheduledReminders]()
for (blogIdentifier, schedule) in store.configuration {
guard let objectID = context.persistentStoreCoordinator?.managedObjectID(forURIRepresentation: blogIdentifier),
let blog = context.object(with: objectID) as? Blog,
let url = blog.url else {
continue
}
configuration[url] = schedule
}

if configuration.count > 0 {
try? PropertyListEncoder().encode(configuration).write(to: sharedFileUrl)
}
}
}

private static func copyStoreToLocalFile() {
guard let localStore = try? defaultStore(),
let sharedFileUrl = sharedDataFileURL(),
FileManager.default.fileExists(at: sharedFileUrl),
let sharedStore = try? BloggingRemindersStore(dataFileURL: sharedFileUrl) else {
let data = try? Data(contentsOf: sharedFileUrl),
let sharedConfig = try? PropertyListDecoder().decode([String: ScheduledReminders].self, from: data) else {
return
}

// Only copy if the existing local store contains no schedules
if localStore.configuration.count == 0 {
for blogIdentifier in sharedStore.configuration.keys {
let schedule = sharedStore.scheduledReminders(for: blogIdentifier)
try? localStore.save(scheduledReminders: schedule, for: blogIdentifier)
ContextManager.shared.performAndSave { context in
for (blogUrl, schedule) in sharedConfig {
guard let blog = try? BlogQuery().hostname(matching: blogUrl).blog(in: context) else {
continue
}
let blogIdentifier = blog.objectID.uriRepresentation()
try? localStore.save(scheduledReminders: schedule, for: blogIdentifier)
}
try? FileManager.default.removeItem(at: sharedFileUrl)
}
}
}
Expand Down

0 comments on commit 43e56ac

Please sign in to comment.