2020-02-18 06:57:35 -05:00
|
|
|
import UIKit
|
|
|
|
import CoreData
|
|
|
|
import AVFoundation
|
|
|
|
import AVKit
|
|
|
|
|
2020-05-24 23:01:11 -04:00
|
|
|
class PostDetailViewController: UIViewController, UIPopoverPresentationControllerDelegate {
|
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
|
|
|
|
|
2020-03-16 13:07:20 -04:00
|
|
|
func updateWindowTitle() {
|
|
|
|
var windowTitle = "Untagged Post"
|
|
|
|
|
|
|
|
var tagList = ""
|
|
|
|
|
|
|
|
guard let postObject = post as? Post else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:01:06 -04:00
|
|
|
for (i, tag) in postObject.tags!.enumerated() {
|
|
|
|
if !tagList.isEmpty && i != postObject.tags!.count - 1 {
|
2020-03-16 13:07:20 -04:00
|
|
|
tagList += ", "
|
|
|
|
}
|
|
|
|
|
|
|
|
tagList += (tag as! Tag).name!
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tagList.isEmpty {
|
|
|
|
windowTitle = tagList
|
|
|
|
}
|
|
|
|
|
|
|
|
self.view.window?.windowScene!.title = windowTitle
|
2020-05-24 21:56:48 -04:00
|
|
|
navigationItem.title = windowTitle
|
2020-03-16 13:07:20 -04:00
|
|
|
}
|
2020-03-16 12:36:52 -04:00
|
|
|
|
2020-02-18 06:57:35 -05:00
|
|
|
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
|
|
|
}
|
2020-05-24 21:56:48 -04:00
|
|
|
|
2020-05-24 22:01:06 -04:00
|
|
|
#if !targetEnvironment(macCatalyst)
|
2020-05-24 21:56:48 -04:00
|
|
|
updateWindowTitle()
|
2020-05-24 22:01:06 -04:00
|
|
|
#endif
|
2020-02-18 06:57:35 -05:00
|
|
|
}
|
|
|
|
|
2020-03-16 12:36:52 -04:00
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
2020-05-24 22:01:06 -04:00
|
|
|
|
|
|
|
#if targetEnvironment(macCatalyst)
|
|
|
|
updateWindowTitle()
|
|
|
|
#endif
|
2020-03-16 12:36:52 -04:00
|
|
|
}
|
|
|
|
|
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
|
2020-03-16 13:07:20 -04:00
|
|
|
|
|
|
|
segue.destination.popoverPresentationController?.delegate = self
|
2020-02-18 06:57:35 -05:00
|
|
|
} else if segue.identifier == "showInfo" {
|
2020-05-24 23:01:11 -04:00
|
|
|
guard let newViewController = segue.destination as? PostInfoViewController else {
|
2020-03-16 12:10:53 -04:00
|
|
|
return
|
|
|
|
}
|
2020-02-18 06:57:35 -05:00
|
|
|
|
|
|
|
newViewController.post = self.post as? Post
|
|
|
|
newViewController.image = self.image
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func playAction(_ sender: Any) {
|
2020-03-16 12:19:36 -04:00
|
|
|
if((post?.value(forKey: "type") as? String) == "public.mpeg-4") {
|
|
|
|
let imagePath = documentsPath.appendingPathComponent(post!.value(forKey: "name") as! String).path
|
|
|
|
|
|
|
|
let player = AVPlayer(url: URL(fileURLWithPath: imagePath))
|
|
|
|
|
|
|
|
let controller = AVPlayerViewController()
|
|
|
|
controller.player = player
|
|
|
|
|
|
|
|
present(controller, animated: true) {
|
|
|
|
player.play()
|
|
|
|
}
|
2020-02-18 06:57:35 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-16 13:07:20 -04:00
|
|
|
|
|
|
|
func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {
|
|
|
|
updateWindowTitle()
|
|
|
|
}
|
2020-02-18 06:57:35 -05:00
|
|
|
}
|
|
|
|
|
2020-05-24 23:01:11 -04:00
|
|
|
extension PostDetailViewController {
|
|
|
|
static func loadFromStoryboard() -> PostDetailViewController? {
|
2020-02-18 06:57:35 -05:00
|
|
|
let storyboard = UIStoryboard(name: "Main", bundle: .main)
|
2020-05-24 23:01:11 -04:00
|
|
|
return storyboard.instantiateViewController(withIdentifier: "PostViewController") as? PostDetailViewController
|
2020-02-18 06:57:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#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")
|
|
|
|
|
2020-05-24 23:01:11 -04:00
|
|
|
extension PostDetailViewController: NSToolbarDelegate {
|
2020-02-18 06:57:35 -05:00
|
|
|
@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] {
|
2020-03-16 13:07:20 -04:00
|
|
|
return [NSToolbarItem.Identifier.flexibleSpace, InfoButtonToolbarIdentifier, EditButtonToolbarIdentifier, ShareButtonToolbarIdentifier]
|
2020-02-18 06:57:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|