Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to Swift 3.1 #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
107 changes: 64 additions & 43 deletions PasscodeLock.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0700"
LastUpgradeVersion = "0830"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
10 changes: 5 additions & 5 deletions PasscodeLock/Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@

import Foundation

func localizedStringFor(key: String, comment: String) -> String {
func localizedStringFor(_ key: String, comment: String) -> String {

let name = "PasscodeLock"
let bundle = bundleForResource(name, ofType: "strings")

return NSLocalizedString(key, tableName: name, bundle: bundle, comment: comment)
}

func bundleForResource(name: String, ofType type: String) -> NSBundle {
func bundleForResource(_ name: String, ofType type: String) -> Bundle {

if(NSBundle.mainBundle().pathForResource(name, ofType: type) != nil) {
return NSBundle.mainBundle()
if(Bundle.main.path(forResource: name, ofType: type) != nil) {
return Bundle.main
}

return NSBundle(forClass: PasscodeLock.self)
return Bundle(for: PasscodeLock.self)
}
2 changes: 1 addition & 1 deletion PasscodeLock/PasscodeLock/ChangePasscodeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct ChangePasscodeState: PasscodeLockStateType {
description = localizedStringFor("PasscodeLockChangeDescription", comment: "Change passcode description")
}

func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType) {
func acceptPasscode(_ passcode: [String], fromLock lock: PasscodeLockType) {

guard let currentPasscode = lock.repository.passcode else {
return
Expand Down
4 changes: 2 additions & 2 deletions PasscodeLock/PasscodeLock/ConfirmPasscodeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct ConfirmPasscodeState: PasscodeLockStateType {
let isCancellableAction = true
var isTouchIDAllowed = false

private var passcodeToConfirm: [String]
fileprivate var passcodeToConfirm: [String]

init(passcode: [String]) {

Expand All @@ -24,7 +24,7 @@ struct ConfirmPasscodeState: PasscodeLockStateType {
description = localizedStringFor("PasscodeLockConfirmDescription", comment: "Confirm passcode description")
}

func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType) {
func acceptPasscode(_ passcode: [String], fromLock lock: PasscodeLockType) {

if passcode == passcodeToConfirm {

Expand Down
12 changes: 6 additions & 6 deletions PasscodeLock/PasscodeLock/EnterPasscodeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ struct EnterPasscodeState: PasscodeLockStateType {
let isCancellableAction: Bool
var isTouchIDAllowed = true

private var inccorectPasscodeAttempts = 0
private var isNotificationSent = false
fileprivate var inccorectPasscodeAttempts = 0
fileprivate var isNotificationSent = false

init(allowCancellation: Bool = false) {

Expand All @@ -27,7 +27,7 @@ struct EnterPasscodeState: PasscodeLockStateType {
description = localizedStringFor("PasscodeLockEnterDescription", comment: "Enter passcode description")
}

mutating func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType) {
mutating func acceptPasscode(_ passcode: [String], fromLock lock: PasscodeLockType) {

guard let currentPasscode = lock.repository.passcode else {
return
Expand All @@ -50,13 +50,13 @@ struct EnterPasscodeState: PasscodeLockStateType {
}
}

private mutating func postNotification() {
fileprivate mutating func postNotification() {

guard !isNotificationSent else { return }

let center = NSNotificationCenter.defaultCenter()
let center = NotificationCenter.default

center.postNotificationName(PasscodeLockIncorrectPasscodeNotification, object: nil)
center.post(name: Notification.Name(rawValue: PasscodeLockIncorrectPasscodeNotification), object: nil)

isNotificationSent = true
}
Expand Down
36 changes: 18 additions & 18 deletions PasscodeLock/PasscodeLock/PasscodeLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
import Foundation
import LocalAuthentication

public class PasscodeLock: PasscodeLockType {
open class PasscodeLock: PasscodeLockType {

public weak var delegate: PasscodeLockTypeDelegate?
public let configuration: PasscodeLockConfigurationType
open weak var delegate: PasscodeLockTypeDelegate?
open let configuration: PasscodeLockConfigurationType

public var repository: PasscodeRepositoryType {
open var repository: PasscodeRepositoryType {
return configuration.repository
}

public var state: PasscodeLockStateType {
open var state: PasscodeLockStateType {
return lockState
}

public var isTouchIDAllowed: Bool {
open var isTouchIDAllowed: Bool {
return isTouchIDEnabled() && configuration.isTouchIDAllowed && lockState.isTouchIDAllowed
}

private var lockState: PasscodeLockStateType
private lazy var passcode = [String]()
fileprivate var lockState: PasscodeLockStateType
fileprivate lazy var passcode = [String]()

public init(state: PasscodeLockStateType, configuration: PasscodeLockConfigurationType) {

Expand All @@ -37,33 +37,33 @@ public class PasscodeLock: PasscodeLockType {
self.configuration = configuration
}

public func addSign(sign: String) {
open func addSign(_ sign: String) {

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

if passcode.count >= configuration.passcodeLength {

lockState.acceptPasscode(passcode, fromLock: self)
passcode.removeAll(keepCapacity: true)
passcode.removeAll(keepingCapacity: true)
}
}

public func removeSign() {
open func removeSign() {

guard passcode.count > 0 else { return }

passcode.removeLast()
delegate?.passcodeLock(self, removedSignAtIndex: passcode.count)
}

public func changeStateTo(state: PasscodeLockStateType) {
open func changeStateTo(_ state: PasscodeLockStateType) {

lockState = state
delegate?.passcodeLockDidChangeState(self)
}

public func authenticateWithBiometrics() {
open func authenticateWithBiometrics() {

guard isTouchIDAllowed else { return }

Expand All @@ -72,16 +72,16 @@ public class PasscodeLock: PasscodeLockType {

context.localizedFallbackTitle = localizedStringFor("PasscodeLockTouchIDButton", comment: "TouchID authentication fallback button")

context.evaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reason) {
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) {
success, error in

self.handleTouchIDResult(success)
}
}

private func handleTouchIDResult(success: Bool) {
fileprivate func handleTouchIDResult(_ success: Bool) {

dispatch_async(dispatch_get_main_queue()) {
DispatchQueue.main.async {

if success {

Expand All @@ -90,10 +90,10 @@ public class PasscodeLock: PasscodeLockType {
}
}

private func isTouchIDEnabled() -> Bool {
fileprivate func isTouchIDEnabled() -> Bool {

let context = LAContext()

return context.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, error: nil)
return context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
}
}
2 changes: 1 addition & 1 deletion PasscodeLock/PasscodeLock/SetPasscodeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct SetPasscodeState: PasscodeLockStateType {
description = localizedStringFor("PasscodeLockSetDescription", comment: "Set passcode description")
}

func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType) {
func acceptPasscode(_ passcode: [String], fromLock lock: PasscodeLockType) {

let nextState = ConfirmPasscodeState(passcode: passcode)

Expand Down
30 changes: 15 additions & 15 deletions PasscodeLock/PasscodeLockPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@

import UIKit

public class PasscodeLockPresenter {
open class PasscodeLockPresenter {

private var mainWindow: UIWindow?
fileprivate var mainWindow: UIWindow?

private lazy var passcodeLockWindow: UIWindow = {
fileprivate lazy var passcodeLockWindow: UIWindow = {

let window = UIWindow(frame: UIScreen.mainScreen().bounds)
let window = UIWindow(frame: UIScreen.main.bounds)

window.windowLevel = 0
window.makeKeyAndVisible()

return window
}()

private let passcodeConfiguration: PasscodeLockConfigurationType
public var isPasscodePresented = false
public let passcodeLockVC: PasscodeLockViewController
fileprivate let passcodeConfiguration: PasscodeLockConfigurationType
open var isPasscodePresented = false
open let passcodeLockVC: PasscodeLockViewController

public init(mainWindow window: UIWindow?, configuration: PasscodeLockConfigurationType, viewController: PasscodeLockViewController) {

Expand All @@ -37,25 +37,25 @@ public class PasscodeLockPresenter {

public convenience init(mainWindow window: UIWindow?, configuration: PasscodeLockConfigurationType) {

let passcodeLockVC = PasscodeLockViewController(state: .EnterPasscode, configuration: configuration)
let passcodeLockVC = PasscodeLockViewController(state: .enterPasscode, configuration: configuration)

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

public func presentPasscodeLock() {
open func presentPasscodeLock() {

guard passcodeConfiguration.repository.hasPasscode else { return }
guard !isPasscodePresented else { return }

isPasscodePresented = true

passcodeLockWindow.windowLevel = 2
passcodeLockWindow.hidden = false
passcodeLockWindow.isHidden = false

mainWindow?.windowLevel = 1
mainWindow?.endEditing(true)

let passcodeLockVC = PasscodeLockViewController(state: .EnterPasscode, configuration: passcodeConfiguration)
let passcodeLockVC = PasscodeLockViewController(state: .enterPasscode, configuration: passcodeConfiguration)
let userDismissCompletionCallback = passcodeLockVC.dismissCompletionCallback

passcodeLockVC.dismissCompletionCallback = { [weak self] in
Expand All @@ -68,7 +68,7 @@ public class PasscodeLockPresenter {
passcodeLockWindow.rootViewController = passcodeLockVC
}

public func dismissPasscodeLock(animated animated: Bool = true) {
open func dismissPasscodeLock(animated: Bool = true) {

isPasscodePresented = false
mainWindow?.windowLevel = 1
Expand All @@ -87,12 +87,12 @@ public class PasscodeLockPresenter {

internal func animatePasscodeLockDismissal() {

UIView.animateWithDuration(
0.5,
UIView.animate(
withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: [.CurveEaseInOut],
options: UIViewAnimationOptions(),
animations: { [weak self] in

self?.passcodeLockWindow.alpha = 0
Expand Down
Loading