Improve swift safety
This commit is contained in:
parent
52e6ffc8d0
commit
1b7c66e206
5 changed files with 68 additions and 62 deletions
|
@ -69,6 +69,8 @@ class PostCollectionView: UICollectionView, UICollectionViewDataSource, UICollec
|
||||||
|
|
||||||
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath as IndexPath) as! PostViewCell
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath as IndexPath) as! PostViewCell
|
||||||
|
|
||||||
|
cell.name = post.value(forKey: "name") as? String
|
||||||
|
|
||||||
let imagePath = documentsPath.appendingPathComponent(post.value(forKey: "name") as! String).path
|
let imagePath = documentsPath.appendingPathComponent(post.value(forKey: "name") as! String).path
|
||||||
|
|
||||||
if(FileManager.default.fileExists(atPath: imagePath)) {
|
if(FileManager.default.fileExists(atPath: imagePath)) {
|
||||||
|
@ -279,8 +281,10 @@ class PostCollectionView: UICollectionView, UICollectionViewDataSource, UICollec
|
||||||
let model = posts[indexPath.item]
|
let model = posts[indexPath.item]
|
||||||
let itemProvider = NSItemProvider(object: (cellForItem(at: indexPath) as! PostViewCell).imageView.image!)
|
let itemProvider = NSItemProvider(object: (cellForItem(at: indexPath) as! PostViewCell).imageView.image!)
|
||||||
itemProvider.suggestedName = model.value(forKey: "name") as? String
|
itemProvider.suggestedName = model.value(forKey: "name") as? String
|
||||||
|
|
||||||
let dragItem = UIDragItem(itemProvider: itemProvider)
|
let dragItem = UIDragItem(itemProvider: itemProvider)
|
||||||
dragItem.localObject = model //We can set the localObject property for convenience
|
dragItem.localObject = model //We can set the localObject property for convenience
|
||||||
|
|
||||||
return [dragItem]
|
return [dragItem]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
class PostViewCell: UICollectionViewCell {
|
class PostViewCell: UICollectionViewCell {
|
||||||
|
var name: String?
|
||||||
|
|
||||||
@IBOutlet weak var imageView: UIImageView!
|
@IBOutlet weak var imageView: UIImageView!
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,19 +4,20 @@ import AVFoundation
|
||||||
import AVKit
|
import AVKit
|
||||||
|
|
||||||
class PostViewController: UIViewController {
|
class PostViewController: UIViewController {
|
||||||
@IBOutlet weak var imageView: UIImageView!
|
@IBOutlet weak var imageView: UIImageView?
|
||||||
|
@IBOutlet weak var shareButton: UIBarButtonItem?
|
||||||
|
|
||||||
var post: NSManagedObject?
|
var post: NSManagedObject?
|
||||||
var image: UIImage?
|
var image: UIImage?
|
||||||
var isPopup: Bool = false
|
var isPopup: Bool = false
|
||||||
@IBOutlet weak var shareButton: UIBarButtonItem!
|
|
||||||
|
|
||||||
let documentsPath : URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].absoluteURL
|
let documentsPath : URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].absoluteURL
|
||||||
|
|
||||||
override func viewWillAppear(_ animated: Bool) {
|
override func viewWillAppear(_ animated: Bool) {
|
||||||
super.viewWillAppear(animated)
|
super.viewWillAppear(animated)
|
||||||
|
|
||||||
if(image == nil) {
|
if post != nil {
|
||||||
|
if image == nil {
|
||||||
let imagePath = documentsPath.appendingPathComponent(post!.value(forKey: "name") as! String).path
|
let imagePath = documentsPath.appendingPathComponent(post!.value(forKey: "name") as! String).path
|
||||||
|
|
||||||
if((post?.value(forKey: "type") as? String) == "public.mpeg-4") {
|
if((post?.value(forKey: "type") as? String) == "public.mpeg-4") {
|
||||||
|
@ -26,12 +27,13 @@ class PostViewController: UIViewController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
imageView.image = self.image
|
imageView?.image = self.image
|
||||||
|
|
||||||
if(isPopup) {
|
if(isPopup) {
|
||||||
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(closePopup))
|
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(closePopup))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@objc func closePopup() {
|
@objc func closePopup() {
|
||||||
UIApplication.shared.requestSceneSessionDestruction((self.view.window?.windowScene!.session)!, options: nil, errorHandler: nil)
|
UIApplication.shared.requestSceneSessionDestruction((self.view.window?.windowScene!.session)!, options: nil, errorHandler: nil)
|
||||||
|
@ -50,11 +52,15 @@ class PostViewController: UIViewController {
|
||||||
|
|
||||||
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
||||||
if segue.identifier == "showTags" {
|
if segue.identifier == "showTags" {
|
||||||
let newViewController = segue.destination as! EditTagsViewController
|
guard let newViewController = segue.destination as? EditTagsViewController else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
newViewController.post = self.post as? Post
|
newViewController.post = self.post as? Post
|
||||||
} else if segue.identifier == "showInfo" {
|
} else if segue.identifier == "showInfo" {
|
||||||
let newViewController = segue.destination as! InfoViewController
|
guard let newViewController = segue.destination as? InfoViewController else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
newViewController.post = self.post as? Post
|
newViewController.post = self.post as? Post
|
||||||
newViewController.image = self.image
|
newViewController.image = self.image
|
||||||
|
@ -87,6 +93,7 @@ extension PostViewController {
|
||||||
|
|
||||||
|
|
||||||
#if targetEnvironment(macCatalyst)
|
#if targetEnvironment(macCatalyst)
|
||||||
|
|
||||||
private let EditButtonToolbarIdentifier = NSToolbarItem.Identifier(rawValue: "OurButton")
|
private let EditButtonToolbarIdentifier = NSToolbarItem.Identifier(rawValue: "OurButton")
|
||||||
private let ShareButtonToolbarIdentifier = NSToolbarItem.Identifier(rawValue: "OurButton2")
|
private let ShareButtonToolbarIdentifier = NSToolbarItem.Identifier(rawValue: "OurButton2")
|
||||||
private let InfoButtonToolbarIdentifier = NSToolbarItem.Identifier(rawValue: "OurButton3")
|
private let InfoButtonToolbarIdentifier = NSToolbarItem.Identifier(rawValue: "OurButton3")
|
||||||
|
@ -144,4 +151,5 @@ extension PostViewController: NSToolbarDelegate {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -12,34 +12,21 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
||||||
}
|
}
|
||||||
|
|
||||||
#if targetEnvironment(macCatalyst)
|
#if targetEnvironment(macCatalyst)
|
||||||
guard let windowScene = (scene as? UIWindowScene) else { return }
|
guard let windowScene = (scene as? UIWindowScene) else {
|
||||||
let toolbar = NSToolbar(identifier: "MyToolbar")
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let toolbar = NSToolbar(identifier: "Toolbar")
|
||||||
toolbar.delegate = (window?.rootViewController as! UINavigationController).topViewController as? NSToolbarDelegate
|
toolbar.delegate = (window?.rootViewController as! UINavigationController).topViewController as? NSToolbarDelegate
|
||||||
windowScene.titlebar!.toolbar = toolbar
|
|
||||||
windowScene.titlebar!.titleVisibility = .hidden
|
|
||||||
toolbar.allowsUserCustomization = true
|
toolbar.allowsUserCustomization = true
|
||||||
|
|
||||||
|
windowScene.titlebar!.toolbar = toolbar
|
||||||
|
windowScene.titlebar!.titleVisibility = .hidden
|
||||||
|
|
||||||
(window?.rootViewController as! UINavigationController).navigationBar.isHidden = true
|
(window?.rootViewController as! UINavigationController).navigationBar.isHidden = true
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
func sceneDidDisconnect(_ scene: UIScene) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func sceneDidBecomeActive(_ scene: UIScene) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func sceneWillResignActive(_ scene: UIScene) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func sceneWillEnterForeground(_ scene: UIScene) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func sceneDidEnterBackground(_ scene: UIScene) {
|
func sceneDidEnterBackground(_ scene: UIScene) {
|
||||||
(UIApplication.shared.delegate as? AppDelegate)?.saveContext()
|
(UIApplication.shared.delegate as? AppDelegate)?.saveContext()
|
||||||
}
|
}
|
||||||
|
@ -48,14 +35,12 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
||||||
if activity.activityType == "post" {
|
if activity.activityType == "post" {
|
||||||
if let photoID = activity.userInfo?["name"] as? String {
|
if let photoID = activity.userInfo?["name"] as? String {
|
||||||
if let photoDetailViewController = PostViewController.loadFromStoryboard() {
|
if let photoDetailViewController = PostViewController.loadFromStoryboard() {
|
||||||
|
|
||||||
guard let appDelegate =
|
guard let appDelegate =
|
||||||
UIApplication.shared.delegate as? AppDelegate else {
|
UIApplication.shared.delegate as? AppDelegate else {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
let managedContext =
|
let managedContext = appDelegate.persistentContainer.viewContext
|
||||||
appDelegate.persistentContainer.viewContext
|
|
||||||
|
|
||||||
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Post")
|
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Post")
|
||||||
|
|
||||||
|
@ -65,7 +50,11 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
||||||
do {
|
do {
|
||||||
let result = try managedContext.fetch(request)
|
let result = try managedContext.fetch(request)
|
||||||
|
|
||||||
photoDetailViewController.post = result[0] as? NSManagedObject
|
guard let object = result[0] as? NSManagedObject else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
photoDetailViewController.post = object
|
||||||
photoDetailViewController.isPopup = true
|
photoDetailViewController.isPopup = true
|
||||||
|
|
||||||
if let navigationController = window?.rootViewController as? UINavigationController {
|
if let navigationController = window?.rootViewController as? UINavigationController {
|
||||||
|
|
|
@ -83,12 +83,16 @@ class ViewController: UIViewController, UIDocumentPickerDelegate {
|
||||||
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
|
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
|
||||||
if identifier == "showPost" {
|
if identifier == "showPost" {
|
||||||
#if targetEnvironment(macCatalyst)
|
#if targetEnvironment(macCatalyst)
|
||||||
let index = self.collectionView.indexPathsForSelectedItems?.first
|
guard let cell = sender as? PostViewCell else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
let post = self.collectionView.posts[index!.row]
|
guard let name = cell.name else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
let activity = NSUserActivity(activityType: "post")
|
let activity = NSUserActivity(activityType: "post")
|
||||||
activity.userInfo = ["name": post.value(forKey: "name") as! String]
|
activity.userInfo = ["name": name]
|
||||||
activity.isEligibleForHandoff = true
|
activity.isEligibleForHandoff = true
|
||||||
|
|
||||||
UIApplication.shared.requestSceneSessionActivation(nil, userActivity: activity, options: nil, errorHandler: nil)
|
UIApplication.shared.requestSceneSessionActivation(nil, userActivity: activity, options: nil, errorHandler: nil)
|
||||||
|
|
Reference in a new issue