Browse Source

Merge pull request #337 from loopandlearn/meal-bolus-auth

Require auth for meal bolus
Marion Barker 1 năm trước cách đây
mục cha
commit
8c0705eec9
1 tập tin đã thay đổi với 35 bổ sung1 xóa
  1. 35 1
      LoopFollow/Remote/TRC/MealView.swift

+ 35 - 1
LoopFollow/Remote/TRC/MealView.swift

@@ -8,6 +8,7 @@
 
 import SwiftUI
 import HealthKit
+import LocalAuthentication
 
 struct MealView: View {
     @Environment(\.presentationMode) private var presentationMode
@@ -193,7 +194,15 @@ struct MealView: View {
                         message: Text(message),
                         primaryButton: .default(Text("Confirm"), action: {
                             DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
-                                sendMealCommand()
+                                if bolusAmount > 0 {
+                                    authenticateUser { success in
+                                        if success {
+                                            sendMealCommand()
+                                        }
+                                    }
+                                } else {
+                                    sendMealCommand()
+                                }
                             }
                         }),
                         secondaryButton: .cancel()
@@ -281,4 +290,29 @@ struct MealView: View {
         alertType = .validationError
         showAlert = true
     }
+
+    private func authenticateUser(completion: @escaping (Bool) -> Void) {
+        let context = LAContext()
+        var error: NSError?
+
+        let reason = "Confirm your identity to send bolus."
+
+        if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
+            context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, _ in
+                DispatchQueue.main.async {
+                    completion(success)
+                }
+            }
+        } else if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
+            context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { success, _ in
+                DispatchQueue.main.async {
+                    completion(success)
+                }
+            }
+        } else {
+            DispatchQueue.main.async {
+                completion(false)
+            }
+        }
+    }
 }