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

Commit e08aeec

Browse files
authored
Merge pull request #4 from moogle19/master
Specific RemovePasscodeState for better Passcode Removal
2 parents 120569e + edd8ce6 commit e08aeec

11 files changed

+194
-145
lines changed

PasscodeLock.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

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

106107
/* Begin PBXFileReference section */
108+
0A526E141DA6786200114A5E /* RemovePasscodeState.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemovePasscodeState.swift; sourceTree = "<group>"; };
107109
C99EAF3F1B90B05700D61E1B /* PasscodeLock.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = PasscodeLock.framework; sourceTree = BUILT_PRODUCTS_DIR; };
108110
C99EAF421B90B05700D61E1B /* PasscodeLock.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PasscodeLock.h; sourceTree = "<group>"; };
109111
C99EAF441B90B05700D61E1B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -285,6 +287,7 @@
285287
C9DC07FE1B90D24A007A4DD0 /* SetPasscodeState.swift */,
286288
C9DC08011B90D2BA007A4DD0 /* ConfirmPasscodeState.swift */,
287289
C9DC08041B90D394007A4DD0 /* ChangePasscodeState.swift */,
290+
0A526E141DA6786200114A5E /* RemovePasscodeState.swift */,
288291
);
289292
path = PasscodeLock;
290293
sourceTree = "<group>";
@@ -549,6 +552,7 @@
549552
C9DC07FF1B90D24A007A4DD0 /* SetPasscodeState.swift in Sources */,
550553
C9DC08051B90D394007A4DD0 /* ChangePasscodeState.swift in Sources */,
551554
C9DC08021B90D2BA007A4DD0 /* ConfirmPasscodeState.swift in Sources */,
555+
0A526E151DA6786200114A5E /* RemovePasscodeState.swift in Sources */,
552556
C9DC08121B90DE1B007A4DD0 /* PasscodeSignPlaceholderView.swift in Sources */,
553557
C9DC07F21B90C9DE007A4DD0 /* EnterPasscodeState.swift in Sources */,
554558
C9DC08141B90DE50007A4DD0 /* PasscodeSignButton.swift in Sources */,

PasscodeLock/PasscodeLock/ChangePasscodeState.swift

+9-13
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,15 @@ struct ChangePasscodeState: PasscodeLockStateType {
2323

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

26-
do {
27-
if try lock.repository.check(passcode: passcode) {
28-
29-
let nextState = SetPasscodeState()
30-
31-
lock.changeState(nextState)
32-
33-
} else {
34-
35-
lock.delegate?.passcodeLockDidFail(lock)
36-
}
37-
} catch {
38-
return
26+
if lock.repository.check(passcode: passcode) {
27+
28+
let nextState = SetPasscodeState()
29+
30+
lock.changeState(nextState)
31+
32+
} else {
33+
34+
lock.delegate?.passcodeLockDidFail(lock)
3935
}
4036
}
4137
}

PasscodeLock/PasscodeLock/EnterPasscodeState.swift

