From 9b5357ccd66158d79ef0c790e60a46c2d1f94de0 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Fri, 27 May 2016 23:30:17 -0500 Subject: [PATCH] Adds Peek & Pop support to EditCheatViewController --- Delta.xcodeproj/project.pbxproj | 4 ++ Delta/Emulation/EmulationViewController.swift | 5 -- .../Extensions/UIViewController+PeekPop.swift | 18 ++++++ .../Cheats/CheatsViewController.swift | 30 ++++++++++ .../Cheats/EditCheatViewController.swift | 57 ++++++++++++++++++- 5 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 Delta/Extensions/UIViewController+PeekPop.swift diff --git a/Delta.xcodeproj/project.pbxproj b/Delta.xcodeproj/project.pbxproj index 8f2ed3c..4dd238c 100644 --- a/Delta.xcodeproj/project.pbxproj +++ b/Delta.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ AF0535CD7331785FA15E0864 /* Pods.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 22506DA00971C4300AF90A35 /* Pods.framework */; }; + BF04A5A31CF8E61C00B4A267 /* UIViewController+PeekPop.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF04A5A21CF8E61C00B4A267 /* UIViewController+PeekPop.swift */; }; BF090CF41B490D8300DCAB45 /* UIDevice+Vibration.m in Sources */ = {isa = PBXBuildFile; fileRef = BF090CF31B490D8300DCAB45 /* UIDevice+Vibration.m */; }; BF0CDDAD1C8155D200640168 /* LoadImageOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF0CDDAC1C8155D200640168 /* LoadImageOperation.swift */; }; BF107EC41BF413F000E0C32C /* GamesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF107EC31BF413F000E0C32C /* GamesViewController.swift */; }; @@ -128,6 +129,7 @@ 0340C4EC8B47535482F7F1BB /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = ""; }; 22506DA00971C4300AF90A35 /* Pods.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 685E0D2F62E4246995A02970 /* Pods.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.release.xcconfig; path = "Pods/Target Support Files/Pods/Pods.release.xcconfig"; sourceTree = ""; }; + BF04A5A21CF8E61C00B4A267 /* UIViewController+PeekPop.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIViewController+PeekPop.swift"; sourceTree = ""; }; BF090CF11B490D8300DCAB45 /* Delta-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Delta-Bridging-Header.h"; sourceTree = ""; }; BF090CF21B490D8300DCAB45 /* UIDevice+Vibration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIDevice+Vibration.h"; sourceTree = ""; }; BF090CF31B490D8300DCAB45 /* UIDevice+Vibration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIDevice+Vibration.m"; sourceTree = ""; }; @@ -235,6 +237,7 @@ BF090CF31B490D8300DCAB45 /* UIDevice+Vibration.m */, BF797A2C1C2D339F00F1A000 /* UILabel+FontSize.swift */, BF7AE8091C2E8C7600B1B5BC /* UIColor+Delta.swift */, + BF04A5A21CF8E61C00B4A267 /* UIViewController+PeekPop.swift */, ); path = Extensions; sourceTree = ""; @@ -737,6 +740,7 @@ BFFA71DD1AAC406100EE9DD1 /* AppDelegate.swift in Sources */, BF7AE81E1C2E984300B1B5BC /* GridCollectionViewCell.swift in Sources */, BF34FA111CF1899D006624C7 /* CheatTextView.swift in Sources */, + BF04A5A31CF8E61C00B4A267 /* UIViewController+PeekPop.swift in Sources */, BF4566E81BC090B6007BFA1A /* Model.xcdatamodeld in Sources */, BFDE393C1BC0CEDF003F72E8 /* Game.swift in Sources */, BF34FA071CF0F510006624C7 /* EditCheatViewController.swift in Sources */, diff --git a/Delta/Emulation/EmulationViewController.swift b/Delta/Emulation/EmulationViewController.swift index 4d91ff3..b724e80 100644 --- a/Delta/Emulation/EmulationViewController.swift +++ b/Delta/Emulation/EmulationViewController.swift @@ -46,11 +46,6 @@ class EmulationViewController: UIViewController @IBOutlet private var controllerViewHeightConstraint: NSLayoutConstraint! - private var isPreviewing: Bool { - guard let presentationController = self.presentationController else { return false } - return NSStringFromClass(presentationController.dynamicType).containsString("PreviewPresentation") - } - private var pauseViewController: PauseViewController? private var context = CIContext(options: [kCIContextWorkingColorSpace: NSNull()]) diff --git a/Delta/Extensions/UIViewController+PeekPop.swift b/Delta/Extensions/UIViewController+PeekPop.swift new file mode 100644 index 0000000..4d2f774 --- /dev/null +++ b/Delta/Extensions/UIViewController+PeekPop.swift @@ -0,0 +1,18 @@ +// +// UIViewController+PeekPop.swift +// Delta +// +// Created by Riley Testut on 5/27/16. +// Copyright © 2016 Riley Testut. All rights reserved. +// + +import Foundation + +extension UIViewController +{ + var isPreviewing: Bool + { + guard let presentationController = self.presentationController else { return false } + return NSStringFromClass(presentationController.dynamicType).containsString("PreviewPresentation") + } +} \ No newline at end of file diff --git a/Delta/Pause Menu/Cheats/CheatsViewController.swift b/Delta/Pause Menu/Cheats/CheatsViewController.swift index e931d73..2a4f1dd 100644 --- a/Delta/Pause Menu/Cheats/CheatsViewController.swift +++ b/Delta/Pause Menu/Cheats/CheatsViewController.swift @@ -49,6 +49,8 @@ extension CheatsViewController self.backgroundView.detailTextLabel.text = NSLocalizedString("You can add a new cheat by pressing the + button in the top right.", comment: "") self.backgroundView.detailTextLabel.textColor = UIColor.whiteColor() self.tableView.backgroundView = self.backgroundView + + self.registerForPreviewingWithDelegate(self, sourceView: self.tableView) } override func viewWillAppear(animated: Bool) @@ -241,6 +243,29 @@ extension CheatsViewController } } +//MARK: - - +extension CheatsViewController: UIViewControllerPreviewingDelegate +{ + func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? + { + guard let indexPath = self.tableView.indexPathForRowAtPoint(location) else { return nil } + + let frame = self.tableView.rectForRowAtIndexPath(indexPath) + previewingContext.sourceRect = frame + + let cheat = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Cheat + + let editCheatViewController = self.makeEditCheatViewController(cheat: cheat) + return editCheatViewController + } + + func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) + { + self.presentViewController(RSTContainInNavigationController(viewControllerToCommit), animated: true, completion: nil) + } +} + +//MARK: - - extension CheatsViewController: EditCheatViewControllerDelegate { func editCheatViewController(editCheatViewController: EditCheatViewController, activateCheat cheat: Cheat, previousCheat: Cheat?) throws @@ -267,6 +292,11 @@ extension CheatsViewController: EditCheatViewControllerDelegate }) } } + + func editCheatViewController(editCheatViewController: EditCheatViewController, deactivateCheat cheat: Cheat) + { + let _ = try? self.delegate.cheatsViewController(self, didDeactivateCheat: cheat) + } } //MARK: - - diff --git a/Delta/Pause Menu/Cheats/EditCheatViewController.swift b/Delta/Pause Menu/Cheats/EditCheatViewController.swift index 438e3af..8ad87ae 100644 --- a/Delta/Pause Menu/Cheats/EditCheatViewController.swift +++ b/Delta/Pause Menu/Cheats/EditCheatViewController.swift @@ -15,6 +15,7 @@ import Roxas protocol EditCheatViewControllerDelegate: class { func editCheatViewController(editCheatViewController: EditCheatViewController, activateCheat cheat: Cheat, previousCheat: Cheat?) throws + func editCheatViewController(editCheatViewController: EditCheatViewController, deactivateCheat cheat: Cheat) } private extension EditCheatViewController @@ -124,6 +125,18 @@ extension EditCheatViewController self.updateSaveButtonState() } + override func viewDidAppear(animated: Bool) + { + super.viewDidAppear(animated) + + // This matters when going from peek -> pop + // Otherwise, has no effect because viewDidLayoutSubviews has already been called + if self.appearing && !self.isPreviewing + { + self.nameTextField.becomeFirstResponder() + } + } + override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() @@ -136,17 +149,59 @@ extension EditCheatViewController self.codeTextView.textContainer.lineFragmentPadding = 0 } - if self.appearing + if self.appearing && !self.isPreviewing { self.nameTextField.becomeFirstResponder() } } + override func previewActionItems() -> [UIPreviewActionItem] + { + guard let cheat = self.cheat else { return [] } + + let copyCodeAction = UIPreviewAction(title: NSLocalizedString("Copy Code", comment: ""), style: .Default) { (action, viewController) in + UIPasteboard.generalPasteboard().string = cheat.code + } + + let presentingViewController = self.presentingViewController + + let editCheatAction = UIPreviewAction(title: NSLocalizedString("Edit", comment: ""), style: .Default) { (action, viewController) in + // Delaying until next run loop prevents self from being dismissed immediately + dispatch_async(dispatch_get_main_queue()) { + presentingViewController?.presentViewController(RSTContainInNavigationController(viewController), animated: true, completion: nil) + } + } + + let deleteAction = UIPreviewAction(title: NSLocalizedString("Delete", comment: ""), style: .Destructive) { [unowned self] (action, viewController) in + self.delegate?.editCheatViewController(self, deactivateCheat: cheat) + + let backgroundContext = DatabaseManager.sharedManager.backgroundManagedObjectContext() + backgroundContext.performBlock { + let temporaryCheat = backgroundContext.objectWithID(cheat.objectID) + backgroundContext.deleteObject(temporaryCheat) + backgroundContext.saveWithErrorLogging() + } + } + + let cancelDeleteAction = UIPreviewAction(title: NSLocalizedString("Cancel", comment: ""), style: .Default) { (action, viewController) in + } + + let deleteActionGroup = UIPreviewActionGroup(title: NSLocalizedString("Delete", comment: ""), style: .Destructive, actions: [deleteAction, cancelDeleteAction]) + + return [copyCodeAction, editCheatAction, deleteActionGroup] + } + override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } + + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) + { + self.nameTextField.resignFirstResponder() + self.codeTextView.resignFirstResponder() + } } private extension EditCheatViewController