Add loading animation while syncing timeline position

This commit is contained in:
Shadowfacts 2023-01-01 12:25:44 -05:00
parent b81c83a250
commit 4dca231a06
3 changed files with 30 additions and 15 deletions

View File

@ -213,12 +213,10 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro
clearSelectionOnAppear(animated: animated) clearSelectionOnAppear(animated: animated)
if case .notLoadedInitial = controller.state { if case .notLoadedInitial = controller.state {
if restoreState() {
Task { Task {
if await restoreState() {
await checkPresent(jumpImmediately: false) await checkPresent(jumpImmediately: false)
}
} else { } else {
Task {
await controller.loadInitial() await controller.loadInitial()
} }
} }
@ -329,19 +327,17 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro
return activity return activity
} }
func restoreState() -> Bool { func restoreState() async -> Bool {
guard persistsState, guard persistsState,
Preferences.shared.timelineStateRestoration, Preferences.shared.timelineStateRestoration,
let position = mastodonController.persistentContainer.getTimelinePosition(timeline: timeline) else { let position = mastodonController.persistentContainer.getTimelinePosition(timeline: timeline) else {
return false return false
} }
loadViewIfNeeded() loadViewIfNeeded()
Task {
await controller.restoreInitial { await controller.restoreInitial {
await loadStatusesToRestore(position: position) await loadStatusesToRestore(position: position)
applyItemsToRestore(position: position) applyItemsToRestore(position: position)
} }
}
return true return true
} }
@ -492,15 +488,29 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro
return false return false
} }
if !alwaysPrompt { if !alwaysPrompt {
_ = self.restoreState() Task {
_ = await restoreState()
}
} else { } else {
var config = ToastConfiguration(title: "Sync Position") var config = ToastConfiguration(title: "Sync Position")
config.edge = .top config.edge = .top
config.dismissAutomaticallyAfter = 5 config.dismissAutomaticallyAfter = 5
config.systemImageName = "arrow.triangle.2.circlepath" config.systemImageName = "arrow.triangle.2.circlepath"
config.action = { [unowned self] toast in config.action = { [unowned self] toast in
toast.isUserInteractionEnabled = false
UIView.animateKeyframes(withDuration: 1, delay: 0, options: .repeat) {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5) {
toast.imageView!.transform = CGAffineTransform(rotationAngle: 0.5 * .pi)
}
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) {
// the translation is because the symbol isn't perfectly centered
toast.imageView!.transform = CGAffineTransform(translationX: -0.5, y: 0).rotated(by: .pi)
}
}
Task {
_ = await self.restoreState()
toast.dismissToast(animated: true) toast.dismissToast(animated: true)
_ = self.restoreState() }
} }
showToast(configuration: config, animated: true) showToast(configuration: config, animated: true)
UIAccessibility.post(notification: .announcement, argument: "Synced Position Updated") UIAccessibility.post(notification: .announcement, argument: "Synced Position Updated")

View File

@ -59,7 +59,9 @@ class TimelinesPageViewController: SegmentedPageViewController<TimelinesPageView
guard let vc = currentViewController as? TimelineViewController else { guard let vc = currentViewController as? TimelineViewController else {
return false return false
} }
_ = vc.restoreState() Task {
_ = await vc.restoreState()
}
return true return true
}), }),
] ]

View File

@ -12,6 +12,8 @@ class ToastView: UIView {
let configuration: ToastConfiguration let configuration: ToastConfiguration
private(set) var imageView: UIImageView?
private var panRecognizer: UIPanGestureRecognizer! private var panRecognizer: UIPanGestureRecognizer!
private var shrinkAnimator: UIViewPropertyAnimator? private var shrinkAnimator: UIViewPropertyAnimator?
private var recognizedGesture = false private var recognizedGesture = false
@ -61,6 +63,7 @@ class ToastView: UIView {
imageView.tintColor = .white imageView.tintColor = .white
imageView.contentMode = .scaleAspectFit imageView.contentMode = .scaleAspectFit
stack.addArrangedSubview(imageView) stack.addArrangedSubview(imageView)
self.imageView = imageView
} }
let titleLabel = UILabel() let titleLabel = UILabel()