2020-02-18 06:57:35 -05:00
|
|
|
import UIKit
|
|
|
|
import CoreData
|
|
|
|
|
|
|
|
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
|
|
|
var window: UIWindow?
|
|
|
|
|
|
|
|
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
|
|
|
|
if let userActivity = connectionOptions.userActivities.first ?? session.stateRestorationActivity {
|
|
|
|
if !configure(window: window, with: userActivity) {
|
|
|
|
print("Failed to restore from \(userActivity)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if targetEnvironment(macCatalyst)
|
2020-05-24 23:14:51 -04:00
|
|
|
guard let windowScene = (scene as? UIWindowScene) else { return }
|
2020-03-16 12:10:53 -04:00
|
|
|
|
2020-05-24 23:14:51 -04:00
|
|
|
if let titlebar = windowScene.titlebar {
|
|
|
|
titlebar.titleVisibility = .hidden
|
|
|
|
titlebar.toolbar = nil
|
|
|
|
}
|
2020-02-18 06:57:35 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
func sceneDidEnterBackground(_ scene: UIScene) {
|
|
|
|
(UIApplication.shared.delegate as? AppDelegate)?.saveContext()
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:14:26 -04:00
|
|
|
func getPostFromActivity(activity: NSUserActivity ) -> Post? {
|
|
|
|
if let photoID = activity.userInfo?["name"] as? String {
|
|
|
|
guard let appDelegate =
|
|
|
|
UIApplication.shared.delegate as? AppDelegate else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let managedContext = appDelegate.persistentContainer.viewContext
|
|
|
|
|
|
|
|
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Post")
|
|
|
|
|
|
|
|
request.predicate = NSPredicate(format: "name = %@", photoID)
|
|
|
|
request.returnsObjectsAsFaults = false
|
|
|
|
|
|
|
|
do {
|
|
|
|
let result = try managedContext.fetch(request)
|
|
|
|
|
|
|
|
return result[0] as? Post;
|
|
|
|
} catch _ as NSError {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-18 06:57:35 -05:00
|
|
|
func configure(window: UIWindow?, with activity: NSUserActivity) -> Bool {
|
|
|
|
if activity.activityType == "post" {
|
2020-05-24 22:14:26 -04:00
|
|
|
if let post = getPostFromActivity(activity: activity) {
|
2020-05-24 23:01:11 -04:00
|
|
|
if let photoDetailViewController = PostDetailViewController.loadFromStoryboard() {
|
2020-05-24 22:14:26 -04:00
|
|
|
photoDetailViewController.post = post
|
|
|
|
photoDetailViewController.isPopup = true
|
2020-02-18 06:57:35 -05:00
|
|
|
|
2020-05-24 22:14:26 -04:00
|
|
|
if let navigationController = window?.rootViewController as? UINavigationController {
|
|
|
|
navigationController.pushViewController(photoDetailViewController, animated: false)
|
2020-03-16 12:10:53 -04:00
|
|
|
|
2020-05-24 22:14:26 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if activity.activityType == "tags" {
|
|
|
|
if let post = getPostFromActivity(activity: activity) {
|
|
|
|
if let editTagsController = EditTagsViewController.loadFromStoryboard() {
|
|
|
|
editTagsController.post = post
|
|
|
|
|
|
|
|
if let navigationController = window?.rootViewController as? UINavigationController {
|
|
|
|
navigationController.pushViewController(editTagsController, animated: false)
|
2020-02-18 06:57:35 -05:00
|
|
|
|
2020-05-24 22:14:26 -04:00
|
|
|
return true
|
2020-02-18 06:57:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|