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/SceneDelegate.swift

85 lines
3.2 KiB
Swift
Raw Normal View History

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()
}
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" {
if let post = getPostFromActivity(activity: activity) {
if let photoDetailViewController = PostDetailViewController.loadFromStoryboard() {
photoDetailViewController.post = post
photoDetailViewController.isPopup = true
2020-02-18 06:57:35 -05:00
if let navigationController = window?.rootViewController as? UINavigationController {
navigationController.pushViewController(photoDetailViewController, animated: false)
2020-03-16 12:10:53 -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
return true
2020-02-18 06:57:35 -05:00
}
}
}
}
return false
}
}