From 319b5458fcdd602f83ce5e701f8cd94ce439c534 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 9 Nov 2022 19:15:08 -0500 Subject: [PATCH] Fix refreshing not loading initial when previous attempt failed Closes #214 --- .../Screens/Timeline/TimelineViewController.swift | 8 +++++++- .../TimelineLikeCollectionViewController.swift | 15 +++++++++++++++ Tusker/TimelineLikeController.swift | 4 ++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Tusker/Screens/Timeline/TimelineViewController.swift b/Tusker/Screens/Timeline/TimelineViewController.swift index 55a9498e..3e365eab 100644 --- a/Tusker/Screens/Timeline/TimelineViewController.swift +++ b/Tusker/Screens/Timeline/TimelineViewController.swift @@ -197,7 +197,11 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro @objc func refresh() { Task { - await controller.loadNewer() + if case .notLoadedInitial = await controller.state { + await controller.loadInitial() + } else { + await controller.loadNewer() + } #if !targetEnvironment(macCatalyst) collectionView.refreshControl?.endRefreshing() #endif @@ -280,6 +284,8 @@ extension TimelineViewController { typealias TimelineItem = String // status ID func loadInitial() async throws -> [TimelineItem] { + try await Task.sleep(nanoseconds: 1 * NSEC_PER_SEC) + guard let mastodonController else { throw Error.noClient } diff --git a/Tusker/Screens/Utilities/TimelineLikeCollectionViewController.swift b/Tusker/Screens/Utilities/TimelineLikeCollectionViewController.swift index 281cdd15..53b45631 100644 --- a/Tusker/Screens/Utilities/TimelineLikeCollectionViewController.swift +++ b/Tusker/Screens/Utilities/TimelineLikeCollectionViewController.swift @@ -64,6 +64,14 @@ extension TimelineLikeCollectionViewController { } func handleAddLoadingIndicator() async { + if case .loadingInitial(_, _) = await controller.state, + let refreshControl = collectionView.refreshControl, + refreshControl.isRefreshing { + refreshControl.beginRefreshing() + // if we're loading initial and the refresh control is already going, we don't need to add another indicator + return + } + var snapshot = dataSource.snapshot() if !snapshot.sectionIdentifiers.contains(.footer) { snapshot.appendSections([.footer]) @@ -77,6 +85,13 @@ extension TimelineLikeCollectionViewController { } func handleRemoveLoadingIndicator() async { + if case .loadingInitial(_, _) = await controller.state, + let refreshControl = collectionView.refreshControl, + refreshControl.isRefreshing { + refreshControl.endRefreshing() + return + } + let oldContentOffset = collectionView.contentOffset var snapshot = dataSource.snapshot() snapshot.deleteSections([.footer]) diff --git a/Tusker/TimelineLikeController.swift b/Tusker/TimelineLikeController.swift index 525e5d87..8d05d566 100644 --- a/Tusker/TimelineLikeController.swift +++ b/Tusker/TimelineLikeController.swift @@ -70,7 +70,7 @@ actor TimelineLikeController { } catch { await loadingIndicator.end() await emit(event: .loadAllError(error, token)) - state = .idle + state = .notLoadedInitial } } @@ -194,7 +194,7 @@ actor TimelineLikeController { return false } case .loadingInitial(let token, let hasAddedLoadingIndicator): - return to == .idle || (!hasAddedLoadingIndicator && to == .loadingInitial(token, hasAddedLoadingIndicator: true)) + return to == .notLoadedInitial || to == .idle || (!hasAddedLoadingIndicator && to == .loadingInitial(token, hasAddedLoadingIndicator: true)) case .loadingNewer(_): return to == .idle case .loadingOlder(let token, let hasAddedLoadingIndicator):