diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/Components/LibraryItemFetcher.swift b/apple/OmnivoreKit/Sources/App/Views/Home/Components/LibraryItemFetcher.swift index a03aa33ff..09fb440a8 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/Components/LibraryItemFetcher.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/Components/LibraryItemFetcher.swift @@ -23,8 +23,6 @@ import Views var searchIdx = 0 var receivedIdx = 0 - var syncCursor: String? - func setItems(_: NSManagedObjectContext, _ items: [Models.LibraryItem]) { self.items = items } @@ -45,36 +43,6 @@ import Views } } - func syncItems(dataService: DataService) async { - let syncStart = Date.now - let lastSyncDate = dataService.lastItemSyncTime - - try? await dataService.syncOfflineItemsWithServerIfNeeded() - - let syncResult = try? await dataService.syncLinkedItems(since: lastSyncDate, - cursor: nil) - - syncCursor = syncResult?.cursor - if let syncResult = syncResult, syncResult.hasMore { - dataService.syncLinkedItemsInBackground(since: lastSyncDate) { - // do nothing - } - } else { - dataService.lastItemSyncTime = syncStart - } - - // If possible start prefetching new pages in the background - if - let itemIDs = syncResult?.updatedItemIDs, - let username = dataService.currentViewer?.username, - !itemIDs.isEmpty - { - Task.detached(priority: .background) { - await dataService.prefetchPages(itemIDs: itemIDs, username: username) - } - } - } - func loadSearchQuery(dataService: DataService, filterState: FetcherFilterState, isRefresh: Bool) async { let thisSearchIdx = searchIdx searchIdx += 1 @@ -123,7 +91,6 @@ import Views await withTaskGroup(of: Void.self) { group in group.addTask { await self.loadCurrentViewer(dataService: dataService) } group.addTask { await self.loadLabels(dataService: dataService) } - group.addTask { await self.syncItems(dataService: dataService) } group.addTask { await self.updateFetchController(dataService: dataService, filterState: filterState) } await group.waitForAll() } @@ -137,6 +104,8 @@ import Views } } + NotificationCenter.default.post(name: NSNotification.PerformSync, object: nil, userInfo: nil) + BadgeCountHandler.updateBadgeCount(dataService: dataService) } diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/Components/LibrarySyncManager.swift b/apple/OmnivoreKit/Sources/App/Views/Home/Components/LibrarySyncManager.swift new file mode 100644 index 000000000..bb4e72709 --- /dev/null +++ b/apple/OmnivoreKit/Sources/App/Views/Home/Components/LibrarySyncManager.swift @@ -0,0 +1,40 @@ +import Foundation +import Services + +class LibrarySyncManager { + var syncCursor: String? + + func syncItems(dataService: DataService) async { + let syncStart = Date.now + let lastSyncDate = dataService.lastItemSyncTime + + if lastSyncDate.timeIntervalSinceNow > -4 { + print("skipping sync as last sync was too recent: ", lastSyncDate) + return + } + + try? await dataService.syncOfflineItemsWithServerIfNeeded() + + let syncResult = try? await dataService.syncLinkedItems(since: lastSyncDate, + cursor: nil) + + syncCursor = syncResult?.cursor + if let syncResult = syncResult, syncResult.hasMore { + dataService.syncLinkedItemsInBackground(since: lastSyncDate) { + // do nothing + } + } else { + dataService.lastItemSyncTime = syncStart + } + + // If possible start prefetching new pages in the background + if + let itemIDs = syncResult?.updatedItemIDs, + !itemIDs.isEmpty + { + Task.detached(priority: .background) { + await dataService.prefetchPages(itemIDs: itemIDs, username: "username") + } + } + } +} diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/LibraryListView.swift b/apple/OmnivoreKit/Sources/App/Views/Home/LibraryListView.swift deleted file mode 100644 index e9c051a95..000000000 --- a/apple/OmnivoreKit/Sources/App/Views/Home/LibraryListView.swift +++ /dev/null @@ -1,42 +0,0 @@ -// -// File.swift -// -// -// Created by Jackson Harper on 6/29/23. -// - -import Foundation -import Models -import SwiftUI - -struct LibraryListView: View { - @StateObject private var libraryViewModel = HomeFeedViewModel( - folder: "inbox", - fetcher: LibraryItemFetcher(), - listConfig: LibraryListConfig( - hasFeatureCards: true, - leadingSwipeActions: [.pin], - trailingSwipeActions: [.archive, .delete], - cardStyle: .library - ) - ) - - var body: some View { -// ZStack { -// NavigationLink( -// destination: LinkDestination(selectedItem: libraryViewModel.selectedItem), -// isActive: $libraryViewModel.linkIsActive -// ) { -// EmptyView() -// } - HomeView(viewModel: libraryViewModel) - .tabItem { - Label { - Text("Library") - } icon: { - Image.tabLibrary - } - } -// } - } -} diff --git a/apple/OmnivoreKit/Sources/App/Views/LibraryTabView.swift b/apple/OmnivoreKit/Sources/App/Views/LibraryTabView.swift index ecf6bd394..1ae664e59 100644 --- a/apple/OmnivoreKit/Sources/App/Views/LibraryTabView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/LibraryTabView.swift @@ -51,6 +51,8 @@ struct LibraryTabView: View { ) ) + private let syncManager = LibrarySyncManager() + var body: some View { VStack(spacing: 0) { TabView(selection: $selectedTab) { @@ -90,5 +92,13 @@ struct LibraryTabView: View { ExpandedAudioPlayer() } .navigationBarHidden(true) + .task { + await syncManager.syncItems(dataService: dataService) + } + .onReceive(NSNotification.performSyncPublisher) { _ in + Task { + await syncManager.syncItems(dataService: dataService) + } + } } } diff --git a/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift b/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift index 7abfd17e1..8848ab1b8 100644 --- a/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift +++ b/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift @@ -13,6 +13,11 @@ public extension NSNotification { static let DisplayProfile = Notification.Name("DisplayProfile") static let Logout = Notification.Name("Logout") static let ScrollToTop = Notification.Name("ScrollToTop") + static let PerformSync = Notification.Name("PerformSync") + + static var performSyncPublisher: NotificationCenter.Publisher { + NotificationCenter.default.publisher(for: PerformSync) + } static var pushFeedItemPublisher: NotificationCenter.Publisher { NotificationCenter.default.publisher(for: PushJSONArticle)