2020-02-18 06:57:35 -05:00
|
|
|
import UIKit
|
|
|
|
import CoreData
|
|
|
|
|
|
|
|
class SearchViewController: UIViewController {
|
2020-05-24 22:55:30 -04:00
|
|
|
@IBOutlet weak var collectionView: UICollectionView!
|
|
|
|
|
|
|
|
var postManager: PostsManager?
|
2020-02-18 06:57:35 -05:00
|
|
|
|
|
|
|
@IBOutlet weak var searchField: UITextField!
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
2020-05-24 23:51:08 -04:00
|
|
|
postManager = PostsManager(viewController: self, collectionView: collectionView, tag: nil)
|
2020-02-18 06:57:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
|
|
|
if segue.identifier == "showPost" {
|
2020-05-24 23:01:11 -04:00
|
|
|
let newViewController = segue.destination as! PostDetailViewController
|
2020-02-18 06:57:35 -05:00
|
|
|
let index = self.collectionView.indexPathsForSelectedItems?.first
|
|
|
|
|
2020-05-24 22:55:30 -04:00
|
|
|
newViewController.post = self.postManager?.posts[index!.row]
|
2020-02-18 06:57:35 -05:00
|
|
|
newViewController.image = (self.collectionView.cellForItem(at: index!) as! PostViewCell).imageView.image;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func searchAction(_ sender: Any) {
|
2020-05-24 22:55:30 -04:00
|
|
|
postManager?.setTag(tag: searchField.text)
|
2020-02-18 06:57:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
|
|
|
|
if identifier == "showPost" {
|
|
|
|
#if targetEnvironment(macCatalyst)
|
|
|
|
let index = self.collectionView.indexPathsForSelectedItems?.first
|
|
|
|
|
2020-05-24 22:55:30 -04:00
|
|
|
let post = postManager?.posts[index!.row]
|
2020-02-18 06:57:35 -05:00
|
|
|
|
|
|
|
let activity = NSUserActivity(activityType: "post")
|
2020-05-24 22:55:30 -04:00
|
|
|
activity.userInfo = ["name": post?.value(forKey: "name") as! String]
|
2020-02-18 06:57:35 -05:00
|
|
|
activity.isEligibleForHandoff = true
|
|
|
|
|
|
|
|
UIApplication.shared.requestSceneSessionActivation(nil, userActivity: activity, options: nil, errorHandler: nil)
|
|
|
|
|
|
|
|
return false
|
|
|
|
#else
|
|
|
|
return true
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.shouldPerformSegue(withIdentifier: identifier, sender: sender)
|
|
|
|
}
|
|
|
|
}
|