Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
gallery/Gallery/PostViewController.swift

156 lines
6.2 KiB
Swift
Raw Normal View History

2020-02-18 06:57:35 -05:00
import UIKit
import CoreData
import AVFoundation
import AVKit
class PostViewController: UIViewController {
2020-03-16 12:10:53 -04:00
@IBOutlet weak var imageView: UIImageView?
@IBOutlet weak var shareButton: UIBarButtonItem?
2020-02-18 06:57:35 -05:00
var post: NSManagedObject?
var image: UIImage?
var isPopup: Bool = false
let documentsPath : URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].absoluteURL
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
2020-03-16 12:10:53 -04:00
if post != nil {
if image == nil {
let imagePath = documentsPath.appendingPathComponent(post!.value(forKey: "name") as! String).path
if((post?.value(forKey: "type") as? String) == "public.mpeg-4") {
self.image = generateThumbnail(path: URL(fileURLWithPath: imagePath))
} else {
self.image = UIImage(contentsOfFile: imagePath)
}
2020-02-18 06:57:35 -05:00
}
2020-03-16 12:10:53 -04:00
imageView?.image = self.image
if(isPopup) {
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(closePopup))
}
2020-02-18 06:57:35 -05:00
}
}
@objc func closePopup() {
UIApplication.shared.requestSceneSessionDestruction((self.view.window?.windowScene!.session)!, options: nil, errorHandler: nil)
}
@IBAction func shareAction(_ sender: Any) {
let imageSearch = ReverseImageSearchService()
imageSearch.viewController = parent
imageSearch.post = self.post as? Post
let activityViewController = UIActivityViewController(activityItems: [self.image!], applicationActivities: [imageSearch])
activityViewController.popoverPresentationController?.barButtonItem = shareButton
self.present(activityViewController, animated: true, completion:nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showTags" {
2020-03-16 12:10:53 -04:00
guard let newViewController = segue.destination as? EditTagsViewController else {
return
}
2020-02-18 06:57:35 -05:00
newViewController.post = self.post as? Post
} else if segue.identifier == "showInfo" {
2020-03-16 12:10:53 -04:00
guard let newViewController = segue.destination as? InfoViewController else {
return
}
2020-02-18 06:57:35 -05:00
newViewController.post = self.post as? Post
newViewController.image = self.image
}
}
@IBAction func playAction(_ sender: Any) {
let imagePath = documentsPath.appendingPathComponent(post!.value(forKey: "name") as! String).path
// Create an AVPlayer, passing it the HTTP Live Streaming URL.
let player = AVPlayer(url: URL(fileURLWithPath: imagePath))
// Create a new AVPlayerViewController and pass it a reference to the player.
let controller = AVPlayerViewController()
controller.player = player
// Modally present the player and call the player's play() method when complete.
present(controller, animated: true) {
player.play()
}
}
}
extension PostViewController {
static func loadFromStoryboard() -> PostViewController? {
let storyboard = UIStoryboard(name: "Main", bundle: .main)
return storyboard.instantiateViewController(withIdentifier: "PostViewController") as? PostViewController
}
}
#if targetEnvironment(macCatalyst)
2020-03-16 12:10:53 -04:00
2020-02-18 06:57:35 -05:00
private let EditButtonToolbarIdentifier = NSToolbarItem.Identifier(rawValue: "OurButton")
private let ShareButtonToolbarIdentifier = NSToolbarItem.Identifier(rawValue: "OurButton2")
private let InfoButtonToolbarIdentifier = NSToolbarItem.Identifier(rawValue: "OurButton3")
extension PostViewController: NSToolbarDelegate {
@objc func editTagsAction() {
if(navigationController?.topViewController != self) {
navigationController?.popViewController(animated: true)
} else {
performSegue(withIdentifier: "showTags", sender: nil)
}
}
@objc func infoAction() {
if(navigationController?.topViewController != self) {
navigationController?.popViewController(animated: true)
} else {
performSegue(withIdentifier: "showInfo", sender: nil)
}
}
func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
return [NSToolbarItem.Identifier.flexibleSpace, InfoButtonToolbarIdentifier, EditButtonToolbarIdentifier, ShareButtonToolbarIdentifier]
}
func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
return toolbarDefaultItemIdentifiers(toolbar)
}
func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
if (itemIdentifier == InfoButtonToolbarIdentifier) {
let barButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.camera,
target: self,
action: #selector(self.infoAction))
let button = NSToolbarItem(itemIdentifier: itemIdentifier, barButtonItem: barButtonItem)
return button
}
if (itemIdentifier == EditButtonToolbarIdentifier) {
let barButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.edit,
target: self,
action: #selector(self.editTagsAction))
let button = NSToolbarItem(itemIdentifier: itemIdentifier, barButtonItem: barButtonItem)
return button
}
if (itemIdentifier == ShareButtonToolbarIdentifier) {
let barButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.action,
target: self,
action: #selector(self.shareAction))
let button = NSToolbarItem(itemIdentifier: itemIdentifier, barButtonItem: barButtonItem)
return button
}
return nil
}
}
2020-03-16 12:10:53 -04:00
2020-02-18 06:57:35 -05:00
#endif