From 50ec9aeb1b108a9c02e4d975cad3f2901d140ac6 Mon Sep 17 00:00:00 2001 From: redstrate <54911369+redstrate@users.noreply.github.com> Date: Sun, 24 May 2020 23:32:42 -0400 Subject: [PATCH] Open a new window when clicking on a tag on macOS --- Gallery/Base.lproj/Main.storyboard | 2 +- Gallery/EditTagsViewController.swift | 26 ++++++++++++++++++++++++++ Gallery/HomeViewController.swift | 26 ++++++++++++++++++-------- Gallery/PostDetailViewController.swift | 10 ---------- Gallery/PostsManager.swift | 2 +- Gallery/SceneDelegate.swift | 20 ++++++++++++++++++++ 6 files changed, 66 insertions(+), 20 deletions(-) diff --git a/Gallery/Base.lproj/Main.storyboard b/Gallery/Base.lproj/Main.storyboard index ec659c1..dc6e788 100644 --- a/Gallery/Base.lproj/Main.storyboard +++ b/Gallery/Base.lproj/Main.storyboard @@ -27,7 +27,7 @@ - + diff --git a/Gallery/EditTagsViewController.swift b/Gallery/EditTagsViewController.swift index fd1ceed..0cfee08 100644 --- a/Gallery/EditTagsViewController.swift +++ b/Gallery/EditTagsViewController.swift @@ -70,6 +70,32 @@ class EditTagsViewController: UIViewController, UITableViewDelegate, UITableView newViewController.tag = (post?.tags![index!.row] as! Tag).name } } + + override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool { + if identifier == "showTag" { + #if targetEnvironment(macCatalyst) + guard let cell = sender as? TagViewCell else { + return false + } + + guard let name = cell.label.text else { + return false + } + + let activity = NSUserActivity(activityType: "postsOf") + activity.userInfo = ["tags": name] + 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) + } } extension EditTagsViewController { diff --git a/Gallery/HomeViewController.swift b/Gallery/HomeViewController.swift index 57a2365..07e4450 100644 --- a/Gallery/HomeViewController.swift +++ b/Gallery/HomeViewController.swift @@ -8,7 +8,14 @@ class HomeViewController: UIViewController, UIDocumentPickerDelegate { let documentsPath : URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].absoluteURL - let windowTitle = "Home" + var tags: String? + + func updateWindowTitle() { + let windowTitle = tags ?? "All Posts" + + self.view.window?.windowScene!.title = windowTitle + navigationItem.title = windowTitle + } func importFile(path: URL) { guard let appDelegate = @@ -53,14 +60,10 @@ class HomeViewController: UIViewController, UIDocumentPickerDelegate { override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) + + collectionManager = PostsManager(collectionView: collectionView, tag: tags) - collectionManager = PostsManager(collectionView: collectionView, tag: nil) - } - - override func viewDidAppear(_ animated: Bool) { - super.viewDidAppear(animated) - - self.view.window?.windowScene!.title = windowTitle + updateWindowTitle() } @IBAction func importAction(_ sender: Any) { @@ -113,3 +116,10 @@ class HomeViewController: UIViewController, UIDocumentPickerDelegate { return super.shouldPerformSegue(withIdentifier: identifier, sender: sender) } } + +extension HomeViewController { + static func loadFromStoryboard() -> HomeViewController? { + let storyboard = UIStoryboard(name: "Main", bundle: .main) + return storyboard.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController + } +} diff --git a/Gallery/PostDetailViewController.swift b/Gallery/PostDetailViewController.swift index 4893ef2..450f2a9 100644 --- a/Gallery/PostDetailViewController.swift +++ b/Gallery/PostDetailViewController.swift @@ -64,17 +64,7 @@ class PostDetailViewController: UIViewController, UIPopoverPresentationControlle } } - #if !targetEnvironment(macCatalyst) updateWindowTitle() - #endif - } - - override func viewDidAppear(_ animated: Bool) { - super.viewDidAppear(animated) - - #if targetEnvironment(macCatalyst) - updateWindowTitle() - #endif } @objc func closePopup() { diff --git a/Gallery/PostsManager.swift b/Gallery/PostsManager.swift index a6452c6..9d16578 100644 --- a/Gallery/PostsManager.swift +++ b/Gallery/PostsManager.swift @@ -110,7 +110,7 @@ class PostsManager: NSObject, UICollectionViewDataSource, UICollectionViewDelega managedContext = appDelegate.persistentContainer.viewContext - setTag(tag: nil) + setTag(tag: tag) } func reload(request: NSFetchRequest) { diff --git a/Gallery/SceneDelegate.swift b/Gallery/SceneDelegate.swift index 2a0d657..dbf960a 100644 --- a/Gallery/SceneDelegate.swift +++ b/Gallery/SceneDelegate.swift @@ -51,6 +51,14 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { return nil } + func getTagsFromActivity(activity: NSUserActivity ) -> String? { + if let photoID = activity.userInfo?["tags"] as? String { + return photoID + } + + return nil + } + func configure(window: UIWindow?, with activity: NSUserActivity) -> Bool { if activity.activityType == "post" { if let post = getPostFromActivity(activity: activity) { @@ -73,6 +81,18 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { if let navigationController = window?.rootViewController as? UINavigationController { navigationController.pushViewController(editTagsController, animated: false) + return true + } + } + } + } else if activity.activityType == "postsOf" { + if let tags = getTagsFromActivity(activity: activity) { + if let homeController = HomeViewController.loadFromStoryboard() { + homeController.tags = tags + + if let navigationController = window?.rootViewController as? UINavigationController { + navigationController.pushViewController(homeController, animated: false) + return true } }