66//
77
88import UIKit
9+ import Combine
910import CoreData
1011import LocalAuthentication
1112
1213class ExpenseSettingsViewModel : ObservableObject {
1314
1415 var csvModelArr = [ ExpenseCSVModel] ( )
1516
17+ var cancellableBiometricTask : AnyCancellable ? = nil
18+
1619 @Published var currency = UserDefaults . standard. string ( forKey: UD_EXPENSE_CURRENCY) ?? " "
1720 @Published var enableBiometric = UserDefaults . standard. bool ( forKey: UD_USE_BIOMETRIC) {
1821 didSet {
19- if enableBiometric { authBiometric ( ) }
22+ if enableBiometric { authenticate ( ) }
2023 else { UserDefaults . standard. setValue ( false , forKey: UD_USE_BIOMETRIC) }
2124 }
2225 }
@@ -25,31 +28,23 @@ class ExpenseSettingsViewModel: ObservableObject {
2528 @Published var showAlert = false
2629
2730 init ( ) { }
28-
29- func authBiometric( ) {
30- let scanner = LAContext ( )
31- var error : NSError ?
32- if scanner. canEvaluatePolicy ( . deviceOwnerAuthenticationWithBiometrics, error: & error) {
33- scanner. evaluatePolicy ( . deviceOwnerAuthenticationWithBiometrics, localizedReason: " To Unlock \( APP_NAME) " ) { ( status, err) in
34- if let err = err {
35- DispatchQueue . main. async {
36- self . enableBiometric = false
37- self . alertMsg = err. localizedDescription
38- self . showAlert = true
39- }
40- } else { UserDefaults . standard. setValue ( true , forKey: UD_USE_BIOMETRIC) }
41- }
42- } else if scanner. canEvaluatePolicy ( . deviceOwnerAuthentication, error: & error) {
43- scanner. evaluatePolicy ( . deviceOwnerAuthentication, localizedReason: " To Unlock \( APP_NAME) " ) { ( status, err) in
44- if let err = err {
45- DispatchQueue . main. async {
46- self . enableBiometric = false
47- self . alertMsg = err. localizedDescription
48- self . showAlert = true
49- }
50- } else { UserDefaults . standard. setValue ( true , forKey: UD_USE_BIOMETRIC) }
31+
32+ func authenticate( ) {
33+ showAlert = false
34+ alertMsg = " "
35+ cancellableBiometricTask = BiometricAuthUtlity . shared. authenticate ( )
36+ . receive ( on: DispatchQueue . main)
37+ . sink ( receiveCompletion: { completion in
38+ switch completion {
39+ case . failure( let error) :
40+ self . showAlert = true
41+ self . alertMsg = error. description
42+ self . enableBiometric = false
43+ default : return
44+ }
45+ } ) { _ in
46+ UserDefaults . standard. setValue ( true , forKey: UD_USE_BIOMETRIC)
5147 }
52- }
5348 }
5449
5550 func getBiometricType( ) -> String {
@@ -113,4 +108,8 @@ class ExpenseSettingsViewModel: ObservableObject {
113108
114109 print ( path ?? " not found " )
115110 }
111+
112+ deinit {
113+ cancellableBiometricTask = nil
114+ }
116115}
0 commit comments