From b63f663947462f398f8d78554f50709faad2b8c5 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 11 Feb 2023 18:10:29 -0500 Subject: [PATCH] Handle errors when loading trending links --- .../Explore/TrendingLinksViewController.swift | 46 ++++++++++++------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/Tusker/Screens/Explore/TrendingLinksViewController.swift b/Tusker/Screens/Explore/TrendingLinksViewController.swift index 85df6798..a622eb33 100644 --- a/Tusker/Screens/Explore/TrendingLinksViewController.swift +++ b/Tusker/Screens/Explore/TrendingLinksViewController.swift @@ -126,16 +126,23 @@ class TrendingLinksViewController: UIViewController, CollectionViewController { snapshot.appendItems([.loadingIndicator]) await dataSource.apply(snapshot) - let request = Client.getTrendingLinks() - guard let (links, _) = try? await mastodonController.run(request) else { - return + do { + let request = Client.getTrendingLinks() + let (links, _) = try await mastodonController.run(request) + snapshot.deleteSections([.loadingIndicator]) + snapshot.appendSections([.links]) + snapshot.appendItems(links.map { .link($0) }) + await dataSource.apply(snapshot) + state = .loaded + } catch { + await dataSource.apply(NSDiffableDataSourceSnapshot()) + state = .unloaded + let config = ToastConfiguration(from: error, with: "Error Loading Trending Links", in: self) { [weak self] toast in + toast.dismissToast(animated: true) + await self?.loadInitial() + } + self.showToast(configuration: config, animated: true) } - snapshot.deleteSections([.loadingIndicator]) - snapshot.appendSections([.links]) - snapshot.appendItems(links.map { .link($0) }) - await dataSource.apply(snapshot) - - state = .loaded } @MainActor @@ -165,15 +172,20 @@ class TrendingLinksViewController: UIViewController, CollectionViewController { await dataSource.apply(snapshot) } - let request = Client.getTrendingLinks(offset: snapshot.itemIdentifiers.count) - guard let (links, _) = try? await mastodonController.run(request) else { - return + do { + let request = Client.getTrendingLinks(offset: origSnapshot.itemIdentifiers.count) + let (links, _) = try await mastodonController.run(request) + snapshot = origSnapshot + snapshot.appendItems(links.map { .link($0) }, toSection: .links) + await dataSource.apply(snapshot) + } catch { + await dataSource.apply(origSnapshot) + let config = ToastConfiguration(from: error, with: "Erorr Loading Older Links", in: self) { [weak self] toast in + toast.dismissToast(animated: true) + await self?.loadOlder() + } + self.showToast(configuration: config, animated: true) } - snapshot = origSnapshot - snapshot.appendItems(links.map { .link($0) }, toSection: .links) - await dataSource.apply(snapshot) - - state = .loaded } }