From 93ef6980d1103a2d4efbb7839d266fb555621ced Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Tue, 19 Apr 2022 12:08:19 -0700 Subject: [PATCH] fetch items from core data if network request fails --- .../App/Views/Home/HomeFeedViewModel.swift | 3 +++ .../Sources/Models/DataModels/FeedItem.swift | 23 +++++++++++++++++++ .../Queries/LibraryItemsQuery.swift | 8 +++++++ 3 files changed, 34 insertions(+) diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift index b20389a81..8da3422cb 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift @@ -76,6 +76,9 @@ import Views receiveCompletion: { [weak self] completion in guard case .failure = completion else { return } self?.isLoading = false + // return cachedItems found in CoreData when request fails + self?.items = dataService.cachedFeedItems() + self?.cursor = nil }, receiveValue: { [weak self] result in // Search results aren't guaranteed to return in order so this diff --git a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift index 9c162178f..c84bf93ca 100644 --- a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift +++ b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift @@ -166,6 +166,29 @@ public extension FeedItemDep { return linkedItem } + + static func make(from item: LinkedItem) -> FeedItemDep { + FeedItemDep( + id: item.id ?? "", + title: item.title ?? "", + createdAt: item.createdAt ?? Date(), + savedAt: item.savedAt ?? Date(), + readingProgress: item.readingProgress, + readingProgressAnchor: Int(item.readingProgressAnchor), + imageURLString: item.imageURLString, + onDeviceImageURLString: item.onDeviceImageURLString, + documentDirectoryPath: nil, + pageURLString: item.pageURLString ?? "", + descriptionText: item.title, + publisherURLString: item.publisherURLString, + author: item.author, + publishDate: item.publishDate, + slug: item.slug ?? "", + isArchived: item.isArchived, + contentReader: item.contentReader, + labels: [] + ) + } } public extension Sequence where Element == FeedItemDep { diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Queries/LibraryItemsQuery.swift b/apple/OmnivoreKit/Sources/Services/DataService/Queries/LibraryItemsQuery.swift index 5ec018a76..f00dc9466 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Queries/LibraryItemsQuery.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Queries/LibraryItemsQuery.swift @@ -1,4 +1,5 @@ import Combine +import CoreData import Foundation import Models import SwiftGraphQL @@ -74,6 +75,13 @@ public extension DataService { .receive(on: DispatchQueue.main) .eraseToAnyPublisher() } + + func cachedFeedItems() -> [FeedItemDep] { + let fetchRequest: NSFetchRequest = LinkedItem.fetchRequest() + // TODO: set sort order? + let items = (try? persistentContainer.viewContext.fetch(fetchRequest)) ?? [] + return items.map { FeedItemDep.make(from: $0) } + } } let homeFeedItemSelection = Selection.Article {