forked from shadowfacts/Tusker
Display toast on load errors
This commit is contained in:
parent
7f4bf52050
commit
7781c5252b
|
@ -59,6 +59,7 @@ class DiffableTimelineLikeTableViewController<Section: Hashable & CaseIterable,
|
||||||
super.viewDidDisappear(animated)
|
super.viewDidDisappear(animated)
|
||||||
|
|
||||||
pruneOffscreenRows()
|
pruneOffscreenRows()
|
||||||
|
currentToast?.dismissToast(animated: false)
|
||||||
}
|
}
|
||||||
|
|
||||||
class func refreshCommandTitle() -> String {
|
class func refreshCommandTitle() -> String {
|
||||||
|
@ -111,13 +112,26 @@ class DiffableTimelineLikeTableViewController<Section: Hashable & CaseIterable,
|
||||||
state = .loadingInitial
|
state = .loadingInitial
|
||||||
|
|
||||||
loadInitialItems() { result in
|
loadInitialItems() { result in
|
||||||
guard case let .success(snapshot) = result else {
|
|
||||||
self.state = .unloaded
|
|
||||||
return
|
|
||||||
}
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
|
switch result {
|
||||||
|
case let .success(snapshot):
|
||||||
self.dataSource.apply(snapshot, animatingDifferences: false)
|
self.dataSource.apply(snapshot, animatingDifferences: false)
|
||||||
self.state = .loaded
|
self.state = .loaded
|
||||||
|
case let .failure(.client(error)):
|
||||||
|
self.state = .unloaded
|
||||||
|
var config = ToastConfiguration(title: "Error Loading")
|
||||||
|
config.subtitle = error.localizedDescription
|
||||||
|
config.systemImageName = error.systemImageName
|
||||||
|
config.actionTitle = "Retry"
|
||||||
|
config.action = { [weak self] (toast) in
|
||||||
|
toast.dismissToast(animated: true)
|
||||||
|
self?.loadInitial()
|
||||||
|
}
|
||||||
|
self.showToast(configuration: config, animated: true)
|
||||||
|
|
||||||
|
default:
|
||||||
|
self.state = .unloaded
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -133,13 +147,27 @@ class DiffableTimelineLikeTableViewController<Section: Hashable & CaseIterable,
|
||||||
state = .loadingOlder
|
state = .loadingOlder
|
||||||
|
|
||||||
loadOlderItems(currentSnapshot: dataSource.snapshot()) { result in
|
loadOlderItems(currentSnapshot: dataSource.snapshot()) { result in
|
||||||
guard case let .success(snapshot) = result else {
|
|
||||||
self.state = .loaded
|
|
||||||
return
|
|
||||||
}
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.dataSource.apply(snapshot, animatingDifferences: false)
|
|
||||||
self.state = .loaded
|
self.state = .loaded
|
||||||
|
|
||||||
|
switch result {
|
||||||
|
case let .success(snapshot):
|
||||||
|
self.dataSource.apply(snapshot, animatingDifferences: false)
|
||||||
|
|
||||||
|
case let .failure(.client(error)):
|
||||||
|
var config = ToastConfiguration(title: "Error Loading Older")
|
||||||
|
config.subtitle = error.localizedDescription
|
||||||
|
config.systemImageName = error.systemImageName
|
||||||
|
config.actionTitle = "Retry"
|
||||||
|
config.action = { [weak self] (toast) in
|
||||||
|
toast.dismissToast(animated: true)
|
||||||
|
self?.loadOlder()
|
||||||
|
}
|
||||||
|
self.showToast(configuration: config, animated: true)
|
||||||
|
|
||||||
|
default:
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -196,24 +224,33 @@ class DiffableTimelineLikeTableViewController<Section: Hashable & CaseIterable,
|
||||||
}
|
}
|
||||||
|
|
||||||
loadNewerItems(currentSnapshot: snapshot) { result in
|
loadNewerItems(currentSnapshot: snapshot) { result in
|
||||||
guard case let .success(snapshot) = result else {
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.refreshControl?.endRefreshing()
|
self.refreshControl?.endRefreshing()
|
||||||
self.state = .loaded
|
self.state = .loaded
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
switch result {
|
||||||
self.refreshControl?.endRefreshing()
|
case let .success(snapshot):
|
||||||
self.dataSource.apply(snapshot, animatingDifferences: false)
|
self.dataSource.apply(snapshot, animatingDifferences: false)
|
||||||
self.state = .loaded
|
|
||||||
|
|
||||||
if let item = item,
|
if let item = item,
|
||||||
let indexPath = self.dataSource.indexPath(for: item) {
|
let indexPath = self.dataSource.indexPath(for: item) {
|
||||||
// maintain the current position in the list (don't scroll to top)
|
// maintain the current position in the list (don't scroll to top)
|
||||||
self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)
|
self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case let .failure(.client(error)):
|
||||||
|
var config = ToastConfiguration(title: "Error Loading Newer")
|
||||||
|
config.subtitle = error.localizedDescription
|
||||||
|
config.systemImageName = error.systemImageName
|
||||||
|
config.actionTitle = "Retry"
|
||||||
|
config.action = { [weak self] (toast) in
|
||||||
|
toast.dismissToast(animated: true)
|
||||||
|
self?.refresh()
|
||||||
|
}
|
||||||
|
self.showToast(configuration: config, animated: true)
|
||||||
|
|
||||||
|
default:
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -267,5 +304,20 @@ extension DiffableTimelineLikeTableViewController {
|
||||||
extension DiffableTimelineLikeTableViewController: BackgroundableViewController {
|
extension DiffableTimelineLikeTableViewController: BackgroundableViewController {
|
||||||
func sceneDidEnterBackground() {
|
func sceneDidEnterBackground() {
|
||||||
pruneOffscreenRows()
|
pruneOffscreenRows()
|
||||||
|
currentToast?.dismissToast(animated: false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension DiffableTimelineLikeTableViewController: ToastableViewController {
|
||||||
|
}
|
||||||
|
|
||||||
|
fileprivate extension Client.Error {
|
||||||
|
var systemImageName: String {
|
||||||
|
switch self {
|
||||||
|
case .networkError(_):
|
||||||
|
return "wifi.exclamationmark"
|
||||||
|
default:
|
||||||
|
return "exclamationmark.triangle"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue