2019-06-15 00:23:03 +00:00
|
|
|
// AttachmentViewController.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 6/14/19.
|
|
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Pachyderm
|
|
|
|
|
2020-03-18 01:24:15 +00:00
|
|
|
class PendingLargeImageViewController: UIViewController {
|
2019-06-15 00:23:03 +00:00
|
|
|
|
2020-03-18 01:24:15 +00:00
|
|
|
let url: URL
|
|
|
|
let cache: ImageCache
|
|
|
|
let imageDescription: String?
|
|
|
|
|
2019-06-15 00:23:03 +00:00
|
|
|
var largeImageVC: LargeImageViewController?
|
|
|
|
var loadingVC: LoadingViewController?
|
|
|
|
|
2020-03-18 01:24:15 +00:00
|
|
|
var imageRequest: ImageCache.Request?
|
2020-01-25 15:06:27 +00:00
|
|
|
|
2019-06-15 00:23:03 +00:00
|
|
|
private var initialControlsVisible: Bool = true
|
|
|
|
var controlsVisible: Bool {
|
|
|
|
get {
|
|
|
|
return largeImageVC?.controlsVisible ?? initialControlsVisible
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
if let largeImageVC = largeImageVC {
|
|
|
|
largeImageVC.setControlsVisible(newValue, animated: false)
|
|
|
|
} else {
|
|
|
|
initialControlsVisible = newValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override var childForHomeIndicatorAutoHidden: UIViewController? {
|
|
|
|
return largeImageVC
|
|
|
|
}
|
|
|
|
|
2020-03-18 01:24:15 +00:00
|
|
|
init(url: URL, cache: ImageCache, imageDescription: String?) {
|
|
|
|
self.url = url
|
|
|
|
self.cache = cache
|
|
|
|
self.imageDescription = imageDescription
|
2019-06-15 00:23:03 +00:00
|
|
|
|
2020-03-18 01:07:44 +00:00
|
|
|
super.init(nibName: nil, bundle: nil)
|
2019-06-15 00:23:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 01:24:15 +00:00
|
|
|
convenience init(attachment: Attachment) {
|
|
|
|
self.init(url: attachment.url, cache: .attachments, imageDescription: attachment.description)
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:23:03 +00:00
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2020-01-26 03:09:00 +00:00
|
|
|
overrideUserInterfaceStyle = .dark
|
|
|
|
view.backgroundColor = .black
|
|
|
|
|
2020-03-18 01:24:15 +00:00
|
|
|
if let data = cache.get(url) {
|
2019-06-15 00:23:03 +00:00
|
|
|
createLargeImage(data: data)
|
|
|
|
} else {
|
|
|
|
loadingVC = LoadingViewController()
|
|
|
|
embedChild(loadingVC!)
|
2020-03-18 01:24:15 +00:00
|
|
|
imageRequest = cache.get(url) { [weak self] (data) in
|
2020-01-25 15:06:27 +00:00
|
|
|
guard let self = self else { return }
|
2020-03-18 01:24:15 +00:00
|
|
|
self.imageRequest = nil
|
2019-06-15 00:23:03 +00:00
|
|
|
DispatchQueue.main.async {
|
2020-01-25 15:06:27 +00:00
|
|
|
self.loadingVC?.removeViewAndController()
|
|
|
|
self.createLargeImage(data: data!)
|
2019-06-15 00:23:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 15:06:27 +00:00
|
|
|
override func didMove(toParent parent: UIViewController?) {
|
|
|
|
super.didMove(toParent: parent)
|
|
|
|
|
|
|
|
if parent == nil {
|
2020-03-18 01:24:15 +00:00
|
|
|
imageRequest?.cancel()
|
2020-01-25 15:06:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:23:03 +00:00
|
|
|
func createLargeImage(data: Data) {
|
|
|
|
guard let image = UIImage(data: data) else { return }
|
2020-03-18 01:24:15 +00:00
|
|
|
largeImageVC = LargeImageViewController(image: image, description: imageDescription, sourceInfo: nil)
|
2019-06-15 00:23:03 +00:00
|
|
|
largeImageVC!.initialControlsVisible = initialControlsVisible
|
|
|
|
largeImageVC!.shrinkGestureEnabled = false
|
2020-03-18 01:24:15 +00:00
|
|
|
if url.pathExtension == "gif" {
|
2019-06-15 00:23:03 +00:00
|
|
|
largeImageVC!.gifData = data
|
|
|
|
}
|
|
|
|
embedChild(largeImageVC!)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|