Update PinCodePreferences to include biometrics config

This commit is contained in:
ismailgulek 2020-07-24 17:45:01 +03:00
parent 4ff794f752
commit 52ef548159
No known key found for this signature in database
GPG key ID: E96336D42D9470A9

View file

@ -16,6 +16,7 @@ limitations under the License.
import Foundation
import KeychainAccess
import LocalAuthentication
/// Pin code preferences.
@objcMembers
@ -29,6 +30,7 @@ final class PinCodePreferences: NSObject {
private struct StoreKeys {
static let pin: String = "pin"
static let biometricsEnabled: String = "biometricsEnabled"
}
static let shared = PinCodePreferences()
@ -49,6 +51,10 @@ final class PinCodePreferences: NSObject {
return RiotSettings.shared.forcePinProtection
}
var isBiometricsAvailable: Bool {
return LAContext().canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
}
/// Allowed number of PIN trials before showing forgot help alert
let allowedNumberOfTrialsBeforeAlert: Int = 5
@ -81,8 +87,60 @@ final class PinCodePreferences: NSObject {
}
}
var biometricsEnabled: Bool? {
get {
do {
return try store.object(forKey: StoreKeys.biometricsEnabled) as? Bool
} catch let error {
NSLog("[PinCodePreferences] Error when reading biometrics enabled from store: \(error)")
return nil
}
} set {
do {
try store.set(newValue, forKey: StoreKeys.biometricsEnabled)
} catch let error {
NSLog("[PinCodePreferences] Error when storing biometrics enabled to the store: \(error)")
}
}
}
var isBiometricsSet: Bool {
return biometricsEnabled == true
}
func localizedBiometricsName() -> String? {
if isBiometricsAvailable {
let context = LAContext()
switch context.biometryType {
case .touchID:
return VectorL10n.biometricsModeTouchId
case .faceID:
return VectorL10n.biometricsModeFaceId
default:
return nil
}
}
return nil
}
func biometricsIcon() -> UIImage? {
if isBiometricsAvailable {
let context = LAContext()
switch context.biometryType {
case .touchID:
return Asset.Images.touchidIcon.image
case .faceID:
return Asset.Images.faceidIcon.image
default:
return nil
}
}
return nil
}
/// Resets user PIN
func reset() {
pin = nil
biometricsEnabled = nil
}
}