+21-17
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ struct EnterPasscodeState: PasscodeLockStateType {
1717
let isCancellableAction: Bool
1818
var isTouchIDAllowed = true
1919

20-
fileprivate var inccorectPasscodeAttempts = 0
20+
fileprivate var incorrectPasscodeAttemptsKey = "incorrectPasscodeAttemps"
21+
private var incorrectPasscodeAttempts: Int {
22+
get {
23+
return UserDefaults.standard.integer(forKey: incorrectPasscodeAttemptsKey)
24+
}
25+
set {
26+
UserDefaults.standard.set(newValue, forKey: incorrectPasscodeAttemptsKey)
27+
}
28+
}
2129
fileprivate var isNotificationSent = false
2230

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

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

32-
do {
33-
if try lock.repository.check(passcode: passcode) {
34-
35-
lock.delegate?.passcodeLockDidSucceed(lock)
36-
37-
} else {
41+
lock.delegate?.passcodeLockDidSucceed(lock)
3842

39-
inccorectPasscodeAttempts += 1
40-
41-
if inccorectPasscodeAttempts >= lock.configuration.maximumInccorectPasscodeAttempts {
42-
43-
postNotification()
44-
}
43+
incorrectPasscodeAttempts = 0
44+
45+
} else {
46+
47+
incorrectPasscodeAttempts += 1
48+
49+
if incorrectPasscodeAttempts >= lock.configuration.maximumInccorectPasscodeAttempts {
4550

46-
lock.delegate?.passcodeLockDidFail(lock)
51+
postNotification()
4752
}
48-
} catch {
49-
print(error)
50-
return
53+
54+
lock.delegate?.passcodeLockDidFail(lock)
5155
}
5256
}
5357

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// RemovePasscodeState.swift
3+
// PasscodeLock
4+
//
5+
// Created by Kevin Seidel on 06/10/16.
6+
// Copyright © 2016 Yanko Dimitrov. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
struct RemovePasscodeState: PasscodeLockStateType {
12+
let title: String
13+
let description: String
14+
let isCancellableAction = false
15+
var isTouchIDAllowed: Bool { return false }
16+
17+
private var isNotificationSent = false
18+
19+
fileprivate var incorrectPasscodeAttemptsKey = "incorrectPasscodeAttemps"
20+
private var incorrectPasscodeAttempts: Int {
21+
get {
22+
return UserDefaults.standard.integer(forKey: incorrectPasscodeAttemptsKey)
23+
}
24+
set {
25+
UserDefaults.standard.set(newValue, forKey: incorrectPasscodeAttemptsKey)
26+
}
27+
}
28+
29+
init() {
30+
31+
title = localizedStringFor("PasscodeLockEnterTitle", comment: "Enter passcode title")
32+
description = localizedStringFor("PasscodeLockEnterDescription", comment: "Enter passcode description")
33+
}
34+
35+
mutating func accept(passcode: String, from lock: PasscodeLockType) {
36+
if lock.repository.check(passcode: passcode) {
37+
38+
lock.repository.delete()
39+
40+
lock.delegate?.passcodeLockDidSucceed(lock)
41+
42+
incorrectPasscodeAttempts = 0
43+
44+
} else {
45+
46+
incorrectPasscodeAttempts += 1
47+
48+
if incorrectPasscodeAttempts >= lock.configuration.maximumInccorectPasscodeAttempts {
49+
50+
postNotification()
51+
}
52+
53+
lock.delegate?.passcodeLockDidFail(lock)
54+
}
55+
}
56+
57+
fileprivate mutating func postNotification() {
58+
59+
guard !isNotificationSent else { return }
60+
61+
NotificationCenter.default.post(name: Notification.Name(rawValue: PasscodeLockIncorrectPasscodeNotification), object: nil)
62+
63+
isNotificationSent = true
64+
}
65+
66+
}

PasscodeLock/PasscodeLockViewController.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ open class PasscodeLockViewController: UIViewController, PasscodeLockTypeDelegat
2222
case .enter: return EnterPasscodeState()
2323
case .set: return SetPasscodeState()
2424
case .change: return ChangePasscodeState()
25-
case .remove: return EnterPasscodeState(allowCancellation: true)
25+
case .remove: return RemovePasscodeState()
2626
}
2727
}
2828
}
@@ -67,6 +67,7 @@ open class PasscodeLockViewController: UIViewController, PasscodeLockTypeDelegat
6767
public convenience init(state: LockState, configuration: PasscodeLockConfigurationType, animateOnDismiss: Bool = true) {
6868

6969
self.init(state: state.getState(), configuration: configuration, animateOnDismiss: animateOnDismiss)
70+
7071
}
7172

7273
public required init(coder aDecoder: NSCoder) {

PasscodeLock/Protocols/PasscodeRepositoryType.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public protocol PasscodeRepositoryType {
1313
var hasPasscode: Bool { get }
1414

1515
func save(passcode: String)
16-
func check(passcode: String) throws -> Bool
16+
func check(passcode: String) -> Bool
1717
func delete()
1818
}

0 commit comments

Comments
 (0)