mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Handle save operations completing in the background while opening itemIDs
This commit is contained in:
parent
4ff8a9b83d
commit
611b237e17
4 changed files with 37 additions and 13 deletions
|
|
@ -168,11 +168,11 @@ class ExtensionSaveService {
|
|||
|
||||
switch pageScrapePayload.contentType {
|
||||
case .none:
|
||||
requestId = try await services.dataService.syncUrl(id: requestId, url: pageScrapePayload.url)
|
||||
requestId = try await services.dataService.createPageFromUrl(id: requestId, url: pageScrapePayload.url)
|
||||
case let .pdf(localUrl):
|
||||
try await services.dataService.syncPdf(id: requestId, localPdfURL: localUrl, url: pageScrapePayload.url)
|
||||
try await services.dataService.createPageFromPdf(id: requestId, localPdfURL: localUrl, url: pageScrapePayload.url)
|
||||
case let .html(html, title, _):
|
||||
requestId = try await services.dataService.syncPage(
|
||||
requestId = try await services.dataService.createPage(
|
||||
id: requestId,
|
||||
originalHtml: html,
|
||||
title: title,
|
||||
|
|
|
|||
|
|
@ -25,19 +25,15 @@ import Utils
|
|||
|
||||
guard let username = username else { return }
|
||||
|
||||
let fetchRequest: NSFetchRequest<Models.LinkedItem> = LinkedItem.fetchRequest()
|
||||
fetchRequest.predicate = NSPredicate(format: "id == %@", requestID)
|
||||
if let existingItem = try? dataService.viewContext.fetch(fetchRequest).first,
|
||||
existingItem.serverSyncStatus == ServerSyncStatus.isNSync.rawValue
|
||||
{
|
||||
print("USING EXISTING ITEM", existingItem.serverSyncStatus)
|
||||
let existing = existingItemOrItemId(dataService: dataService, requestID: requestID)
|
||||
if let existingItem = existing.existingItem {
|
||||
item = existingItem
|
||||
return
|
||||
}
|
||||
|
||||
// If the page was locally created, make sure they are synced before we pull content
|
||||
await dataService.syncUnsyncedArticleContent(itemID: requestID)
|
||||
await fetchLinkedItem(dataService: dataService, requestID: requestID, username: username)
|
||||
await dataService.syncUnsyncedArticleContent(itemID: existing.itemID)
|
||||
await fetchLinkedItem(dataService: dataService, requestID: existing.itemID, username: username)
|
||||
}
|
||||
|
||||
private func fetchLinkedItem(
|
||||
|
|
@ -64,9 +60,16 @@ import Utils
|
|||
do {
|
||||
let retryDelayInNanoSeconds = UInt64(requestCount * 2 * 1_000_000_000)
|
||||
try await Task.sleep(nanoseconds: retryDelayInNanoSeconds)
|
||||
|
||||
let existing = existingItemOrItemId(dataService: dataService, requestID: requestID)
|
||||
if let existingItem = existing.existingItem {
|
||||
item = existingItem
|
||||
return
|
||||
}
|
||||
|
||||
await fetchLinkedItem(
|
||||
dataService: dataService,
|
||||
requestID: requestID,
|
||||
requestID: existing.itemID,
|
||||
username: username,
|
||||
requestCount: requestCount + 1
|
||||
)
|
||||
|
|
@ -75,6 +78,25 @@ import Utils
|
|||
}
|
||||
}
|
||||
|
||||
private func existingItemOrItemId(dataService: DataService, requestID: String) -> (existingItem: LinkedItem?, itemID: String) {
|
||||
let fetchRequest: NSFetchRequest<Models.LinkedItem> = LinkedItem.fetchRequest()
|
||||
fetchRequest.predicate = NSPredicate(format: "createdId == %@ OR id == %@", requestID, requestID)
|
||||
if let existingItem = try? dataService.viewContext.fetch(fetchRequest).first {
|
||||
// If the existing item is synced, we can use it
|
||||
if let itemID = existingItem.id, existingItem.serverSyncStatus == ServerSyncStatus.isNSync.rawValue {
|
||||
item = existingItem
|
||||
return (existingItem: item, itemID: itemID)
|
||||
}
|
||||
|
||||
// If the existing item is not synced, we might have an updated request id
|
||||
if let existingID = existingItem.id {
|
||||
return (existingItem: nil, itemID: existingID)
|
||||
}
|
||||
}
|
||||
|
||||
return (existingItem: nil, itemID: requestID)
|
||||
}
|
||||
|
||||
func trackReadEvent() {
|
||||
guard let item = item else { return }
|
||||
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@
|
|||
<attribute name="author" optional="YES" attributeType="String"/>
|
||||
<attribute name="contentReader" optional="YES" attributeType="String"/>
|
||||
<attribute name="createdAt" attributeType="Date" usesScalarValueType="NO"/>
|
||||
<attribute name="createdId" optional="YES" attributeType="String"/>
|
||||
<attribute name="descriptionText" optional="YES" attributeType="String"/>
|
||||
<attribute name="htmlContent" optional="YES" attributeType="String"/>
|
||||
<attribute name="id" attributeType="String"/>
|
||||
|
|
@ -89,7 +90,7 @@
|
|||
</entity>
|
||||
<elements>
|
||||
<element name="Highlight" positionX="27" positionY="225" width="128" height="224"/>
|
||||
<element name="LinkedItem" positionX="-18" positionY="63" width="128" height="404"/>
|
||||
<element name="LinkedItem" positionX="-18" positionY="63" width="128" height="419"/>
|
||||
<element name="LinkedItemLabel" positionX="-36" positionY="18" width="128" height="134"/>
|
||||
<element name="NewsletterEmail" positionX="0" positionY="180" width="128" height="74"/>
|
||||
<element name="Viewer" positionX="45" positionY="234" width="128" height="89"/>
|
||||
|
|
|
|||
|
|
@ -121,6 +121,7 @@ public final class DataService: ObservableObject {
|
|||
let existingItem = try? self.backgroundContext.fetch(fetchRequest).first
|
||||
let linkedItem = existingItem ?? LinkedItem(entity: LinkedItem.entity(), insertInto: self.backgroundContext)
|
||||
|
||||
linkedItem.createdId = requestId
|
||||
linkedItem.id = existingItem?.unwrappedID ?? requestId
|
||||
linkedItem.title = normalizedURL
|
||||
linkedItem.pageURLString = normalizedURL
|
||||
|
|
|
|||
Loading…
Reference in a new issue