-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f298f8e
commit 668440c
Showing
10 changed files
with
401 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
ViewControllerThinning/Models/Behaviours/AuthFormBehaviour.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// | ||
// AuthFormBehaviour.swift | ||
// ViewControllerThinning | ||
// | ||
// Created by Ilya Puchka on 09.10.15. | ||
// Copyright © 2015 Ilya Puchka. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import SwiftNetworking | ||
|
||
class AuthFormBehaviour: NSObject, FormBehaviour { | ||
|
||
var apiClient: APIClient = APIClient(baseURL: NSURL(string: "http://localhost")!) | ||
|
||
override init() {} | ||
|
||
@IBOutlet | ||
var userNameInput: UITextField! { | ||
didSet { | ||
userNameInput.delegate = self | ||
} | ||
} | ||
|
||
@IBOutlet | ||
var passwordInput: UITextField! { | ||
didSet { | ||
passwordInput.delegate = self | ||
} | ||
} | ||
|
||
@IBOutlet | ||
var formFields: [UIView]! | ||
|
||
@IBAction | ||
func submitForm() { | ||
guard let | ||
username = userNameInput.text, | ||
password = passwordInput.text else { | ||
return | ||
} | ||
userNameInput.endEditing(true) | ||
passwordInput.endEditing(true) | ||
login(username, password: password) | ||
} | ||
|
||
var onCancel: (()->())? | ||
|
||
@IBAction | ||
func cancelForm() { | ||
onCancel?() | ||
} | ||
|
||
var onLoggedIn: ((error: NSError?, performedRequest: Bool) -> ())? | ||
|
||
func login(username: String, password: String) { | ||
apiClient.login(username, password: password) { [weak self] (error, performedRequest) -> () in | ||
self?.onLoggedIn?(error: error, performedRequest: performedRequest) | ||
} | ||
} | ||
|
||
} | ||
|
||
extension AuthFormBehaviour: UITextFieldDelegate { | ||
|
||
func textFieldShouldReturn(textField: UITextField) -> Bool { | ||
if textField == formFields.last { | ||
submitForm() | ||
} | ||
else { | ||
goToNextFormField() | ||
} | ||
return true | ||
} | ||
} | ||
|
39 changes: 39 additions & 0 deletions
39
ViewControllerThinning/Models/Behaviours/BackgroundHighlightableView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// BackgroundHighlightableView.swift | ||
// ViewControllerThinning | ||
// | ||
// Created by Ilya Puchka on 09.10.15. | ||
// Copyright © 2015 Ilya Puchka. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
protocol BackgroundHighlightableView: class { | ||
var backgroundColor: UIColor? {get set} | ||
var backgroundColorForHighlightedState: UIColor? {get} | ||
var backgroundColorForNormalState: UIColor? {get} | ||
|
||
var highlighted: Bool {get set} | ||
func setHighlighted(highlighted: Bool, animated: Bool) | ||
func backgroundColor(highlighted: Bool) -> UIColor? | ||
} | ||
|
||
extension BackgroundHighlightableView where Self: UIView { | ||
|
||
func setHighlighted(highlighted: Bool, animated: Bool) { | ||
let view = self as UIView | ||
guard backgroundColorForHighlightedState != nil && | ||
view.backgroundColor != nil else { | ||
return | ||
} | ||
changeAnimated(true, options: [.BeginFromCurrentState]) { | ||
view.backgroundColor = self.backgroundColor(highlighted) | ||
} | ||
} | ||
|
||
func backgroundColor(highlighted: Bool) -> UIColor? { | ||
return highlighted ? backgroundColorForHighlightedState : backgroundColorForNormalState | ||
} | ||
|
||
} | ||
|
51 changes: 51 additions & 0 deletions
51
ViewControllerThinning/Models/Behaviours/FormBehaviour.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// FormBehaviour.swift | ||
// ViewControllerThinning | ||
// | ||
// Created by Ilya Puchka on 09.10.15. | ||
// Copyright © 2015 Ilya Puchka. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
protocol FormBehaviour { | ||
var formFields: [UIView]! {get} | ||
func goToNextFormField() -> UIView? | ||
func currentFormField() -> UIView? | ||
func submitForm() | ||
func cancelForm() | ||
} | ||
|
||
extension FormBehaviour { | ||
|
||
func currentFormField() -> UIView? { | ||
for field in formFields where field.isFirstResponder() { | ||
return field | ||
} | ||
return nil | ||
} | ||
|
||
func goToNextFormField() -> UIView? { | ||
guard let | ||
formFields = self.formFields, | ||
currentField = currentFormField(), | ||
currentFieldIndex = formFields.indexOf(currentField) | ||
where | ||
formFields.count > 1 | ||
else { | ||
return nil | ||
} | ||
|
||
var nextFormField: UIView! = nil | ||
var nextIndex = currentFieldIndex | ||
repeat { | ||
nextIndex = (nextIndex + 1) % formFields.count | ||
nextFormField = formFields[nextIndex] | ||
} while nextFormField.canBecomeFirstResponder() == false && nextIndex != currentFieldIndex | ||
if nextIndex != currentFieldIndex { | ||
nextFormField.becomeFirstResponder() | ||
} | ||
return nextFormField | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
ViewControllerThinning/Views/Animations/ShakeAnimation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// ShakeAnimation.swift | ||
// ViewControllerThinning | ||
// | ||
// Created by Ilya Puchka on 09.10.15. | ||
// Copyright © 2015 Ilya Puchka. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
@objc | ||
protocol Animation { | ||
var duration: Double {get} | ||
var view: UIView? {get} | ||
} | ||
|
||
extension Animation { | ||
func play() { | ||
fatalError("Concrete instances of Animation protocol should provide implementation of this method.") | ||
} | ||
} | ||
|
||
@objc | ||
protocol ShakeAnimation: Animation { | ||
var maxOffset: Double {get set} | ||
var keyPath: String {get set} | ||
} | ||
|
||
extension ShakeAnimation { | ||
func play() { | ||
guard let view = view else { return } | ||
|
||
let animation = CAKeyframeAnimation(keyPath: keyPath) | ||
animation.values = [0, maxOffset, -0.8 * maxOffset, 0.4 * maxOffset, 0] | ||
animation.keyTimes = [ 0, (1 / 6.0), (3 / 6.0), (5 / 6.0), 1 ] | ||
animation.duration = duration ?? view.implicitAnimationDuration | ||
animation.additive = true | ||
view.layer.addAnimation(animation, forKey: "shake") | ||
} | ||
} | ||
|
||
class ShakeAnimationImp: NSObject, ShakeAnimation { | ||
|
||
@IBInspectable | ||
var duration: Double = 0.2 | ||
|
||
@IBInspectable | ||
var maxOffset: Double = 10 | ||
|
||
@IBInspectable | ||
var keyPath: String = "position.x" | ||
|
||
@IBOutlet | ||
weak var view: UIView? | ||
|
||
} | ||
|
Oops, something went wrong.