Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #4 from moogle19/master
Browse files Browse the repository at this point in the history
Specific RemovePasscodeState for better Passcode Removal
  • Loading branch information
moogle19 authored Oct 6, 2016
2 parents 120569e + edd8ce6 commit e08aeec
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 145 deletions.
4 changes: 4 additions & 0 deletions PasscodeLock.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0A526E151DA6786200114A5E /* RemovePasscodeState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A526E141DA6786200114A5E /* RemovePasscodeState.swift */; };
C99EAF431B90B05700D61E1B /* PasscodeLock.h in Headers */ = {isa = PBXBuildFile; fileRef = C99EAF421B90B05700D61E1B /* PasscodeLock.h */; settings = {ATTRIBUTES = (Public, ); }; };
C99EAF4A1B90B05800D61E1B /* PasscodeLock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C99EAF3F1B90B05700D61E1B /* PasscodeLock.framework */; };
C9D3DF0B1B919CE4008561EB /* PasscodeLockView.xib in Resources */ = {isa = PBXBuildFile; fileRef = C9D3DF0A1B919CE4008561EB /* PasscodeLockView.xib */; };
Expand Down Expand Up @@ -104,6 +105,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0A526E141DA6786200114A5E /* RemovePasscodeState.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemovePasscodeState.swift; sourceTree = "<group>"; };
C99EAF3F1B90B05700D61E1B /* PasscodeLock.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = PasscodeLock.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C99EAF421B90B05700D61E1B /* PasscodeLock.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PasscodeLock.h; sourceTree = "<group>"; };
C99EAF441B90B05700D61E1B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -285,6 +287,7 @@
C9DC07FE1B90D24A007A4DD0 /* SetPasscodeState.swift */,
C9DC08011B90D2BA007A4DD0 /* ConfirmPasscodeState.swift */,
C9DC08041B90D394007A4DD0 /* ChangePasscodeState.swift */,
0A526E141DA6786200114A5E /* RemovePasscodeState.swift */,
);
path = PasscodeLock;
sourceTree = "<group>";
Expand Down Expand Up @@ -549,6 +552,7 @@
C9DC07FF1B90D24A007A4DD0 /* SetPasscodeState.swift in Sources */,
C9DC08051B90D394007A4DD0 /* ChangePasscodeState.swift in Sources */,
C9DC08021B90D2BA007A4DD0 /* ConfirmPasscodeState.swift in Sources */,
0A526E151DA6786200114A5E /* RemovePasscodeState.swift in Sources */,
C9DC08121B90DE1B007A4DD0 /* PasscodeSignPlaceholderView.swift in Sources */,
C9DC07F21B90C9DE007A4DD0 /* EnterPasscodeState.swift in Sources */,
C9DC08141B90DE50007A4DD0 /* PasscodeSignButton.swift in Sources */,
Expand Down
22 changes: 9 additions & 13 deletions PasscodeLock/PasscodeLock/ChangePasscodeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,15 @@ struct ChangePasscodeState: PasscodeLockStateType {

func accept(passcode: String, from lock: PasscodeLockType) {

do {
if try lock.repository.check(passcode: passcode) {

let nextState = SetPasscodeState()

lock.changeState(nextState)

} else {

lock.delegate?.passcodeLockDidFail(lock)
}
} catch {
return
if lock.repository.check(passcode: passcode) {

let nextState = SetPasscodeState()

lock.changeState(nextState)

} else {

lock.delegate?.passcodeLockDidFail(lock)
}
}
}
38 changes: 21 additions & 17 deletions PasscodeLock/PasscodeLock/EnterPasscodeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ struct EnterPasscodeState: PasscodeLockStateType {
let isCancellableAction: Bool
var isTouchIDAllowed = true

fileprivate var inccorectPasscodeAttempts = 0
fileprivate var incorrectPasscodeAttemptsKey = "incorrectPasscodeAttemps"
private var incorrectPasscodeAttempts: Int {
get {
return UserDefaults.standard.integer(forKey: incorrectPasscodeAttemptsKey)
}
set {
UserDefaults.standard.set(newValue, forKey: incorrectPasscodeAttemptsKey)
}
}
fileprivate var isNotificationSent = false

init(allowCancellation: Bool = false) {
Expand All @@ -28,26 +36,22 @@ struct EnterPasscodeState: PasscodeLockStateType {
}

mutating func accept(passcode: String, from lock: PasscodeLockType) {
if lock.repository.check(passcode: passcode) {

do {
if try lock.repository.check(passcode: passcode) {

lock.delegate?.passcodeLockDidSucceed(lock)

} else {
lock.delegate?.passcodeLockDidSucceed(lock)

inccorectPasscodeAttempts += 1

if inccorectPasscodeAttempts >= lock.configuration.maximumInccorectPasscodeAttempts {

postNotification()
}
incorrectPasscodeAttempts = 0

} else {

incorrectPasscodeAttempts += 1

if incorrectPasscodeAttempts >= lock.configuration.maximumInccorectPasscodeAttempts {

lock.delegate?.passcodeLockDidFail(lock)
postNotification()
}
} catch {
print(error)
return

lock.delegate?.passcodeLockDidFail(lock)
}
}

Expand Down
66 changes: 66 additions & 0 deletions PasscodeLock/PasscodeLock/RemovePasscodeState.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// RemovePasscodeState.swift
// PasscodeLock
//
// Created by Kevin Seidel on 06/10/16.
// Copyright © 2016 Yanko Dimitrov. All rights reserved.
//

import Foundation

struct RemovePasscodeState: PasscodeLockStateType {
let title: String
let description: String
let isCancellableAction = false
var isTouchIDAllowed: Bool { return false }

private var isNotificationSent = false

fileprivate var incorrectPasscodeAttemptsKey = "incorrectPasscodeAttemps"
private var incorrectPasscodeAttempts: Int {
get {
return UserDefaults.standard.integer(forKey: incorrectPasscodeAttemptsKey)
}
set {
UserDefaults.standard.set(newValue, forKey: incorrectPasscodeAttemptsKey)
}
}

init() {

title = localizedStringFor("PasscodeLockEnterTitle", comment: "Enter passcode title")
description = localizedStringFor("PasscodeLockEnterDescription", comment: "Enter passcode description")
}

mutating func accept(passcode: String, from lock: PasscodeLockType) {
if lock.repository.check(passcode: passcode) {

lock.repository.delete()

lock.delegate?.passcodeLockDidSucceed(lock)

incorrectPasscodeAttempts = 0

} else {

incorrectPasscodeAttempts += 1

if incorrectPasscodeAttempts >= lock.configuration.maximumInccorectPasscodeAttempts {

postNotification()
}

lock.delegate?.passcodeLockDidFail(lock)
}
}

fileprivate mutating func postNotification() {

guard !isNotificationSent else { return }

NotificationCenter.default.post(name: Notification.Name(rawValue: PasscodeLockIncorrectPasscodeNotification), object: nil)

isNotificationSent = true
}

}
3 changes: 2 additions & 1 deletion PasscodeLock/PasscodeLockViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ open class PasscodeLockViewController: UIViewController, PasscodeLockTypeDelegat
case .enter: return EnterPasscodeState()
case .set: return SetPasscodeState()
case .change: return ChangePasscodeState()
case .remove: return EnterPasscodeState(allowCancellation: true)
case .remove: return RemovePasscodeState()
}
}
}
Expand Down Expand Up @@ -67,6 +67,7 @@ open class PasscodeLockViewController: UIViewController, PasscodeLockTypeDelegat
public convenience init(state: LockState, configuration: PasscodeLockConfigurationType, animateOnDismiss: Bool = true) {

self.init(state: state.getState(), configuration: configuration, animateOnDismiss: animateOnDismiss)

}

public required init(coder aDecoder: NSCoder) {
Expand Down
2 changes: 1 addition & 1 deletion PasscodeLock/Protocols/PasscodeRepositoryType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public protocol PasscodeRepositoryType {
var hasPasscode: Bool { get }

func save(passcode: String)
func check(passcode: String) throws -> Bool
func check(passcode: String) -> Bool
func delete()
}
Loading

0 comments on commit e08aeec

Please sign in to comment.