Adds Peek & Pop support to EditCheatViewController

This commit is contained in:
Riley Testut 2016-05-27 23:30:17 -05:00
parent a80aeed4a4
commit 9b5357ccd6
5 changed files with 108 additions and 6 deletions

View File

@ -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 = "<group>"; };
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 = "<group>"; };
BF04A5A21CF8E61C00B4A267 /* UIViewController+PeekPop.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIViewController+PeekPop.swift"; sourceTree = "<group>"; };
BF090CF11B490D8300DCAB45 /* Delta-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Delta-Bridging-Header.h"; sourceTree = "<group>"; };
BF090CF21B490D8300DCAB45 /* UIDevice+Vibration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIDevice+Vibration.h"; sourceTree = "<group>"; };
BF090CF31B490D8300DCAB45 /* UIDevice+Vibration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIDevice+Vibration.m"; sourceTree = "<group>"; };
@ -235,6 +237,7 @@
BF090CF31B490D8300DCAB45 /* UIDevice+Vibration.m */,
BF797A2C1C2D339F00F1A000 /* UILabel+FontSize.swift */,
BF7AE8091C2E8C7600B1B5BC /* UIColor+Delta.swift */,
BF04A5A21CF8E61C00B4A267 /* UIViewController+PeekPop.swift */,
);
path = Extensions;
sourceTree = "<group>";
@ -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 */,

View File

@ -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()])

View File

@ -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")
}
}

View File

@ -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: - <UIViewControllerPreviewingDelegate> -
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: - <EditCheatViewControllerDelegate> -
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: - <NSFetchedResultsControllerDelegate> -

View File

@ -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