Tusker/Tusker/Screens/Attachment Gallery/AttachmentViewController.swift

90 lines
2.6 KiB
Swift
Raw Normal View History

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
class AttachmentViewController: UIViewController {
let attachment: Attachment
var largeImageVC: LargeImageViewController?
var loadingVC: LoadingViewController?
var attachmentRequest: ImageCache.Request?
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
}
init(attachment: Attachment) {
self.attachment = attachment
2020-03-18 01:07:44 +00:00
super.init(nibName: nil, bundle: nil)
2019-06-15 00:23:03 +00:00
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
overrideUserInterfaceStyle = .dark
view.backgroundColor = .black
2019-06-15 00:23:03 +00:00
if let data = ImageCache.attachments.get(attachment.url) {
createLargeImage(data: data)
} else {
loadingVC = LoadingViewController()
embedChild(loadingVC!)
attachmentRequest = ImageCache.attachments.get(attachment.url) { [weak self] (data) in
guard let self = self else { return }
self.attachmentRequest = nil
2019-06-15 00:23:03 +00:00
DispatchQueue.main.async {
self.loadingVC?.removeViewAndController()
self.createLargeImage(data: data!)
2019-06-15 00:23:03 +00:00
}
}
}
}
override func didMove(toParent parent: UIViewController?) {
super.didMove(toParent: parent)
if parent == nil {
attachmentRequest?.cancel()
}
}
2019-06-15 00:23:03 +00:00
func createLargeImage(data: Data) {
guard let image = UIImage(data: data) else { return }
2019-06-17 02:39:46 +00:00
largeImageVC = LargeImageViewController(image: image, description: attachment.description, sourceInfo: nil)
2019-06-15 00:23:03 +00:00
largeImageVC!.initialControlsVisible = initialControlsVisible
largeImageVC!.shrinkGestureEnabled = false
if attachment.url.pathExtension == "gif" {
largeImageVC!.gifData = data
}
embedChild(largeImageVC!)
}
}