Handle errors when loading trending links

This commit is contained in:
Shadowfacts 2023-02-11 18:10:29 -05:00
parent 00a23b525f
commit b63f663947
1 changed files with 29 additions and 17 deletions

View File

@ -126,16 +126,23 @@ class TrendingLinksViewController: UIViewController, CollectionViewController {
snapshot.appendItems([.loadingIndicator]) snapshot.appendItems([.loadingIndicator])
await dataSource.apply(snapshot) await dataSource.apply(snapshot)
let request = Client.getTrendingLinks() do {
guard let (links, _) = try? await mastodonController.run(request) else { let request = Client.getTrendingLinks()
return 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 @MainActor
@ -165,15 +172,20 @@ class TrendingLinksViewController: UIViewController, CollectionViewController {
await dataSource.apply(snapshot) await dataSource.apply(snapshot)
} }
let request = Client.getTrendingLinks(offset: snapshot.itemIdentifiers.count) do {
guard let (links, _) = try? await mastodonController.run(request) else { let request = Client.getTrendingLinks(offset: origSnapshot.itemIdentifiers.count)
return 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
} }
} }