diff --git a/PasscodeLock.xcodeproj/project.pbxproj b/PasscodeLock.xcodeproj/project.pbxproj index 0cf64878..69545778 100644 --- a/PasscodeLock.xcodeproj/project.pbxproj +++ b/PasscodeLock.xcodeproj/project.pbxproj @@ -456,23 +456,26 @@ TargetAttributes = { C99EAF3E1B90B05700D61E1B = { CreatedOnToolsVersion = 7.0; - LastSwiftMigration = 0800; + LastSwiftMigration = 0820; }; C99EAF481B90B05700D61E1B = { CreatedOnToolsVersion = 7.0; + LastSwiftMigration = 0820; }; C9D3DF0F1B91AD11008561EB = { CreatedOnToolsVersion = 7.0; DevelopmentTeam = JZ2H644EU7; - LastSwiftMigration = 0800; + LastSwiftMigration = 0820; ProvisioningStyle = Manual; }; C9D3DF221B91AD11008561EB = { CreatedOnToolsVersion = 7.0; + LastSwiftMigration = 0820; TestTargetID = C9D3DF0F1B91AD11008561EB; }; C9D3DF2D1B91AD12008561EB = { CreatedOnToolsVersion = 7.0; + LastSwiftMigration = 0820; TestTargetID = C9D3DF0F1B91AD11008561EB; }; }; @@ -786,7 +789,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -805,7 +808,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.yankodimitrov.PasscodeLock; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -819,6 +822,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "PasscodeLockTests/PasscodeLockTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -831,6 +835,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.yankodimitrov.PasscodeLockTests; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "PasscodeLockTests/PasscodeLockTests-Bridging-Header.h"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -848,7 +853,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE = "c4ef3957-2c3b-4abf-b8dd-d5d2c5712664"; PROVISIONING_PROFILE_SPECIFIER = "XC Ad Hoc: *"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -864,7 +869,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.yankodimitrov.PasscodeLockDemo; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; @@ -878,6 +883,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.yankodimitrov.PasscodeLockDemoTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/PasscodeLockDemo.app/PasscodeLockDemo"; }; name = Debug; @@ -891,6 +897,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.yankodimitrov.PasscodeLockDemoTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/PasscodeLockDemo.app/PasscodeLockDemo"; }; name = Release; @@ -903,6 +910,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.yankodimitrov.PasscodeLockDemoUITests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_TARGET_NAME = PasscodeLockDemo; USES_XCTRUNNER = YES; }; @@ -916,6 +924,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.yankodimitrov.PasscodeLockDemoUITests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_TARGET_NAME = PasscodeLockDemo; USES_XCTRUNNER = YES; }; diff --git a/PasscodeLock/Functions.swift b/PasscodeLock/Functions.swift index 6b61c4bd..2e19da00 100644 --- a/PasscodeLock/Functions.swift +++ b/PasscodeLock/Functions.swift @@ -6,9 +6,9 @@ // Copyright © 2015 Yanko Dimitrov. All rights reserved. // -import Foundation +import UIKit -func localizedStringFor(key: String, comment: String) -> String { +func localizedStringFor(_ key: String, comment: String) -> String { let name = "Localize" let bundle = bundleForResource(name, ofType: "strings") @@ -16,13 +16,13 @@ func localizedStringFor(key: String, comment: String) -> String { 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) } func defaultCustomColor() -> UIColor { diff --git a/PasscodeLock/PasscodeLock/ChangePasscodeState.swift b/PasscodeLock/PasscodeLock/ChangePasscodeState.swift index 066d990e..4ccf5980 100644 --- a/PasscodeLock/PasscodeLock/ChangePasscodeState.swift +++ b/PasscodeLock/PasscodeLock/ChangePasscodeState.swift @@ -6,7 +6,7 @@ // Copyright © 2015 Yanko Dimitrov. All rights reserved. // -import Foundation +import UIKit struct ChangePasscodeState: PasscodeLockStateType { @@ -24,16 +24,12 @@ struct ChangePasscodeState: PasscodeLockStateType { 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)) + self.font = (font ?? UIFont.systemFont(ofSize: 16)) } - func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) { + 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 lock.repository.isPasscodeCorrect(passcode) { let nextState = SetPasscodeState(stringsToShow: stringsToShow, tintColor: tintColor, font: font) lock.changeStateTo(nextState) diff --git a/PasscodeLock/PasscodeLock/ConfirmPasscodeState.swift b/PasscodeLock/PasscodeLock/ConfirmPasscodeState.swift index e676f4a2..15b05398 100644 --- a/PasscodeLock/PasscodeLock/ConfirmPasscodeState.swift +++ b/PasscodeLock/PasscodeLock/ConfirmPasscodeState.swift @@ -6,7 +6,7 @@ // Copyright © 2015 Yanko Dimitrov. All rights reserved. // -import Foundation +import UIKit struct ConfirmPasscodeState: PasscodeLockStateType { @@ -17,7 +17,7 @@ struct ConfirmPasscodeState: PasscodeLockStateType { var tintColor : UIColor? var font : UIFont? - private var passcodeToConfirm: [String] + fileprivate var passcodeToConfirm: [String] init(passcode: [String], stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) { @@ -26,10 +26,10 @@ struct ConfirmPasscodeState: PasscodeLockStateType { 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)) + self.font = (font ?? UIFont.systemFont(ofSize: 16)) } - func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) { + func acceptPasscode(_ passcode: [String], fromLock lock: PasscodeLockType, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) { if (passcode == passcodeToConfirm) { lock.repository.savePasscode(passcode) diff --git a/PasscodeLock/PasscodeLock/EnterPasscodeState.swift b/PasscodeLock/PasscodeLock/EnterPasscodeState.swift index 86a75a87..20ff22f6 100644 --- a/PasscodeLock/PasscodeLock/EnterPasscodeState.swift +++ b/PasscodeLock/PasscodeLock/EnterPasscodeState.swift @@ -6,7 +6,7 @@ // Copyright © 2015 Yanko Dimitrov. All rights reserved. // -import Foundation +import UIKit public let PasscodeLockIncorrectPasscodeNotification = "passcode.lock.incorrect.passcode.notification" @@ -19,8 +19,8 @@ struct EnterPasscodeState: PasscodeLockStateType { var tintColor : UIColor? var font : UIFont? - private var inccorectPasscodeAttempts = 0 - private var isNotificationSent = false + fileprivate var inccorectPasscodeAttempts = 0 + fileprivate var isNotificationSent = false init(allowCancellation: Bool = false, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) { @@ -29,16 +29,12 @@ struct EnterPasscodeState: PasscodeLockStateType { 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)) + self.font = (font ?? UIFont.systemFont(ofSize: 16)) } - mutating func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) { + 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 lock.repository.isPasscodeCorrect(passcode) { lock.delegate?.passcodeLockDidSucceed(lock) } else { @@ -52,12 +48,12 @@ struct EnterPasscodeState: PasscodeLockStateType { } } - private mutating func postNotification() { + fileprivate mutating func postNotification() { guard !isNotificationSent else { return } - let center = NSNotificationCenter.defaultCenter() - center.postNotificationName(PasscodeLockIncorrectPasscodeNotification, object: nil) + let center = NotificationCenter.default + center.post(name: Notification.Name(rawValue: PasscodeLockIncorrectPasscodeNotification), object: nil) self.isNotificationSent = true } } diff --git a/PasscodeLock/PasscodeLock/PasscodeLock.swift b/PasscodeLock/PasscodeLock/PasscodeLock.swift index 6f9aecd9..9c324162 100644 --- a/PasscodeLock/PasscodeLock/PasscodeLock.swift +++ b/PasscodeLock/PasscodeLock/PasscodeLock.swift @@ -6,32 +6,32 @@ // Copyright © 2015 Yanko Dimitrov. All rights reserved. // -import Foundation +import UIKit import LocalAuthentication -public class PasscodeLock: PasscodeLockType { +open class PasscodeLock: PasscodeLockType { - public weak var delegate: PasscodeLockTypeDelegate? + open weak var delegate: PasscodeLockTypeDelegate? public 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 } - public var isPincodeEmpty: Bool { + open var isPincodeEmpty: Bool { return passcode.isEmpty } - private var lockState: PasscodeLockStateType - private lazy var passcode = [String]() + fileprivate var lockState: PasscodeLockStateType + fileprivate lazy var passcode = [String]() public init(state: PasscodeLockStateType, configuration: PasscodeLockConfigurationType) { @@ -41,7 +41,7 @@ public class PasscodeLock: PasscodeLockType { self.configuration = configuration } - public func addSign(sign: String, stringsToBeDisplayed: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) { + open func addSign(_ sign: String, stringsToBeDisplayed: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) { passcode.append(sign) delegate?.passcodeLock(self, addedSignAtIndex: passcode.count - 1) @@ -49,11 +49,11 @@ public class PasscodeLock: PasscodeLockType { if (passcode.count >= configuration.passcodeLength) { self.lockState.acceptPasscode(self.passcode, fromLock: self, stringsToShow: stringsToBeDisplayed, tintColor: tintColor, font: font) - self.passcode.removeAll(keepCapacity: true) + self.passcode.removeAll(keepingCapacity: true) } } - public func removeSign() { + open func removeSign() { guard passcode.count > 0 else { return } @@ -61,13 +61,14 @@ public class PasscodeLock: PasscodeLockType { delegate?.passcodeLock(self, removedSignAtIndex: passcode.count) } - public func changeStateTo(state: PasscodeLockStateType) { - - lockState = state - delegate?.passcodeLockDidChangeState(self) + open func changeStateTo(_ state: PasscodeLockStateType) { + DispatchQueue.main.async { + self.lockState = state + self.delegate?.passcodeLockDidChangeState(self) + } } - public func authenticateWithBiometrics(stringsToShow: StringsToBeDisplayed?) { + open func authenticateWithBiometrics(_ stringsToShow: StringsToBeDisplayed?) { guard isTouchIDAllowed else { return } @@ -76,16 +77,16 @@ public class PasscodeLock: PasscodeLockType { context.localizedFallbackTitle = (stringsToShow?.passcodeLockTouchIDButton ?? 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 { @@ -94,10 +95,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) } } diff --git a/PasscodeLock/PasscodeLock/SetPasscodeState.swift b/PasscodeLock/PasscodeLock/SetPasscodeState.swift index f4853ab4..d0ed4e8f 100644 --- a/PasscodeLock/PasscodeLock/SetPasscodeState.swift +++ b/PasscodeLock/PasscodeLock/SetPasscodeState.swift @@ -6,7 +6,7 @@ // Copyright © 2015 Yanko Dimitrov. All rights reserved. // -import Foundation +import UIKit struct SetPasscodeState: PasscodeLockStateType { @@ -23,7 +23,7 @@ struct SetPasscodeState: PasscodeLockStateType { self.title = title self.description = description self.tintColor = (tintColor ?? defaultColor) - self.font = (font ?? UIFont.systemFontOfSize(16)) + self.font = (font ?? UIFont.systemFont(ofSize: 16)) } init(stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) { @@ -32,10 +32,10 @@ struct SetPasscodeState: PasscodeLockStateType { 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)) + self.font = (font ?? UIFont.systemFont(ofSize: 16)) } - func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) { + func acceptPasscode(_ passcode: [String], fromLock lock: PasscodeLockType, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) { let nextState = ConfirmPasscodeState(passcode: passcode, stringsToShow: stringsToShow, tintColor: tintColor, font: font) lock.changeStateTo(nextState) diff --git a/PasscodeLock/PasscodeLockPresenter.swift b/PasscodeLock/PasscodeLockPresenter.swift index 1967290f..9bc24a5d 100644 --- a/PasscodeLock/PasscodeLockPresenter.swift +++ b/PasscodeLock/PasscodeLockPresenter.swift @@ -48,54 +48,54 @@ public struct StringsToBeDisplayed { } } -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.windowLevel = UIWindow.Level(rawValue: 0) window.makeKeyAndVisible() return window }() - private let passcodeConfiguration: PasscodeLockConfigurationType - public var isPasscodePresented = false + fileprivate let passcodeConfiguration: PasscodeLockConfigurationType + open var isPasscodePresented = false public let passcodeLockVC: PasscodeLockViewController - public var stringsToBeDisplayed: StringsToBeDisplayed? + open var stringsToBeDisplayed: StringsToBeDisplayed? public init(mainWindow window: UIWindow?, configuration: PasscodeLockConfigurationType, viewController: PasscodeLockViewController) { mainWindow = window - mainWindow?.windowLevel = 1 + mainWindow?.windowLevel = UIWindow.Level(rawValue: 1) passcodeConfiguration = configuration passcodeLockVC = viewController } public convenience init(mainWindow window: UIWindow?, configuration: PasscodeLockConfigurationType, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) { - let passcodeLockVC = PasscodeLockViewController(state: .EnterPasscode, configuration: configuration, stringsToShow: stringsToShow, tintColor: tintColor, font: font) + 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, tintColor: UIColor?, font: UIFont?, dismissCompletionBlock: (() -> Void)? = nil) { + open 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 } isPasscodePresented = true - passcodeLockWindow.windowLevel = 2 - passcodeLockWindow.hidden = false + passcodeLockWindow.windowLevel = UIWindow.Level(rawValue: 2) + passcodeLockWindow.isHidden = false - mainWindow?.windowLevel = 1 + mainWindow?.windowLevel = UIWindow.Level(rawValue: 1) mainWindow?.endEditing(true) - let passcodeLockVC = PasscodeLockViewController(state: .EnterPasscode, configuration: passcodeConfiguration, stringsToShow: stringsToShow, tintColor: tintColor, font: font) + let passcodeLockVC = PasscodeLockViewController(state: .enterPasscode, configuration: passcodeConfiguration, stringsToShow: stringsToShow, tintColor: tintColor, font: font) if (image != nil) { passcodeLockVC.customImage = image } @@ -111,10 +111,10 @@ public class PasscodeLockPresenter { passcodeLockWindow.rootViewController = passcodeLockVC } - public func dismissPasscodeLock(animated animated: Bool = true) { + open func dismissPasscodeLock(animated: Bool = true) { isPasscodePresented = false - mainWindow?.windowLevel = 1 + mainWindow?.windowLevel = UIWindow.Level(rawValue: 1) mainWindow?.makeKeyAndVisible() if animated { @@ -123,26 +123,26 @@ public class PasscodeLockPresenter { } else { - passcodeLockWindow.windowLevel = 0 + passcodeLockWindow.windowLevel = UIWindow.Level(rawValue: 0) passcodeLockWindow.rootViewController = nil } } internal func animatePasscodeLockDismissal() { - UIView.animateWithDuration( - 0.5, + UIView.animate( + withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, - options: [.CurveEaseInOut], + options: UIView.AnimationOptions(), animations: { [weak self] in self?.passcodeLockWindow.alpha = 0 }, completion: { [weak self] _ in - self?.passcodeLockWindow.windowLevel = 0 + self?.passcodeLockWindow.windowLevel = UIWindow.Level(rawValue: 0) self?.passcodeLockWindow.rootViewController = nil self?.passcodeLockWindow.alpha = 1 } diff --git a/PasscodeLock/PasscodeLockViewController.swift b/PasscodeLock/PasscodeLockViewController.swift index 25e826df..4dfd3b13 100644 --- a/PasscodeLock/PasscodeLockViewController.swift +++ b/PasscodeLock/PasscodeLockViewController.swift @@ -8,49 +8,49 @@ import UIKit -public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDelegate { +open class PasscodeLockViewController: UIViewController, PasscodeLockTypeDelegate { public enum LockState { - case EnterPasscode - case SetPasscode - case ChangePasscode - case RemovePasscode + case enterPasscode + case setPasscode + case changePasscode + case removePasscode - func getState(stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) -> PasscodeLockStateType { + func getState(_ stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) -> PasscodeLockStateType { switch self { - 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) + 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? - @IBOutlet public var placeholders : [PasscodeSignPlaceholderView] = [PasscodeSignPlaceholderView]() - @IBOutlet public weak var cancelDeleteButton : UIButton? - @IBOutlet public weak var touchIDButton : UIButton? - @IBOutlet public weak var placeholdersX : NSLayoutConstraint? - - public var successCallback : ((lock: PasscodeLockType) -> Void)? - public var customImage : UIImage? - public var dismissCompletionCallback : (()->Void)? - public var animateOnDismiss : Bool - public var notificationCenter : NSNotificationCenter? - public var stringsToShow : StringsToBeDisplayed? - public var closingView : Bool = false + @IBOutlet open var passcodeButtons : [PasscodeSignButton]? + @IBOutlet open weak var titleLabel : UILabel? + @IBOutlet open weak var customImageView : UIImageView? + @IBOutlet open weak var descriptionLabel : UILabel? + @IBOutlet open var placeholders : [PasscodeSignPlaceholderView] = [PasscodeSignPlaceholderView]() + @IBOutlet open weak var cancelDeleteButton : UIButton? + @IBOutlet open weak var touchIDButton : UIButton? + @IBOutlet open weak var placeholdersX : NSLayoutConstraint? + + open var successCallback : ((_ lock: PasscodeLockType) -> Void)? + open var customImage : UIImage? + open var dismissCompletionCallback : (()->Void)? + open var animateOnDismiss : Bool + open var notificationCenter : NotificationCenter? + open var stringsToShow : StringsToBeDisplayed? + open var closingView : Bool = false internal let passcodeConfiguration : PasscodeLockConfigurationType - internal let passcodeLock : PasscodeLockType + internal var passcodeLock : PasscodeLockType internal var isPlaceholdersAnimationCompleted = true - private var shouldTryToAuthenticateWithBiometrics = true - private var customTintColor : UIColor? - private var font : UIFont? + fileprivate var shouldTryToAuthenticateWithBiometrics = true + fileprivate var customTintColor : UIColor? + fileprivate var font : UIFont? // MARK: - Initializers @@ -58,16 +58,16 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg self.stringsToShow = stringToShow self.animateOnDismiss = animateOnDismiss - self.font = (font ?? UIFont.systemFontOfSize(16)) + self.font = (font ?? UIFont.systemFont(ofSize: 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" - let bundle: NSBundle = bundleForResource(nibName, ofType: "nib") + let bundle: Bundle = bundleForResource(nibName, ofType: "nib") super.init(nibName: nibName, bundle: bundle) passcodeLock.delegate = self - notificationCenter = NSNotificationCenter.defaultCenter() + notificationCenter = NotificationCenter.default } public convenience init(state: LockState, configuration: PasscodeLockConfigurationType, animateOnDismiss: Bool = true, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) { @@ -85,7 +85,7 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg // MARK: - View - public override func viewDidLoad() { + open override func viewDidLoad() { super.viewDidLoad() self.configurePasscodeButtons() @@ -93,7 +93,7 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg self.setupEvents() } - private func configurePasscodeButtons() { + fileprivate func configurePasscodeButtons() { self.placeholders.forEach { (passcodePlaceHolder: PasscodeSignPlaceholderView) in if let _customTintColor = self.customTintColor { @@ -103,7 +103,7 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg } } - public override func viewDidAppear(animated: Bool) { + open override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) if shouldTryToAuthenticateWithBiometrics { @@ -118,9 +118,9 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg 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.touchIDButton?.setTitleColor(self.customTintColor, forState: .Normal) + self.touchIDButton?.isHidden = !passcodeLock.isTouchIDAllowed + self.touchIDButton?.setTitle((self.stringsToShow?.useTouchID ?? localizedStringFor("UseTouchId", comment: "")), for: .normal) + self.touchIDButton?.setTitleColor(self.customTintColor, for: .normal) self.passcodeButtons?.forEach({ (passcodeButton: PasscodeSignButton) in passcodeButton.tintColor = self.customTintColor }) @@ -130,31 +130,31 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg // MARK: - Events - private func setupEvents() { + fileprivate func setupEvents() { - notificationCenter?.addObserver(self, selector: #selector(self.appWillEnterForegroundHandler(_:)), name: UIApplicationWillEnterForegroundNotification, object: nil) - notificationCenter?.addObserver(self, selector: #selector(PasscodeLockViewController.appDidEnterBackgroundHandler(_:)), name: UIApplicationDidEnterBackgroundNotification, object: nil) + notificationCenter?.addObserver(self, selector: #selector(self.appWillEnterForegroundHandler(_:)), name: UIApplication.willEnterForegroundNotification, object: nil) + notificationCenter?.addObserver(self, selector: #selector(PasscodeLockViewController.appDidEnterBackgroundHandler(_:)), name: UIApplication.didEnterBackgroundNotification, object: nil) } - private func clearEvents() { + fileprivate func clearEvents() { - notificationCenter?.removeObserver(self, name: UIApplicationWillEnterForegroundNotification, object: nil) - notificationCenter?.removeObserver(self, name: UIApplicationDidEnterBackgroundNotification, object: nil) + notificationCenter?.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil) + notificationCenter?.removeObserver(self, name: UIApplication.didEnterBackgroundNotification, object: nil) } - public func appWillEnterForegroundHandler(notification: NSNotification) { + @objc open func appWillEnterForegroundHandler(_ notification: Notification) { authenticateWithBiometrics() } - public func appDidEnterBackgroundHandler(notification: NSNotification) { + @objc open func appDidEnterBackgroundHandler(_ notification: Notification) { shouldTryToAuthenticateWithBiometrics = false } // MARK: - Actions - @IBAction func passcodeSignButtonTap(sender: PasscodeSignButton) { + @IBAction func passcodeSignButtonTap(_ sender: PasscodeSignButton) { guard isPlaceholdersAnimationCompleted else { return @@ -163,12 +163,12 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg passcodeLock.addSign(sender.passcodeSign, stringsToBeDisplayed: self.stringsToShow, tintColor: customTintColor, font: font) } - @IBAction func cancelButtonTap(sender: UIButton) { + @IBAction func cancelButtonTap(_ sender: UIButton) { dismissPasscodeLock(passcodeLock) } - @IBAction func deleteSignButtonTap(sender: UIButton) { + @IBAction func deleteSignButtonTap(_ sender: UIButton) { if (passcodeLock.isPincodeEmpty == true && passcodeLock.state.isCancellableAction == true) { dismissPasscodeLock(passcodeLock) @@ -178,24 +178,24 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg } } - @IBAction func touchIDButtonTap(sender: UIButton) { + @IBAction func touchIDButtonTap(_ sender: UIButton) { passcodeLock.authenticateWithBiometrics(self.stringsToShow) } - private func authenticateWithBiometrics() { + fileprivate func authenticateWithBiometrics() { if (passcodeConfiguration.shouldRequestTouchIDImmediately == true && passcodeLock.isTouchIDAllowed == true) { passcodeLock.authenticateWithBiometrics(self.stringsToShow) } } - internal func dismissPasscodeLock(lock: PasscodeLockType, completionHandler: (() -> Void)? = nil) { + internal func dismissPasscodeLock(_ lock: PasscodeLockType, completionHandler: (() -> Void)? = nil) { // if presented as modal if (presentingViewController?.presentedViewController == self) { - dismissViewControllerAnimated(animateOnDismiss, completion: { [weak self] _ in + dismiss(animated: animateOnDismiss, completion: { [weak self] in self?.dismissCompletionCallback?() completionHandler?() }) @@ -204,7 +204,7 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg // if pushed in a navigation controller } else if navigationController != nil { - navigationController?.popViewControllerAnimated(animateOnDismiss) + navigationController?.popViewController(animated: animateOnDismiss) } dismissCompletionCallback?() @@ -218,29 +218,29 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg self.cancelDeleteButtonSetup() isPlaceholdersAnimationCompleted = false - animatePlaceholders(placeholders, toState: .Error) + animatePlaceholders(placeholders, toState: .error) placeholdersX?.constant = -40 view.layoutIfNeeded() - UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0, options: [], animations: { + UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0, options: [], animations: { self.placeholdersX?.constant = 0 self.view.layoutIfNeeded() }, completion: { completed in self.isPlaceholdersAnimationCompleted = true - self.animatePlaceholders(self.placeholders, toState: .Inactive) + self.animatePlaceholders(self.placeholders, toState: .inactive) }) } - internal func animatePlaceholders(placeholders: [PasscodeSignPlaceholderView], toState state: PasscodeSignPlaceholderView.State) { + internal func animatePlaceholders(_ placeholders: [PasscodeSignPlaceholderView], toState state: PasscodeSignPlaceholderView.State) { for placeholder in placeholders { placeholder.animateState(state) } } - private func animatePlacehodlerAtIndex(index: Int, toState state: PasscodeSignPlaceholderView.State) { + fileprivate func animatePlacehodlerAtIndex(_ index: Int, toState state: PasscodeSignPlaceholderView.State) { guard (index < placeholders.count && index >= 0) else { return @@ -251,55 +251,63 @@ public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDeleg // MARK: - PasscodeLockDelegate - public func passcodeLockDidSucceed(lock: PasscodeLockType) { + open func passcodeLockDidSucceed(_ lock: PasscodeLockType) { self.cancelDeleteButtonSetup() - animatePlaceholders(placeholders, toState: .Inactive) - dismissPasscodeLock(lock, completionHandler: { [weak self] _ in - self?.successCallback?(lock: lock) + animatePlaceholders(placeholders, toState: .inactive) + dismissPasscodeLock(lock, completionHandler: { [weak self] in + self?.successCallback?(lock) }) } - public func passcodeLockDidFail(lock: PasscodeLockType) { + open func passcodeLockDidFail(_ lock: PasscodeLockType) { animateWrongPassword() } - public func passcodeLockDidChangeState(lock: PasscodeLockType) { + open func passcodeLockDidChangeState(_ lock: PasscodeLockType) { updatePasscodeView() - animatePlaceholders(placeholders, toState: .Inactive) + animatePlaceholders(placeholders, toState: .inactive) self.cancelDeleteButtonSetup() } - public func passcodeLock(lock: PasscodeLockType, addedSignAtIndex index: Int) { + open func passcodeLock(_ lock: PasscodeLockType, addedSignAtIndex index: Int) { - animatePlacehodlerAtIndex(index, toState: .Active) + animatePlacehodlerAtIndex(index, toState: .active) self.cancelDeleteButtonSetup() } - public func passcodeLock(lock: PasscodeLockType, removedSignAtIndex index: Int) { + open func passcodeLock(_ lock: PasscodeLockType, removedSignAtIndex index: Int) { - animatePlacehodlerAtIndex(index, toState: .Inactive) + animatePlacehodlerAtIndex(index, toState: .inactive) if (index == 0) { self.cancelDeleteButtonSetup() } } - public func cancelDeleteButtonSetup() { + open func cancelDeleteButtonSetup() { + DispatchQueue.main.async { + // Swift 4: fix for + // Simultaneous accesses to ... but modification requires exclusive access. + self.doCancelDeleteButtonSetup() + } + } + + open func doCancelDeleteButtonSetup() { 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?.setTitleColor(self.customTintColor?.colorWithAlphaComponent(0.5), forState: .Disabled) + self.cancelDeleteButton?.setTitle(titleForButton, for: UIControl.State()) + self.cancelDeleteButton?.setTitleColor(self.customTintColor, for: UIControl.State()) + self.cancelDeleteButton?.setTitleColor(self.customTintColor?.withAlphaComponent(0.5), for: .disabled) self.cancelDeleteButton?.titleLabel?.font = self.font if (self.passcodeLock.isPincodeEmpty == true && self.passcodeLock.state.isCancellableAction == false) { - self.cancelDeleteButton?.enabled = false + self.cancelDeleteButton?.isEnabled = false } else { - self.cancelDeleteButton?.enabled = true + self.cancelDeleteButton?.isEnabled = true } } } diff --git a/PasscodeLock/Protocols/PasscodeLockStateType.swift b/PasscodeLock/Protocols/PasscodeLockStateType.swift index 59c07353..db077cd2 100644 --- a/PasscodeLock/Protocols/PasscodeLockStateType.swift +++ b/PasscodeLock/Protocols/PasscodeLockStateType.swift @@ -6,7 +6,7 @@ // Copyright © 2015 Yanko Dimitrov. All rights reserved. // -import Foundation +import UIKit public protocol PasscodeLockStateType { @@ -16,5 +16,5 @@ public protocol PasscodeLockStateType { var isTouchIDAllowed : Bool {get} var tintColor : UIColor? {get} - mutating func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) + mutating func acceptPasscode(_ passcode: [String], fromLock lock: PasscodeLockType, stringsToShow: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) } diff --git a/PasscodeLock/Protocols/PasscodeLockType.swift b/PasscodeLock/Protocols/PasscodeLockType.swift index 257d005e..f00be2e2 100644 --- a/PasscodeLock/Protocols/PasscodeLockType.swift +++ b/PasscodeLock/Protocols/PasscodeLockType.swift @@ -6,28 +6,28 @@ // Copyright © 2015 Yanko Dimitrov. All rights reserved. // -import Foundation +import UIKit public protocol PasscodeLockType { - weak var delegate: PasscodeLockTypeDelegate? {get set} + var delegate: PasscodeLockTypeDelegate? {get set} var configuration: PasscodeLockConfigurationType {get} var repository: PasscodeRepositoryType {get} var state: PasscodeLockStateType {get} var isTouchIDAllowed: Bool {get} var isPincodeEmpty: Bool {get} - func addSign(sign: String, stringsToBeDisplayed: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) + func addSign(_ sign: String, stringsToBeDisplayed: StringsToBeDisplayed?, tintColor: UIColor?, font: UIFont?) func removeSign() - func changeStateTo(state: PasscodeLockStateType) - func authenticateWithBiometrics(stringsToShow: StringsToBeDisplayed?) + func changeStateTo(_ state: PasscodeLockStateType) + func authenticateWithBiometrics(_ stringsToShow: StringsToBeDisplayed?) } public protocol PasscodeLockTypeDelegate: class { - func passcodeLockDidSucceed(lock: PasscodeLockType) - func passcodeLockDidFail(lock: PasscodeLockType) - func passcodeLockDidChangeState(lock: PasscodeLockType) - func passcodeLock(lock: PasscodeLockType, addedSignAtIndex index: Int) - func passcodeLock(lock: PasscodeLockType, removedSignAtIndex index: Int) + func passcodeLockDidSucceed(_ lock: PasscodeLockType) + func passcodeLockDidFail(_ lock: PasscodeLockType) + func passcodeLockDidChangeState(_ lock: PasscodeLockType) + func passcodeLock(_ lock: PasscodeLockType, addedSignAtIndex index: Int) + func passcodeLock(_ lock: PasscodeLockType, removedSignAtIndex index: Int) } diff --git a/PasscodeLock/Protocols/PasscodeRepositoryType.swift b/PasscodeLock/Protocols/PasscodeRepositoryType.swift index 19272379..eed2ca39 100644 --- a/PasscodeLock/Protocols/PasscodeRepositoryType.swift +++ b/PasscodeLock/Protocols/PasscodeRepositoryType.swift @@ -11,8 +11,8 @@ import Foundation public protocol PasscodeRepositoryType { var hasPasscode: Bool {get} - var passcode: [String]? {get} + func isPasscodeCorrect(_ passcode: [String]) -> Bool - func savePasscode(passcode: [String]) + func savePasscode(_ passcode: [String]) func deletePasscode() } diff --git a/PasscodeLock/Views/PasscodeSignButton.swift b/PasscodeLock/Views/PasscodeSignButton.swift index ceb08817..1e38da02 100644 --- a/PasscodeLock/Views/PasscodeSignButton.swift +++ b/PasscodeLock/Views/PasscodeSignButton.swift @@ -8,12 +8,12 @@ import UIKit -public class PasscodeSignButton: UIButton { +open class PasscodeSignButton: UIButton { - public var passcodeSign: String = "1" - public var customTintColor: UIColor? = UIColor(red: 0, green: 100/255, blue: 165/255, alpha: 1) + @IBInspectable open var passcodeSign: String! + open var customTintColor: UIColor? = UIColor(red: 0, green: 100/255, blue: 165/255, alpha: 1) - public override func awakeFromNib() { + open override func awakeFromNib() { super.awakeFromNib() self.tintColor = customTintColor diff --git a/PasscodeLock/Views/PasscodeSignPlaceholderView.swift b/PasscodeLock/Views/PasscodeSignPlaceholderView.swift index b769d230..c6c30d56 100644 --- a/PasscodeLock/Views/PasscodeSignPlaceholderView.swift +++ b/PasscodeLock/Views/PasscodeSignPlaceholderView.swift @@ -9,30 +9,30 @@ import UIKit @IBDesignable -public class PasscodeSignPlaceholderView: UIView { +open class PasscodeSignPlaceholderView: UIView { public enum State { - case Inactive - case Active - case Error + case inactive + case active + case error } @IBInspectable - public var inactiveColor: UIColor = UIColor.whiteColor() { + open var inactiveColor: UIColor = UIColor.white { didSet { self.setupView() } } @IBInspectable - public var activeColor: UIColor = UIColor.grayColor() { + open var activeColor: UIColor = UIColor.gray { didSet { self.setupView() } } @IBInspectable - public var errorColor: UIColor = UIColor.redColor() { + open var errorColor: UIColor = UIColor.red { didSet { self.setupView() } @@ -50,35 +50,35 @@ public class PasscodeSignPlaceholderView: UIView { super.init(coder: aDecoder) } - public override func intrinsicContentSize() -> CGSize { + open override var intrinsicContentSize : CGSize { - return CGSizeMake(16, 16) + return CGSize(width: 16, height: 16) } func setupView() { layer.cornerRadius = 7 layer.borderWidth = 1 - layer.borderColor = activeColor.CGColor + layer.borderColor = activeColor.cgColor backgroundColor = inactiveColor } - private func colorsForState(state: State) -> (backgroundColor: UIColor, borderColor: UIColor) { + fileprivate func colorsForState(_ state: State) -> (backgroundColor: UIColor, borderColor: UIColor) { switch state { - case .Inactive: return (inactiveColor, activeColor) - case .Active: return (activeColor, activeColor) - case .Error: return (errorColor, errorColor) + case .inactive: return (inactiveColor, activeColor) + case .active: return (activeColor, activeColor) + case .error: return (errorColor, errorColor) } } - public func animateState(state: State, completion: (() -> Void)? = nil) { + open func animateState(_ state: State, completion: (() -> Void)? = nil) { let colors = colorsForState(state) - UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: [], animations: { [weak self] in + UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: [], animations: { [weak self] in self?.backgroundColor = colors.backgroundColor - self?.layer.borderColor = colors.borderColor.CGColor + self?.layer.borderColor = colors.borderColor.cgColor }, completion: nil) } diff --git a/PasscodeLockDemo/AppDelegate.swift b/PasscodeLockDemo/AppDelegate.swift index 11814561..72af41e0 100644 --- a/PasscodeLockDemo/AppDelegate.swift +++ b/PasscodeLockDemo/AppDelegate.swift @@ -17,39 +17,39 @@ class AppDelegate: UIResponder, UIApplicationDelegate { lazy var passcodeLockPresenter: PasscodeLockPresenter = { let configuration = PasscodeLockConfiguration() - let presenter = PasscodeLockPresenter(mainWindow: self.window, configuration: configuration, stringsToShow: nil, tintColor: UIColor.blueColor(), font: UIFont.italicSystemFontOfSize(16)) + let presenter = PasscodeLockPresenter(mainWindow: self.window, configuration: configuration, stringsToShow: nil, tintColor: UIColor.blue, font: UIFont.italicSystemFont(ofSize: 16)) return presenter }() - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { - passcodeLockPresenter.presentPasscodeLock(tintColor: UIColor.blueColor(), font: UIFont.italicSystemFontOfSize(16)) + passcodeLockPresenter.presentPasscodeLock(tintColor: UIColor.blue, font: UIFont.italicSystemFont(ofSize: 16)) return true } - func applicationWillResignActive(application: UIApplication) { + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - func applicationDidEnterBackground(application: UIApplication) { + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. - passcodeLockPresenter.presentPasscodeLock(tintColor: UIColor.blueColor(), font: UIFont.italicSystemFontOfSize(16)) + passcodeLockPresenter.presentPasscodeLock(tintColor: UIColor.blue, font: UIFont.italicSystemFont(ofSize: 16)) } - func applicationWillEnterForeground(application: UIApplication) { + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - func applicationDidBecomeActive(application: UIApplication) { + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - func applicationWillTerminate(application: UIApplication) { + func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } } diff --git a/PasscodeLockDemo/PasscodeSettingsViewController.swift b/PasscodeLockDemo/PasscodeSettingsViewController.swift index 5bc806b1..c8580ce6 100644 --- a/PasscodeLockDemo/PasscodeSettingsViewController.swift +++ b/PasscodeLockDemo/PasscodeSettingsViewController.swift @@ -16,7 +16,7 @@ class PasscodeSettingsViewController: UIViewController { @IBOutlet weak var testTextField: UITextField! @IBOutlet weak var testActivityButton: UIButton! - private let configuration: PasscodeLockConfigurationType + fileprivate let configuration: PasscodeLockConfigurationType init(configuration: PasscodeLockConfigurationType) { @@ -35,7 +35,7 @@ class PasscodeSettingsViewController: UIViewController { // MARK: - View - override func viewWillAppear(animated: Bool) { + override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) updatePasscodeView() @@ -45,23 +45,23 @@ class PasscodeSettingsViewController: UIViewController { let hasPasscode = configuration.repository.hasPasscode - changePasscodeButton.hidden = !hasPasscode - passcodeSwitch.on = hasPasscode + changePasscodeButton.isHidden = !hasPasscode + passcodeSwitch.isOn = hasPasscode } // MARK: - Actions - @IBAction func passcodeSwitchValueChange(sender: UISwitch) { + @IBAction func passcodeSwitchValueChange(_ sender: UISwitch) { let passcodeVC: PasscodeLockViewController - if passcodeSwitch.on { + if passcodeSwitch.isOn { - passcodeVC = PasscodeLockViewController(state: .SetPasscode, configuration: configuration, stringsToShow: nil, tintColor: UIColor.blueColor(), font: UIFont.italicSystemFontOfSize(16)) + passcodeVC = PasscodeLockViewController(state: .setPasscode, configuration: configuration, stringsToShow: nil, tintColor: UIColor.blue, font: UIFont.italicSystemFont(ofSize: 16)) } else { - passcodeVC = PasscodeLockViewController(state: .RemovePasscode, configuration: configuration, stringsToShow: nil, tintColor: UIColor.blueColor(), font: UIFont.italicSystemFontOfSize(16)) + passcodeVC = PasscodeLockViewController(state: .removePasscode, configuration: configuration, stringsToShow: nil, tintColor: UIColor.blue, font: UIFont.italicSystemFont(ofSize: 16)) passcodeVC.successCallback = { lock in @@ -69,37 +69,37 @@ class PasscodeSettingsViewController: UIViewController { } } - presentViewController(passcodeVC, animated: true, completion: nil) + present(passcodeVC, animated: true, completion: nil) } - @IBAction func changePasscodeButtonTap(sender: UIButton) { + @IBAction func changePasscodeButtonTap(_ sender: UIButton) { let repo = UserDefaultsPasscodeRepository() let config = PasscodeLockConfiguration(repository: repo) - let passcodeLock = PasscodeLockViewController(state: .ChangePasscode, configuration: config, stringsToShow: nil, tintColor: UIColor.blueColor(), font: UIFont.italicSystemFontOfSize(16)) + let passcodeLock = PasscodeLockViewController(state: .changePasscode, configuration: config, stringsToShow: nil, tintColor: UIColor.blue, font: UIFont.italicSystemFont(ofSize: 16)) - presentViewController(passcodeLock, animated: true, completion: nil) + present(passcodeLock, animated: true, completion: nil) } - @IBAction func testAlertButtonTap(sender: UIButton) { + @IBAction func testAlertButtonTap(_ sender: UIButton) { - let alertVC = UIAlertController(title: "Test", message: "", preferredStyle: .Alert) + let alertVC = UIAlertController(title: "Test", message: "", preferredStyle: .alert) - alertVC.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil)) + alertVC.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil)) - presentViewController(alertVC, animated: true, completion: nil) + present(alertVC, animated: true, completion: nil) } - @IBAction func testActivityButtonTap(sender: UIButton) { + @IBAction func testActivityButtonTap(_ sender: UIButton) { let activityVC = UIActivityViewController(activityItems: ["Test"], applicationActivities: nil) activityVC.popoverPresentationController?.sourceView = testActivityButton - activityVC.popoverPresentationController?.sourceRect = CGRectMake(10, 20, 0, 0) + activityVC.popoverPresentationController?.sourceRect = CGRect(x: 10, y: 20, width: 0, height: 0) - presentViewController(activityVC, animated: true, completion: nil) + present(activityVC, animated: true, completion: nil) } @IBAction func dismissKeyboard() { diff --git a/PasscodeLockDemo/UserDefaultsPasscodeRepository.swift b/PasscodeLockDemo/UserDefaultsPasscodeRepository.swift index 696171a3..a8e6b6dc 100644 --- a/PasscodeLockDemo/UserDefaultsPasscodeRepository.swift +++ b/PasscodeLockDemo/UserDefaultsPasscodeRepository.swift @@ -11,11 +11,11 @@ import PasscodeLock class UserDefaultsPasscodeRepository: PasscodeRepositoryType { - private let passcodeKey = "passcode.lock.passcode" + fileprivate let passcodeKey = "passcode.lock.passcode" - private lazy var defaults: NSUserDefaults = { + fileprivate lazy var defaults: UserDefaults = { - return NSUserDefaults.standardUserDefaults() + return UserDefaults.standard }() var hasPasscode: Bool { @@ -29,18 +29,18 @@ class UserDefaultsPasscodeRepository: PasscodeRepositoryType { var passcode: [String]? { - return defaults.valueForKey(passcodeKey) as? [String] ?? nil + return defaults.value(forKey: passcodeKey) as? [String] ?? nil } - func savePasscode(passcode: [String]) { + func savePasscode(_ passcode: [String]) { - defaults.setObject(passcode, forKey: passcodeKey) + defaults.set(passcode, forKey: passcodeKey) defaults.synchronize() } func deletePasscode() { - defaults.removeObjectForKey(passcodeKey) + defaults.removeObject(forKey: passcodeKey) defaults.synchronize() } } diff --git a/PasscodeLockTests/Fakes/FakePasscodeLock.swift b/PasscodeLockTests/Fakes/FakePasscodeLock.swift index 518710e3..762f4dc2 100644 --- a/PasscodeLockTests/Fakes/FakePasscodeLock.swift +++ b/PasscodeLockTests/Fakes/FakePasscodeLock.swift @@ -6,7 +6,7 @@ // Copyright © 2015 Yanko Dimitrov. All rights reserved. // -import Foundation +import UIKit class FakePasscodeLock: PasscodeLockType { @@ -25,7 +25,7 @@ class FakePasscodeLock: PasscodeLockType { self.configuration = configuration } - func addSign(sign: String) { + func addSign(_ sign: String) { } @@ -33,7 +33,7 @@ class FakePasscodeLock: PasscodeLockType { } - func changeStateTo(state: PasscodeLockStateType) { + func changeStateTo(_ state: PasscodeLockStateType) { lockState = state changeStateCalled = true diff --git a/PasscodeLockTests/Fakes/FakePasscodeLockDelegate.swift b/PasscodeLockTests/Fakes/FakePasscodeLockDelegate.swift index 3d82b9bf..72d4b85a 100644 --- a/PasscodeLockTests/Fakes/FakePasscodeLockDelegate.swift +++ b/PasscodeLockTests/Fakes/FakePasscodeLockDelegate.swift @@ -10,9 +10,9 @@ import Foundation class FakePasscodeLockDelegate: PasscodeLockTypeDelegate { - func passcodeLockDidSucceed(lock: PasscodeLockType) {} - func passcodeLockDidFail(lock: PasscodeLockType) {} - func passcodeLockDidChangeState(lock: PasscodeLockType) {} - func passcodeLock(lock: PasscodeLockType, addedSignAtIndex index: Int) {} - func passcodeLock(lock: PasscodeLockType, removedSignAtIndex index: Int) {} + func passcodeLockDidSucceed(_ lock: PasscodeLockType) {} + func passcodeLockDidFail(_ lock: PasscodeLockType) {} + func passcodeLockDidChangeState(_ lock: PasscodeLockType) {} + func passcodeLock(_ lock: PasscodeLockType, addedSignAtIndex index: Int) {} + func passcodeLock(_ lock: PasscodeLockType, removedSignAtIndex index: Int) {} } diff --git a/PasscodeLockTests/Fakes/FakePasscodeRepository.swift b/PasscodeLockTests/Fakes/FakePasscodeRepository.swift index 657b67fc..9c210521 100644 --- a/PasscodeLockTests/Fakes/FakePasscodeRepository.swift +++ b/PasscodeLockTests/Fakes/FakePasscodeRepository.swift @@ -18,7 +18,7 @@ class FakePasscodeRepository: PasscodeRepositoryType { var savePasscodeCalled = false var savedPasscode = [String]() - func savePasscode(passcode: [String]) { + func savePasscode(_ passcode: [String]) { savePasscodeCalled = true savedPasscode = passcode diff --git a/PasscodeLockTests/Fakes/FakePasscodeState.swift b/PasscodeLockTests/Fakes/FakePasscodeState.swift index 61bc3e3c..e2b345a4 100644 --- a/PasscodeLockTests/Fakes/FakePasscodeState.swift +++ b/PasscodeLockTests/Fakes/FakePasscodeState.swift @@ -21,7 +21,7 @@ class FakePasscodeState: PasscodeLockStateType { init() {} - func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType) { + func acceptPasscode(_ passcode: [String], fromLock lock: PasscodeLockType) { acceptedPasscode = passcode acceptPaccodeCalled = true diff --git a/PasscodeLockTests/PasscodeLock/ChangePasscodeStateTests.swift b/PasscodeLockTests/PasscodeLock/ChangePasscodeStateTests.swift index eade20ca..721b5ce5 100644 --- a/PasscodeLockTests/PasscodeLock/ChangePasscodeStateTests.swift +++ b/PasscodeLockTests/PasscodeLock/ChangePasscodeStateTests.swift @@ -31,7 +31,7 @@ class ChangePasscodeStateTests: XCTestCase { var didChangedState = false - override func passcodeLockDidChangeState(lock: PasscodeLockType) { + override func passcodeLockDidChangeState(_ lock: PasscodeLockType) { didChangedState = true } @@ -52,7 +52,7 @@ class ChangePasscodeStateTests: XCTestCase { var called = false - override func passcodeLockDidFail(lock: PasscodeLockType) { + override func passcodeLockDidFail(_ lock: PasscodeLockType) { called = true } diff --git a/PasscodeLockTests/PasscodeLock/ConfirmPasscodeStateTests.swift b/PasscodeLockTests/PasscodeLock/ConfirmPasscodeStateTests.swift index f122f702..47b9dfbc 100644 --- a/PasscodeLockTests/PasscodeLock/ConfirmPasscodeStateTests.swift +++ b/PasscodeLockTests/PasscodeLock/ConfirmPasscodeStateTests.swift @@ -32,7 +32,7 @@ class ConfirmPasscodeStateTests: XCTestCase { var called = false - override func passcodeLockDidSucceed(lock: PasscodeLockType) { + override func passcodeLockDidSucceed(_ lock: PasscodeLockType) { called = true } @@ -61,12 +61,12 @@ class ConfirmPasscodeStateTests: XCTestCase { var didFailed = false var didChangedState = false - override func passcodeLockDidFail(lock: PasscodeLockType) { + override func passcodeLockDidFail(_ lock: PasscodeLockType) { didFailed = true } - override func passcodeLockDidChangeState(lock: PasscodeLockType) { + override func passcodeLockDidChangeState(_ lock: PasscodeLockType) { didChangedState = true } diff --git a/PasscodeLockTests/PasscodeLock/EnterPasscodeStateTests.swift b/PasscodeLockTests/PasscodeLock/EnterPasscodeStateTests.swift index 2e4dcf94..c8fefca2 100644 --- a/PasscodeLockTests/PasscodeLock/EnterPasscodeStateTests.swift +++ b/PasscodeLockTests/PasscodeLock/EnterPasscodeStateTests.swift @@ -13,14 +13,14 @@ class NotificaionObserver: NSObject { var called = false var callCounter = 0 - func observe(notification: String) { + func observe(_ notification: String) { - let center = NSNotificationCenter.defaultCenter() + let center = NotificationCenter.default - center.addObserver(self, selector: "handle:", name: notification, object: nil) + center.addObserver(self, selector: #selector(NotificaionObserver.handle(_:)), name: NSNotification.Name(rawValue: notification), object: nil) } - func handle(notification: NSNotification) { + func handle(_ notification: Notification) { called = true callCounter += 1 @@ -50,7 +50,7 @@ class EnterPasscodeStateTests: XCTestCase { var called = false - override func passcodeLockDidSucceed(lock: PasscodeLockType) { + override func passcodeLockDidSucceed(_ lock: PasscodeLockType) { called = true } @@ -70,7 +70,7 @@ class EnterPasscodeStateTests: XCTestCase { var called = false - override func passcodeLockDidFail(lock: PasscodeLockType) { + override func passcodeLockDidFail(_ lock: PasscodeLockType) { called = true } diff --git a/PasscodeLockTests/PasscodeLock/PasscodeLockTests.swift b/PasscodeLockTests/PasscodeLock/PasscodeLockTests.swift index efb192a3..0ec9f6b9 100644 --- a/PasscodeLockTests/PasscodeLock/PasscodeLockTests.swift +++ b/PasscodeLockTests/PasscodeLock/PasscodeLockTests.swift @@ -29,7 +29,7 @@ class PasscodeLockTests: XCTestCase { var called = false - override func passcodeLockDidChangeState(lock: PasscodeLockType) { + override func passcodeLockDidChangeState(_ lock: PasscodeLockType) { called = true } @@ -51,7 +51,7 @@ class PasscodeLockTests: XCTestCase { var called = false var signIndex = 0 - override func passcodeLock(lock: PasscodeLockType, addedSignAtIndex index: Int) { + override func passcodeLock(_ lock: PasscodeLockType, addedSignAtIndex index: Int) { called = true signIndex = index @@ -78,7 +78,7 @@ class PasscodeLockTests: XCTestCase { var called = false var signIndex = 0 - override func passcodeLock(lock: PasscodeLockType, removedSignAtIndex index: Int) { + override func passcodeLock(_ lock: PasscodeLockType, removedSignAtIndex index: Int) { called = true signIndex = index diff --git a/PasscodeLockTests/PasscodeLock/SetPasscodeStateTests.swift b/PasscodeLockTests/PasscodeLock/SetPasscodeStateTests.swift index 20ef1214..e90429a0 100644 --- a/PasscodeLockTests/PasscodeLock/SetPasscodeStateTests.swift +++ b/PasscodeLockTests/PasscodeLock/SetPasscodeStateTests.swift @@ -31,7 +31,7 @@ class SetPasscodeStateTests: XCTestCase { var didChangedState = false - override func passcodeLockDidChangeState(lock: PasscodeLockType) { + override func passcodeLockDidChangeState(_ lock: PasscodeLockType) { didChangedState = true }