Skip to content

Commit

Permalink
Merge branch 'develop' into listen-analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
bassrock committed Apr 17, 2023
2 parents ae2881f + f5f8778 commit 9bdbd50
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 16 deletions.
8 changes: 5 additions & 3 deletions PocketKit/Sources/PocketKit/Refresh/RefreshCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ extension RefreshCoordinator {
/// Submit the request to be scheduled anytime after the given interval
private func submitRequest() {
guard let refreshInterval else {
Log.info("No refresh interval set by developer, not scheduling a refresh for \(type(of: self))")
Log.info("No refresh interval set by developer, not scheduling a refresh for \(self.taskID)")
return
}

guard appSession.currentSession != nil else {
Log.warning("No user session, so not scheduling a refresh \(type(of: self))")
Log.warning("No user session, so not scheduling a refresh \(self.taskID)")
return
}

Expand All @@ -134,7 +134,7 @@ extension RefreshCoordinator {
request.earliestBeginDate = Date().addingTimeInterval(refreshInterval)
try taskScheduler.submit(request)
} catch {
Log.warning("Could not submit background task request for \(type(of: self))")
Log.warning("Could not submit background task request for \(self.taskID)")
Log.capture(error: error)
}
}
Expand All @@ -159,6 +159,8 @@ extension RefreshCoordinator {
task.setTaskCompleted(success: false)
}

Log.breadcrumb(category: "background", level: .debug, message: "Starting background refresh call for \(self.taskID)")

/// Call the refresh function and then upon sucess set the task completed
self.refreshData {
task.setTaskCompleted(success: true)
Expand Down
85 changes: 72 additions & 13 deletions PocketKit/Sources/Sync/PocketSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,20 @@ extension PocketSource {
}

public func favorite(item: SavedItem) {
Log.breadcrumb(category: "sync", level: .debug, message: "Favoriting item with id \(String(describing: item.remoteID))")
space.performAndWait {
guard let item = space.backgroundObject(with: item.objectID) as? SavedItem,
let remoteID = item.remoteID else {
Log.capture(message: "Could not retreive item from background context for mutation")
return
}

item.isFavorite = true
try? space.save()
do {
try space.save()
} catch {
Log.capture(error: error)
}

let operation = operations.savedItemMutationOperation(
apollo: apollo,
Expand All @@ -311,14 +317,20 @@ extension PocketSource {
}

public func unfavorite(item: SavedItem) {
Log.breadcrumb(category: "sync", level: .debug, message: "Unfavoriting item with id \(String(describing: item.remoteID))")
space.performAndWait {
guard let item = space.backgroundObject(with: item.objectID) as? SavedItem,
let remoteID = item.remoteID else {
Log.capture(message: "Could not retreive item from background context for mutation")
return
}

item.isFavorite = false
try? space.save()
do {
try space.save()
} catch {
Log.capture(error: error)
}

let operation = operations.savedItemMutationOperation(
apollo: apollo,
Expand All @@ -330,9 +342,11 @@ extension PocketSource {
}

public func delete(item savedItem: SavedItem) {
Log.breadcrumb(category: "sync", level: .debug, message: "Deleting item with id \(String(describing: savedItem.remoteID))")
space.performAndWait {
guard let savedItem = space.backgroundObject(with: savedItem.objectID) as? SavedItem,
let remoteID = savedItem.remoteID else {
Log.capture(message: "Could not retreive item from background context for mutation")
return
}

Expand All @@ -344,7 +358,11 @@ extension PocketSource {
space.delete(item)
}

try? space.save()
do {
try space.save()
} catch {
Log.capture(error: error)
}

let operation = operations.savedItemMutationOperation(
apollo: apollo,
Expand All @@ -357,15 +375,22 @@ extension PocketSource {
}

public func archive(item: SavedItem) {
Log.breadcrumb(category: "sync", level: .debug, message: "Archiving item with id \(String(describing: item.remoteID))")
space.performAndWait {
guard let item = space.backgroundObject(with: item.objectID) as? SavedItem,
let remoteID = item.remoteID else {
Log.capture(message: "Could not retreive item from background context for mutation")
return
}

item.isArchived = true
item.archivedAt = Date()
try? space.save()

do {
try space.save()
} catch {
Log.capture(error: error)
}

let operation = operations.savedItemMutationOperation(
apollo: apollo,
Expand All @@ -378,13 +403,20 @@ extension PocketSource {
}

public func unarchive(item: SavedItem) {
Log.breadcrumb(category: "sync", level: .debug, message: "Unarchiving item with id \(String(describing: item.remoteID))")
space.performAndWait {
guard let item = space.backgroundObject(with: item.objectID) as? SavedItem else {
Log.capture(message: "Could not retreive item from background context for mutation")
return
}
item.isArchived = false
item.createdAt = Date()
try? space.save()

do {
try space.save()
} catch {
Log.capture(error: error)
}

let operation = operations.saveItemOperation(
managedItemID: item.objectID,
Expand All @@ -399,6 +431,8 @@ extension PocketSource {
}

public func save(item: SavedItem) {
// Not logging url for privacy
Log.breadcrumb(category: "sync", level: .debug, message: "Saving item")
let operation = operations.saveItemOperation(
managedItemID: item.objectID,
url: item.url,
Expand All @@ -410,17 +444,23 @@ extension PocketSource {
}

public func addTags(item: SavedItem, tags: [String]) {
Log.breadcrumb(category: "sync", level: .debug, message: "Adding tags to item with id \(String(describing: item.remoteID))")
space.performAndWait {
guard let item = space.backgroundObject(with: item.objectID) as? SavedItem,
let remoteID = item.remoteID else {
Log.capture(message: "Could not retreive item from background context for mutation")
return
}

item.tags = NSOrderedSet(array: tags.compactMap { $0 }.map({ tag in
space.fetchOrCreateTag(byName: tag)
}))

try? space.save()
do {
try space.save()
} catch {
Log.capture(error: error)
}

let operation = operations.savedItemMutationOperation(
apollo: apollo,
Expand All @@ -433,12 +473,20 @@ extension PocketSource {
}

public func deleteTag(tag: Tag) {
Log.breadcrumb(category: "sync", level: .debug, message: "Deleting tags")
space.performAndWait {
guard let tag = space.backgroundObject(with: tag.objectID) as? Tag,
let remoteID = tag.remoteID else { return }
let remoteID = tag.remoteID else {
Log.capture(message: "Could not retreive item from background context for mutation")
return
}

try? space.deleteTag(byID: remoteID)
try? space.save()
do {
try space.deleteTag(byID: remoteID)
try space.save()
} catch {
Log.capture(error: error)
}

let operation = operations.savedItemMutationOperation(
apollo: apollo,
Expand All @@ -451,13 +499,22 @@ extension PocketSource {
}

public func renameTag(from oldTag: Tag, to name: String) {
Log.breadcrumb(category: "sync", level: .debug, message: "Renaming tag")
space.performAndWait {
guard let oldTag = space.backgroundObject(with: oldTag.objectID) as? Tag,
let remoteID = oldTag.remoteID else { return }
let remoteID = oldTag.remoteID else {
Log.capture(message: "Could not retreive item from background context for mutation")
return
}

let fetchedTag = try? space.fetchTag(byID: remoteID)
fetchedTag?.name = name
try? space.save()

do {
try space.save()
} catch {
Log.capture(error: error)
}

let operation = operations.savedItemMutationOperation(
apollo: apollo,
Expand All @@ -482,6 +539,8 @@ extension PocketSource {
}

public func fetchDetails(for savedItem: SavedItem) async throws {
Log.breadcrumb(category: "sync", level: .debug, message: "Fetching detals for item with id \(String(describing: savedItem.remoteID))")

guard let remoteID = savedItem.remoteID else {
return
}
Expand All @@ -494,7 +553,7 @@ extension PocketSource {

try space.performAndWait {
guard let savedItem = space.backgroundObject(with: savedItem.objectID) as? SavedItem else {
Log.capture(message: "Could not get SavedItem from backgroundContext while fetching details")
Log.capture(message: "Could not retreive item from background context for mutation")
return
}
savedItem.update(from: remoteSavedItem.fragments.savedItemParts, with: space)
Expand Down Expand Up @@ -544,7 +603,7 @@ extension PocketSource {
}

public func fetchDetails(for recommendation: Recommendation) async throws {
Log.breadcrumb(category: "detail-loading", level: .debug, message: "Loading details for Recomendation: \(String(describing: recommendation.remoteID))")
Log.breadcrumb(category: "recommendations", level: .debug, message: "Loading details for Recomendation: \(String(describing: recommendation.remoteID))")
guard let item = recommendation.item else {
Log.capture(message: "Could not fetch details for recommendation due to no item")
return
Expand Down

0 comments on commit 9bdbd50

Please sign in to comment.