Skip to content

Commit

Permalink
Fix critical bug where the SSH channel got revoked after locking the …
Browse files Browse the repository at this point in the history
…screen.

Add temporary shortcut for removing favorites.
  • Loading branch information
MrStarktastic committed Dec 5, 2017
1 parent 2fc6abd commit d0d0474
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 42 deletions.
18 changes: 16 additions & 2 deletions AquaDoor/DoorTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class DoorTask {
private var start: (() -> Void)?
private var completion: ((Result) -> Void)?

private var tryingAgain = false
private var lastCommand: String!

private var delegate: LeaveDoorOpenDelegate?

public init(delegate: LeaveDoorOpenDelegate?) {
Expand All @@ -39,15 +42,26 @@ class DoorTask {

private func sendCommand(_ command: String) {
start?()
start = nil
lastCommand = command

SSHCommander.prepare(success: { instance in
instance.execute(command: command, completion: self.parseOutput)
}) { error in self.completion?(.error(error)) }
}, failure: { error in
self.completion?(.error(error)) }, force: tryingAgain)
}

private func parseOutput(_ output: String?) {
guard let output = output else {
completion?(.error(.executionFailed))
if tryingAgain {
tryingAgain = false
completion?(.error(.executionFailed))
return
}

tryingAgain = true
sendCommand(lastCommand)

return
}

Expand Down
3 changes: 3 additions & 0 deletions AquaDoor/EmptyFavoritesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@ class EmptyFavoritesViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

// TODO: Consider using a gif. [Gifu](https://github.com/kaishin/Gifu)
player = AVPlayer(url: Bundle.main.url(forResource: "fish_and_doors_anim",
withExtension: "mov")!)
(playerView.layer as! AVPlayerLayer).player = player
player.volume = 0
player.actionAtItemEnd = .none
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)

instructionLabel.isUserInteractionEnabled = true
instructionLabel.addGestureRecognizer(UITapGestureRecognizer(
target: self, action: #selector(didTapInstructionLabel)))
}

override func viewDidDisappear(_ animated: Bool) {

super.viewDidDisappear(animated)

NotificationCenter.default.removeObserver(self)
Expand Down
5 changes: 5 additions & 0 deletions AquaDoor/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public extension ImplicitlyUnwrappedOptional where Wrapped == Int {
defer { x = x + 1 }
return x
}

static prefix func --(x: inout Int!) -> Int! {
x = x - 1

This comment has been minimized.

Copy link
@yossi-k

yossi-k Dec 5, 2017

Collaborator

@MrStarktastic Fucking insane

return x
}
}

public extension UIImage {
Expand Down
17 changes: 17 additions & 0 deletions AquaDoor/FavoriteCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,21 @@ class FavoriteCollectionViewCell: UICollectionViewCell {
}

var titleText: String { get { return title.text! }}

var delegate: FavoriteCollectionViewCellDelegate?

override func layoutSubviews() {
super.layoutSubviews()
addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(didLongPress)))
}

@objc private func didLongPress(_ recognizer: UILongPressGestureRecognizer) {
if recognizer.state == .began {
delegate?.didLongPress(cell: self)
}
}
}

protocol FavoriteCollectionViewCellDelegate {
func didLongPress(cell: FavoriteCollectionViewCell)
}
17 changes: 15 additions & 2 deletions AquaDoor/FavoritesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit

class FavoritesViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout,
CredentialAlertDelegate, DoorActionDelegate, EmptyStateDelegate {
CredentialAlertDelegate, DoorActionDelegate, EmptyStateDelegate, FavoriteCollectionViewCellDelegate {
private let sectionCount = 1
private let itemsPerRow: CGFloat = 2
private let cellInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
Expand Down Expand Up @@ -65,6 +65,8 @@ CredentialAlertDelegate, DoorActionDelegate, EmptyStateDelegate {
cell.gradient = .get(byId: door.colorId)
}

if cell.delegate == nil { cell.delegate = self }

return cell
}

Expand All @@ -84,10 +86,21 @@ CredentialAlertDelegate, DoorActionDelegate, EmptyStateDelegate {
return cellInsets.left
}

internal func didLongPress(cell: FavoriteCollectionViewCell) {
do { try database.delete(doorWithId: cell.doorId) }
catch { return }

if --doorCount == 0 { presentChildViewController(withIdentifier: "EmptyFavorites") }

if let indexPath = collectionView?.indexPath(for: cell) {
collectionView!.deleteItems(at: [indexPath])
}
}

/// Makes sure that an SSH session is created (if possible) before actual usage
/// and does not care if the error is not related to credentials.
private func prepareSSHCommander() {
SSHCommander.prepare(success: nil, failure: credentialErrorDidOccur)
SSHCommander.prepare(success: nil, failure: credentialErrorDidOccur, force: false)
}

private func presentCredentialsAlertController(withTitle title: String) {
Expand Down
8 changes: 4 additions & 4 deletions AquaDoor/SSHCommander.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class SSHCommander {

private init() {}

public static func prepare(success: ((SSHCommander) -> Void)?, failure: @escaping (SSHError) -> Void) {
public static func prepare(success: ((SSHCommander) -> Void)?, failure: @escaping (SSHError) -> Void, force: Bool) {
guard credentials != nil else {
failure(.credentialsNotFound)
return
}

shared.sshQueue.async {
if let session = shared.session, session.isConnected && session.isAuthorized {
if !force, let session = shared.session, session.isConnected && session.isAuthorized {
shared.mainQueue.async { success?(shared) }
return
}
Expand All @@ -41,7 +41,7 @@ class SSHCommander {
private func connect(_ success: ((SSHCommander) -> Void)?, _ failure: @escaping (SSHError) -> Void) {
let (username, password) = SSHCommander.credentials!

if let oldSession = self.session { if oldSession.isConnected { oldSession.disconnect() }}
if let oldSession = session { if oldSession.isConnected { oldSession.disconnect(); session = nil }}
let newSession = NMSSHSession(host: host, andUsername: username)!

do {
Expand All @@ -62,7 +62,7 @@ class SSHCommander {
return
}

self.session = newSession
session = newSession
mainQueue.async { success?(.shared) }
}

Expand Down
8 changes: 4 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- KeychainSwift (9.0.2)
- MarqueeLabel/Swift (3.1.3)
- KeychainSwift (10.0.0)
- MarqueeLabel/Swift (3.1.4)
- NMSSH (2.2.8)
- SQLite.swift (0.11.4):
- SQLite.swift/standard (= 0.11.4)
Expand All @@ -15,8 +15,8 @@ DEPENDENCIES:
- UITextField+Shake

SPEC CHECKSUMS:
KeychainSwift: 3ab1d428d0dbff59cd056f1d0bfc640b36c2b081
MarqueeLabel: baa5753dcbd36dca080e1f5c589a665aa5c906f1
KeychainSwift: f9f7910449a0c0fd2cabc889121530dd2c477c33
MarqueeLabel: bf768455fe88d427f71476ebb23f9092b660f40b
NMSSH: b722885a07485a2c03ffa3ea56a7b0308dbdc37c
SQLite.swift: 3e3bee21da701b5b9f87c4a672cb54f233505692
UITextField+Shake: 3e86d67700b9db1915df965e677cec8b8cfda225
Expand Down
7 changes: 4 additions & 3 deletions Pods/KeychainSwift/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Pods/KeychainSwift/Sources/KeychainSwift.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Pods/MarqueeLabel/Sources/Swift/MarqueeLabel.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d0d0474

Please sign in to comment.