From c16f4246845acc859f2f9f59846279c8f09859cc Mon Sep 17 00:00:00 2001 From: Tuan Pham Date: Fri, 20 Oct 2023 11:24:34 -0500 Subject: [PATCH 1/3] fix: update session timed out error to be non-blocking --- ...ionViewModel+FaceDetectionResultHandler.swift | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionViewModel+FaceDetectionResultHandler.swift b/Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionViewModel+FaceDetectionResultHandler.swift index a7458ed4..8a87ac03 100644 --- a/Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionViewModel+FaceDetectionResultHandler.swift +++ b/Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionViewModel+FaceDetectionResultHandler.swift @@ -89,9 +89,11 @@ extension FaceLivenessDetectionViewModel: FaceDetectionResultHandler { noFitStartTime = Date() } if let elapsedTime = noFitStartTime?.timeIntervalSinceNow, abs(elapsedTime) >= noFitTimeoutInterval { - self.livenessState - .unrecoverableStateEncountered(.timedOut) - self.captureSession.stopRunning() + DispatchQueue.main.async { + self.livenessState + .unrecoverableStateEncountered(.timedOut) + self.captureSession.stopRunning() + } } } @@ -100,9 +102,11 @@ extension FaceLivenessDetectionViewModel: FaceDetectionResultHandler { noFitStartTime = Date() } if let elapsedTime = noFitStartTime?.timeIntervalSinceNow, abs(elapsedTime) >= noFitTimeoutInterval { - self.livenessState - .unrecoverableStateEncountered(.timedOut) - self.captureSession.stopRunning() + DispatchQueue.main.async { + self.livenessState + .unrecoverableStateEncountered(.timedOut) + self.captureSession.stopRunning() + } } } From 1a63309b8f49c07925808372c2d179bb49821ab3 Mon Sep 17 00:00:00 2001 From: Tuan Pham Date: Fri, 20 Oct 2023 11:28:00 -0500 Subject: [PATCH 2/3] chore: remove duplicate code --- ...ViewModel+FaceDetectionResultHandler.swift | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionViewModel+FaceDetectionResultHandler.swift b/Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionViewModel+FaceDetectionResultHandler.swift index 8a87ac03..28fbbb42 100644 --- a/Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionViewModel+FaceDetectionResultHandler.swift +++ b/Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionViewModel+FaceDetectionResultHandler.swift @@ -89,11 +89,7 @@ extension FaceLivenessDetectionViewModel: FaceDetectionResultHandler { noFitStartTime = Date() } if let elapsedTime = noFitStartTime?.timeIntervalSinceNow, abs(elapsedTime) >= noFitTimeoutInterval { - DispatchQueue.main.async { - self.livenessState - .unrecoverableStateEncountered(.timedOut) - self.captureSession.stopRunning() - } + handleSessionTimedOut() } } @@ -102,11 +98,7 @@ extension FaceLivenessDetectionViewModel: FaceDetectionResultHandler { noFitStartTime = Date() } if let elapsedTime = noFitStartTime?.timeIntervalSinceNow, abs(elapsedTime) >= noFitTimeoutInterval { - DispatchQueue.main.async { - self.livenessState - .unrecoverableStateEncountered(.timedOut) - self.captureSession.stopRunning() - } + handleSessionTimedOut() } } @@ -134,4 +126,12 @@ extension FaceLivenessDetectionViewModel: FaceDetectionResultHandler { } } } + + private func handleSessionTimedOut() { + DispatchQueue.main.async { + self.livenessState + .unrecoverableStateEncountered(.timedOut) + self.captureSession.stopRunning() + } + } } From 193c343f9e39bcf2a258cf69a12cf5974a9100fe Mon Sep 17 00:00:00 2001 From: Tuan Pham Date: Fri, 20 Oct 2023 14:55:30 -0500 Subject: [PATCH 3/3] chore: fix failed timed out unit test --- Tests/FaceLivenessTests/LivenessTests.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tests/FaceLivenessTests/LivenessTests.swift b/Tests/FaceLivenessTests/LivenessTests.swift index 1b3c13b2..39f84f53 100644 --- a/Tests/FaceLivenessTests/LivenessTests.swift +++ b/Tests/FaceLivenessTests/LivenessTests.swift @@ -152,6 +152,7 @@ final class FaceLivenessDetectionViewModelTestCase: XCTestCase { XCTAssertNotEqual(self.viewModel.livenessState.state, .encounteredUnrecoverableError(.timedOut)) try await Task.sleep(seconds: 1) self.viewModel.handleNoFaceFit(instruction: .tooFar(nearnessPercentage: 0.2), percentage: 0.2) + try await Task.sleep(seconds: 1) XCTAssertEqual(self.viewModel.livenessState.state, .encounteredUnrecoverableError(.timedOut)) } @@ -168,6 +169,7 @@ final class FaceLivenessDetectionViewModelTestCase: XCTestCase { XCTAssertNotEqual(self.viewModel.livenessState.state, .encounteredUnrecoverableError(.timedOut)) try await Task.sleep(seconds: 1) self.viewModel.handleNoFaceDetected() + try await Task.sleep(seconds: 1) XCTAssertEqual(self.viewModel.livenessState.state, .encounteredUnrecoverableError(.timedOut)) } }