Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions PasscodeLock/Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ func bundleForResource(name: String, ofType type: String) -> NSBundle {

return NSBundle(forClass: PasscodeLock.self)
}

func defaultCustomColor() -> UIColor {
return UIColor(red: 0, green: 100/255, blue: 165/255, alpha: 1)
}
31 changes: 17 additions & 14 deletions PasscodeLock/PasscodeLock/ChangePasscodeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,34 @@ import Foundation

struct ChangePasscodeState: PasscodeLockStateType {

let title: String
let description: String
let title : String
let description : String
let isCancellableAction = true
var isTouchIDAllowed = false

init(stringsToShow: StringsToBeDisplayed?) {

title = (stringsToShow?.passcodeLockChangeTitle ?? localizedStringFor("PasscodeLockChangeTitle", comment: "Change passcode title"))
description = (stringsToShow?.passcodeLockChangeDescription ?? localizedStringFor("PasscodeLockChangeDescription", comment: "Change passcode description"))
var isTouchIDAllowed = false
var tintColor : UIColor?
var font : UIFont?

init(stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) {


let defaultColor = defaultCustomColor()
self.title = (stringsToShow?.passcodeLockChangeTitle ?? localizedStringFor("PasscodeLockChangeTitle", comment: "Change passcode title"))
self.description = (stringsToShow?.passcodeLockChangeDescription ?? localizedStringFor("PasscodeLockChangeDescription", comment: "Change passcode description"))
self.tintColor = (tintColor ?? defaultColor)
self.font = (font ?? UIFont.systemFontOfSize(16))
}

func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType, stringsToShow: StringsToBeDisplayed?) {
func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) {

guard let currentPasscode = lock.repository.passcode else {
return
}

if passcode == currentPasscode {

let nextState = SetPasscodeState(stringsToShow: stringsToShow)

if (passcode == currentPasscode) {
let nextState = SetPasscodeState(stringsToShow: stringsToShow, tintColor: tintColor, font: font)
lock.changeStateTo(nextState)

} else {

lock.delegate?.passcodeLockDidFail(lock)
}
}
Expand Down
30 changes: 16 additions & 14 deletions PasscodeLock/PasscodeLock/ConfirmPasscodeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,36 @@ import Foundation

struct ConfirmPasscodeState: PasscodeLockStateType {

let title: String
let description: String
let title : String
let description : String
let isCancellableAction = true
var isTouchIDAllowed = false
var isTouchIDAllowed = false
var tintColor : UIColor?
var font : UIFont?

private var passcodeToConfirm: [String]

init(passcode: [String], stringsToShow: StringsToBeDisplayed?) {

passcodeToConfirm = passcode
title = (stringsToShow?.passcodeLockConfirmTitle ?? localizedStringFor("PasscodeLockConfirmTitle", comment: "Confirm passcode title"))
description = (stringsToShow?.passcodeLockConfirmDescription ?? localizedStringFor("PasscodeLockConfirmDescription", comment: "Confirm passcode description"))
init(passcode: [String], stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) {

let defaultColor = defaultCustomColor()
self.passcodeToConfirm = passcode
self.title = (stringsToShow?.passcodeLockConfirmTitle ?? localizedStringFor("PasscodeLockConfirmTitle", comment: "Confirm passcode title"))
self.description = (stringsToShow?.passcodeLockConfirmDescription ?? localizedStringFor("PasscodeLockConfirmDescription", comment: "Confirm passcode description"))
self.tintColor = (tintColor ?? defaultColor)
self.font = (font ?? UIFont.systemFontOfSize(16))
}

func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType, stringsToShow: StringsToBeDisplayed?) {
func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) {

if passcode == passcodeToConfirm {

if (passcode == passcodeToConfirm) {
lock.repository.savePasscode(passcode)
lock.delegate?.passcodeLockDidSucceed(lock)

} else {

let mismatchTitle = (stringsToShow?.passcodeLockMismatchTitle ?? localizedStringFor("PasscodeLockMismatchTitle", comment: "Passcode mismatch title"))
let mismatchDescription = (stringsToShow?.passcodeLockMismatchDescription ?? localizedStringFor("PasscodeLockMismatchDescription", comment: "Passcode mismatch description"))

let nextState = SetPasscodeState(title: mismatchTitle, description: mismatchDescription)

let nextState = SetPasscodeState(title: mismatchTitle, description: mismatchDescription, tintColor: tintColor, font: font)
lock.changeStateTo(nextState)
lock.delegate?.passcodeLockDidFail(lock)
}
Expand Down
34 changes: 17 additions & 17 deletions PasscodeLock/PasscodeLock/EnterPasscodeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,39 @@ public let PasscodeLockIncorrectPasscodeNotification = "passcode.lock.incorrect.

struct EnterPasscodeState: PasscodeLockStateType {

let title: String
let description: String
let isCancellableAction: Bool
var isTouchIDAllowed = true
let title : String
let description : String
let isCancellableAction : Bool
var isTouchIDAllowed = true
var tintColor : UIColor?
var font : UIFont?

private var inccorectPasscodeAttempts = 0
private var isNotificationSent = false

init(allowCancellation: Bool = false, stringsToShow: StringsToBeDisplayed?) {
init(allowCancellation: Bool = false, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) {

isCancellableAction = allowCancellation
title = (stringsToShow?.passcodeLockEnterTitle ?? localizedStringFor("PasscodeLockEnterTitle", comment: "Enter passcode title"))
description = (stringsToShow?.passcodeLockEnterDescription ?? localizedStringFor("PasscodeLockEnterDescription", comment: "Enter passcode description"))
let defaultColor = defaultCustomColor()
self.isCancellableAction = allowCancellation
self.title = (stringsToShow?.passcodeLockEnterTitle ?? localizedStringFor("PasscodeLockEnterTitle", comment: "Enter passcode title"))
self.description = (stringsToShow?.passcodeLockEnterDescription ?? localizedStringFor("PasscodeLockEnterDescription", comment: "Enter passcode description"))
self.tintColor = (tintColor ?? defaultColor)
self.font = (font ?? UIFont.systemFontOfSize(16))
}

mutating func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType, stringsToShow: StringsToBeDisplayed?) {
mutating func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) {

guard let currentPasscode = lock.repository.passcode else {
return
}

if passcode == currentPasscode {

if (passcode == currentPasscode) {
lock.delegate?.passcodeLockDidSucceed(lock)

} else {

inccorectPasscodeAttempts += 1

if inccorectPasscodeAttempts >= lock.configuration.maximumInccorectPasscodeAttempts {

if (inccorectPasscodeAttempts >= lock.configuration.maximumInccorectPasscodeAttempts) {
postNotification()
}

Expand All @@ -55,9 +57,7 @@ struct EnterPasscodeState: PasscodeLockStateType {
guard !isNotificationSent else { return }

let center = NSNotificationCenter.defaultCenter()

center.postNotificationName(PasscodeLockIncorrectPasscodeNotification, object: nil)

isNotificationSent = true
self.isNotificationSent = true
}
}
10 changes: 5 additions & 5 deletions PasscodeLock/PasscodeLock/PasscodeLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public class PasscodeLock: PasscodeLockType {
self.configuration = configuration
}

public func addSign(sign: String) {
public func addSign(sign: String, stringsToBeDisplayed: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) {

passcode.append(sign)
delegate?.passcodeLock(self, addedSignAtIndex: passcode.count - 1)

if passcode.count >= configuration.passcodeLength {
lockState.acceptPasscode(passcode, fromLock: self, stringsToShow: nil)
passcode.removeAll(keepCapacity: true)
if (passcode.count >= configuration.passcodeLength) {

self.lockState.acceptPasscode(self.passcode, fromLock: self, stringsToShow: stringsToBeDisplayed, tintColor: tintColor, font: font)
self.passcode.removeAll(keepCapacity: true)
}
}

Expand Down
31 changes: 19 additions & 12 deletions PasscodeLock/PasscodeLock/SetPasscodeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,34 @@ import Foundation

struct SetPasscodeState: PasscodeLockStateType {

let title: String
let description: String
let title : String
let description : String
let isCancellableAction = true
var isTouchIDAllowed = false
var isTouchIDAllowed = false
var tintColor : UIColor?
var font : UIFont?

init(title: String, description: String) {

init(title: String, description: String, tintColor: UIColor?, font: UIFont?) {

let defaultColor = defaultCustomColor()
self.title = title
self.description = description
self.tintColor = (tintColor ?? defaultColor)
self.font = (font ?? UIFont.systemFontOfSize(16))
}

init(stringsToShow: StringsToBeDisplayed?) {

title = (stringsToShow?.passcodeLockSetTitle ?? localizedStringFor("PasscodeLockSetTitle", comment: "Set passcode title"))
description = (stringsToShow?.passcodeLockSetDescription ?? localizedStringFor("PasscodeLockSetDescription", comment: "Set passcode description"))
init(stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) {

let defaultColor = defaultCustomColor()
self.title = (stringsToShow?.passcodeLockSetTitle ?? localizedStringFor("PasscodeLockSetTitle", comment: "Set passcode title"))
self.description = (stringsToShow?.passcodeLockSetDescription ?? localizedStringFor("PasscodeLockSetDescription", comment: "Set passcode description"))
self.tintColor = (tintColor ?? defaultColor)
self.font = (font ?? UIFont.systemFontOfSize(16))
}

func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType, stringsToShow: StringsToBeDisplayed?) {
func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) {

let nextState = ConfirmPasscodeState(passcode: passcode, stringsToShow: stringsToShow)

let nextState = ConfirmPasscodeState(passcode: passcode, stringsToShow: stringsToShow, tintColor: tintColor, font: font)
lock.changeStateTo(nextState)
}
}
8 changes: 4 additions & 4 deletions PasscodeLock/PasscodeLockPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ public class PasscodeLockPresenter {
passcodeLockVC = viewController
}

public convenience init(mainWindow window: UIWindow?, configuration: PasscodeLockConfigurationType, stringsToShow: StringsToBeDisplayed?) {
public convenience init(mainWindow window: UIWindow?, configuration: PasscodeLockConfigurationType, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) {

let passcodeLockVC = PasscodeLockViewController(state: .EnterPasscode, configuration: configuration, stringsToShow: stringsToShow)
let passcodeLockVC = PasscodeLockViewController(state: .EnterPasscode, configuration: configuration, stringsToShow: stringsToShow, tintColor: tintColor, font: font)

self.init(mainWindow: window, configuration: configuration, viewController: passcodeLockVC)
}

public func presentPasscodeLock(withImage image: UIImage? = nil, andStrings stringsToShow: StringsToBeDisplayed? = nil, dismissCompletionBlock: (() -> Void)? = nil) {
public func presentPasscodeLock(withImage image: UIImage? = nil, andStrings stringsToShow: StringsToBeDisplayed? = nil, tintColor: UIColor?, font: UIFont?, dismissCompletionBlock: (() -> Void)? = nil) {

guard passcodeConfiguration.repository.hasPasscode else { return }
guard !isPasscodePresented else { return }
Expand All @@ -95,7 +95,7 @@ public class PasscodeLockPresenter {
mainWindow?.windowLevel = 1
mainWindow?.endEditing(true)

let passcodeLockVC = PasscodeLockViewController(state: .EnterPasscode, configuration: passcodeConfiguration, stringsToShow: stringsToShow)
let passcodeLockVC = PasscodeLockViewController(state: .EnterPasscode, configuration: passcodeConfiguration, stringsToShow: stringsToShow, tintColor: tintColor, font: font)
if (image != nil) {
passcodeLockVC.customImage = image
}
Expand Down
61 changes: 43 additions & 18 deletions PasscodeLock/PasscodeLockViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg
case ChangePasscode
case RemovePasscode

func getState(stringsToShow: StringsToBeDisplayed?) -> PasscodeLockStateType {
func getState(stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) -> PasscodeLockStateType {

switch self {
case .EnterPasscode: return EnterPasscodeState(stringsToShow: stringsToShow)
case .SetPasscode: return SetPasscodeState(stringsToShow: stringsToShow)
case .ChangePasscode: return ChangePasscodeState(stringsToShow: stringsToShow)
case .RemovePasscode: return EnterPasscodeState(allowCancellation: true, stringsToShow: stringsToShow)
case .EnterPasscode: return EnterPasscodeState(stringsToShow: stringsToShow, tintColor: tintColor, font: font)
case .SetPasscode: return SetPasscodeState(stringsToShow: stringsToShow, tintColor: tintColor, font: font)
case .ChangePasscode: return ChangePasscodeState(stringsToShow: stringsToShow, tintColor: tintColor, font: font)
case .RemovePasscode: return EnterPasscodeState(allowCancellation: true, stringsToShow: stringsToShow, tintColor: tintColor, font: font)
}
}
}

@IBOutlet public var passcodeButtons : [PasscodeSignButton]?
@IBOutlet public weak var titleLabel : UILabel?
@IBOutlet public weak var customImageView : UIImageView?
@IBOutlet public weak var descriptionLabel : UILabel?
Expand All @@ -48,13 +49,17 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg
internal var isPlaceholdersAnimationCompleted = true

private var shouldTryToAuthenticateWithBiometrics = true
private var customTintColor : UIColor?
private var font : UIFont?

// MARK: - Initializers

public init(state: PasscodeLockStateType, configuration: PasscodeLockConfigurationType, animateOnDismiss: Bool = true, stringToShow: StringsToBeDisplayed?) {
public init(state: PasscodeLockStateType, configuration: PasscodeLockConfigurationType, animateOnDismiss: Bool = true, stringToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) {

self.stringsToShow = stringToShow
self.animateOnDismiss = animateOnDismiss
self.font = (font ?? UIFont.systemFontOfSize(16))
self.customTintColor = (tintColor ?? UIColor(red: 0, green: 100/255, blue: 165/255, alpha: 1))
passcodeConfiguration = configuration
passcodeLock = PasscodeLock(state: state, configuration: configuration)
let nibName = "PasscodeLockView"
Expand All @@ -65,9 +70,9 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg
notificationCenter = NSNotificationCenter.defaultCenter()
}

public convenience init(state: LockState, configuration: PasscodeLockConfigurationType, animateOnDismiss: Bool = true, stringsToShow: StringsToBeDisplayed?) {
public convenience init(state: LockState, configuration: PasscodeLockConfigurationType, animateOnDismiss: Bool = true, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) {

self.init(state: state.getState(stringsToShow), configuration: configuration, animateOnDismiss: animateOnDismiss, stringToShow: stringsToShow)
self.init(state: state.getState(stringsToShow, tintColor: tintColor, font: font), configuration: configuration, animateOnDismiss: animateOnDismiss, stringToShow: stringsToShow, tintColor: tintColor, font: font)
}

public required init(coder aDecoder: NSCoder) {
Expand All @@ -82,10 +87,21 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg

public override func viewDidLoad() {
super.viewDidLoad()

updatePasscodeView()
setupEvents()

self.configurePasscodeButtons()
self.updatePasscodeView()
self.setupEvents()
}

private func configurePasscodeButtons() {

self.placeholders.forEach { (passcodePlaceHolder: PasscodeSignPlaceholderView) in
if let _customTintColor = self.customTintColor {
passcodePlaceHolder.activeColor = _customTintColor
}
passcodePlaceHolder.setupView()
}
}

public override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
Expand All @@ -97,11 +113,18 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg

internal func updatePasscodeView() {

customImageView?.image = self.customImage
titleLabel?.text = passcodeLock.state.title
descriptionLabel?.text = passcodeLock.state.description
touchIDButton?.hidden = !passcodeLock.isTouchIDAllowed
touchIDButton?.setTitle((self.stringsToShow?.useTouchID ?? localizedStringFor("UseTouchId", comment: "")), forState: .Normal)
self.customImageView?.image = self.customImage
self.titleLabel?.text = passcodeLock.state.title
self.titleLabel?.font = self.font
self.titleLabel?.textColor = self.customTintColor
self.descriptionLabel?.text = passcodeLock.state.description
self.touchIDButton?.hidden = !passcodeLock.isTouchIDAllowed
self.touchIDButton?.setTitle((self.stringsToShow?.useTouchID ?? localizedStringFor("UseTouchId", comment: "")), forState: .Normal)

self.passcodeButtons?.forEach({ (passcodeButton: PasscodeSignButton) in
passcodeButton.tintColor = self.customTintColor
})

self.cancelDeleteButtonSetup()
}

Expand Down Expand Up @@ -137,7 +160,7 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg
return
}

passcodeLock.addSign(sender.passcodeSign)
passcodeLock.addSign(sender.passcodeSign, stringsToBeDisplayed: self.stringsToShow, tintColor: customTintColor, font: font)
}

@IBAction func cancelButtonTap(sender: UIButton) {
Expand Down Expand Up @@ -222,7 +245,7 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg
guard (index < placeholders.count && index >= 0) else {
return
}

placeholders[index].animateState(state)
}

Expand Down Expand Up @@ -269,6 +292,8 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg
let cancelButton = ((self.passcodeLock.isPincodeEmpty == true) ? (self.stringsToShow?.cancel ?? localizedStringFor("Cancel", comment: "")) : (self.stringsToShow?.delete ?? localizedStringFor("Delete", comment: "")))
let titleForButton = ((self.passcodeLock.state.isCancellableAction == true) ? cancelButton : (self.stringsToShow?.delete ?? localizedStringFor("Delete", comment: "")))
self.cancelDeleteButton?.setTitle(titleForButton, forState: .Normal)
self.cancelDeleteButton?.setTitleColor(self.customTintColor, forState: .Normal)
self.cancelDeleteButton?.titleLabel?.font = self.font

if (self.passcodeLock.isPincodeEmpty == true && self.passcodeLock.state.isCancellableAction == false) {
self.cancelDeleteButton?.enabled = false
Expand Down
Loading