From 5e9caf9179852f9b6eb79257f082274a53ada2ad Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 17 Mar 2020 21:42:09 -0400 Subject: [PATCH] Use LoadingLargeImageViewController for account avatar/header Prevents crash when tapping unloaded avatar/header images --- .../LargeImageViewController.swift | 2 -- .../LoadingLargeImageViewController.swift | 23 +++++++++++++++++-- Tusker/TuskerNavigationDelegate.swift | 15 ++++++++++++ .../ProfileHeaderTableViewCell.swift | 6 +++-- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/Tusker/Screens/Large Image/LargeImageViewController.swift b/Tusker/Screens/Large Image/LargeImageViewController.swift index 4375ff77..fa3a68b4 100644 --- a/Tusker/Screens/Large Image/LargeImageViewController.swift +++ b/Tusker/Screens/Large Image/LargeImageViewController.swift @@ -7,8 +7,6 @@ // import UIKit -import Pachyderm -import Photos import Gifu class LargeImageViewController: UIViewController, UIScrollViewDelegate, LargeImageAnimatableViewController { diff --git a/Tusker/Screens/Large Image/LoadingLargeImageViewController.swift b/Tusker/Screens/Large Image/LoadingLargeImageViewController.swift index 35b90d2f..dee16a24 100644 --- a/Tusker/Screens/Large Image/LoadingLargeImageViewController.swift +++ b/Tusker/Screens/Large Image/LoadingLargeImageViewController.swift @@ -8,8 +8,8 @@ import UIKit import Pachyderm -class LoadingLargeImageViewController: UIViewController { - +class LoadingLargeImageViewController: UIViewController, LargeImageAnimatableViewController { + let url: URL let cache: ImageCache let imageDescription: String? @@ -33,9 +33,24 @@ class LoadingLargeImageViewController: UIViewController { } } + var animationSourceInfo: LargeImageViewController.SourceInfo? + var animationImage: UIImage? { animationSourceInfo?.image ?? largeImageVC?.image } + var animationGifData: Data? { largeImageVC?.gifData } + var dismissInteractionController: LargeImageInteractionController? + + override var prefersStatusBarHidden: Bool { + return true + } override var childForHomeIndicatorAutoHidden: UIViewController? { return largeImageVC } + override var supportedInterfaceOrientations: UIInterfaceOrientationMask { + if UIDevice.current.userInterfaceIdiom == .phone { + return .allButUpsideDown + } else { + return .all + } + } init(url: URL, cache: ImageCache, imageDescription: String?) { self.url = url @@ -43,6 +58,8 @@ class LoadingLargeImageViewController: UIViewController { self.imageDescription = imageDescription super.init(nibName: nil, bundle: nil) + + modalPresentationStyle = .fullScreen } convenience init(attachment: Attachment) { @@ -73,6 +90,8 @@ class LoadingLargeImageViewController: UIViewController { } } } + + dismissInteractionController = LargeImageInteractionController(viewController: self) } override func didMove(toParent parent: UIViewController?) { diff --git a/Tusker/TuskerNavigationDelegate.swift b/Tusker/TuskerNavigationDelegate.swift index 3c8b5750..321eb602 100644 --- a/Tusker/TuskerNavigationDelegate.swift +++ b/Tusker/TuskerNavigationDelegate.swift @@ -44,6 +44,10 @@ protocol TuskerNavigationDelegate: class { func showLargeImage(gifData: Data, description: String?, animatingFrom sourceView: UIImageView) + func loadingLargeImage(url: URL, cache: ImageCache, description: String?, animatingFrom sourceView: UIImageView) -> LoadingLargeImageViewController + + func showLoadingLargeImage(url: URL, cache: ImageCache, description: String?, animatingFrom sourceView: UIImageView) + func gallery(attachments: [Attachment], sourceViews: [UIImageView?], startIndex: Int) -> GalleryViewController func showGallery(attachments: [Attachment], sourceViews: [UIImageView?], startIndex: Int) @@ -183,6 +187,17 @@ extension TuskerNavigationDelegate where Self: UIViewController { present(largeImage(gifData: gifData, description: description, sourceView: sourceView), animated: true) } + func loadingLargeImage(url: URL, cache: ImageCache, description: String?, animatingFrom sourceView: UIImageView) -> LoadingLargeImageViewController { + let vc = LoadingLargeImageViewController(url: url, cache: cache, imageDescription: description) + vc.animationSourceInfo = sourceViewInfo(sourceView) + vc.transitioningDelegate = self + return vc + } + + func showLoadingLargeImage(url: URL, cache: ImageCache, description: String?, animatingFrom sourceView: UIImageView) { + present(loadingLargeImage(url: url, cache: cache, description: description, animatingFrom: sourceView), animated: true) + } + func gallery(attachments: [Attachment], sourceViews: [UIImageView?], startIndex: Int) -> GalleryViewController { let sourcesInfo = sourceViews.map(sourceViewInfo) let vc = GalleryViewController(attachments: attachments, sourcesInfo: sourcesInfo, startIndex: startIndex) diff --git a/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift b/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift index 9bbc03b1..4a914c58 100644 --- a/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift +++ b/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift @@ -149,11 +149,13 @@ class ProfileHeaderTableViewCell: UITableViewCell { } @objc func avatarPressed() { - delegate?.showLargeImage(avatarImageView.image!, description: nil, animatingFrom: avatarImageView) + guard let account = mastodonController.cache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } + delegate?.showLoadingLargeImage(url: account.avatar, cache: .avatars, description: nil, animatingFrom: avatarImageView) } @objc func headerPressed() { - delegate?.showLargeImage(headerImageView.image!, description: nil, animatingFrom: headerImageView) + guard let account = mastodonController.cache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } + delegate?.showLoadingLargeImage(url: account.header, cache: .headers, description: nil, animatingFrom: headerImageView) } }