Skip to content

Commit

Permalink
fixed file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jumaallan committed Aug 13, 2024
1 parent 23358b3 commit 26957bf
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {

// MARK: - Other Properties

internal var selfieFile: URL?
internal var livenessFiles: [URL]?
private var error: Error?
private var selfieCaptureResultStore: SelfieCaptureResultStore?
private var didSubmitBiometricJob: Bool = false

// MARK: - UI Properties
Expand All @@ -45,42 +46,62 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {
}

func onRetry() {
if selfieCaptureResultStore == nil {
if selfieFile == nil {
DispatchQueue.main.async { self.step = .selfie }
} else {
submitJob(selfieCaptureResultStore: selfieCaptureResultStore!)
submitJob()
}
}

func onFinished(delegate: BiometricKycResultDelegate) {
if let selfieCaptureResultStore {
delegate.didSucceed(
selfieImage: getRelativePath(from: selfieCaptureResultStore.selfie)!,
livenessImages: selfieCaptureResultStore.livenessImages.compactMap { getRelativePath(from: $0) },
didSubmitBiometricJob: didSubmitBiometricJob
)
if let selfieFile {
if let livenessFiles {
delegate.didSucceed(
selfieImage: getRelativePath(from: selfieFile)!,
livenessImages: livenessFiles.compactMap { getRelativePath(from: $0) },
didSubmitBiometricJob: didSubmitBiometricJob
)
}
} else if let error {
delegate.didError(error: error)
} else {
delegate.didError(error: SmileIDError.unknown("onFinish with no result or error"))
}
}

func submitJob(selfieCaptureResultStore: SelfieCaptureResultStore) {
func submitJob() {
DispatchQueue.main.async { self.step = .processing(.inProgress) }
Task {
do {
let livenessImages = selfieCaptureResultStore.livenessImages
let selfieImage = selfieCaptureResultStore.selfie
selfieFile = try LocalStorage.getFileByType(
jobId: jobId,
fileType: FileType.selfie
)

livenessFiles = try LocalStorage.getFilesByType(
jobId: jobId,
fileType: FileType.liveness
)

guard let selfieFile else {
// Set step to .selfieCapture so that the Retry button goes back to this step
DispatchQueue.main.async { self.step = .selfie }
error = SmileIDError.unknown("Error capturing selfie")
return
}

var allFiles = [URL]()
let infoJson = try LocalStorage.createInfoJsonFile(
jobId: jobId,
idInfo: idInfo.copy(entered: true),
selfie: selfieImage,
livenessImages: livenessImages
)
let zipData = try LocalStorage.zipFiles(
at: livenessImages + [selfieImage] + [infoJson]
selfie: selfieFile,
livenessImages: livenessFiles
)
allFiles.append(contentsOf: [selfieFile, infoJson])
if let livenessFiles {
allFiles.append(contentsOf: livenessFiles)
}
let zipData = try LocalStorage.zipFiles(at: allFiles)
let authRequest = AuthenticationRequest(
jobType: .biometricKyc,
enrollment: false,
Expand Down Expand Up @@ -128,18 +149,6 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {
didSubmitBiometricJob = true
do {
try LocalStorage.moveToSubmittedJobs(jobId: self.jobId)
self.selfieCaptureResultStore = SelfieCaptureResultStore(
selfie: try LocalStorage.getFileByType(
jobId: jobId,
fileType: FileType.selfie,
submitted: true
) ?? selfieCaptureResultStore.selfie,
livenessImages: try LocalStorage.getFilesByType(
jobId: jobId,
fileType: FileType.liveness,
submitted: true
) ?? selfieCaptureResultStore.livenessImages
)
} catch {
print("Error moving job to submitted directory: \(error)")
self.error = error
Expand All @@ -149,24 +158,10 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {
DispatchQueue.main.async { self.step = .processing(.success) }
} catch let error as SmileIDError {
do {
let didMove = try LocalStorage.handleOfflineJobFailure(
_ = try LocalStorage.handleOfflineJobFailure(
jobId: self.jobId,
error: error
)
if didMove {
self.selfieCaptureResultStore = SelfieCaptureResultStore(
selfie: try LocalStorage.getFileByType(
jobId: jobId,
fileType: FileType.selfie,
submitted: true
) ?? selfieCaptureResultStore.selfie,
livenessImages: try LocalStorage.getFilesByType(
jobId: jobId,
fileType: FileType.liveness,
submitted: true
) ?? selfieCaptureResultStore.livenessImages
)
}
} catch {
print("Error moving job to submitted directory: \(error)")
self.error = error
Expand Down Expand Up @@ -199,24 +194,15 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {

extension OrchestratedBiometricKycViewModel: SmartSelfieResultDelegate {
func didSucceed(
selfieImage: URL,
livenessImages: [URL],
selfieImage _: URL,
livenessImages _: [URL],
apiResponse _: SmartSelfieResponse?
) {
selfieCaptureResultStore = SelfieCaptureResultStore(
selfie: selfieImage,
livenessImages: livenessImages
)
if let selfieCaptureResultStore {
submitJob(selfieCaptureResultStore: selfieCaptureResultStore)
} else {
error = SmileIDError.unknown("Failed to save selfie capture result")
DispatchQueue.main.async { self.step = .processing(.error) }
}
submitJob()
}

func didError(error _: Error) {
error = SmileIDError.unknown("Failed to capture selfie")
error = SmileIDError.unknown("Error capturing selfie")
DispatchQueue.main.async { self.step = .processing(.error) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,37 @@ internal class IOrchestratedDocumentVerificationViewModel<T, U: JobResult>: Obse
}

func submitJob() {
guard let documentFrontFile else {
// Set step to .frontDocumentCapture so that the Retry button goes back to this step
step = .frontDocumentCapture
onError(error: SmileIDError.unknown("Error getting document front file"))
return
}
guard let selfieFile else {
// Set step to .selfieCapture so that the Retry button goes back to this step
step = .selfieCapture
onError(error: SmileIDError.unknown("Error getting selfie file"))
return
}
DispatchQueue.main.async {
self.step = .processing(.inProgress)
}
Task {
let zip: Data
do {
selfieFile = try LocalStorage.getFileByType(
jobId: jobId,
fileType: FileType.selfie
)

livenessFiles = try LocalStorage.getFilesByType(
jobId: jobId,
fileType: FileType.liveness
)

guard let documentFrontFile else {
// Set step to .frontDocumentCapture so that the Retry button goes back to this step
step = .frontDocumentCapture
onError(error: SmileIDError.unknown("Error getting document front file"))
return
}

guard let selfieFile else {
// Set step to .selfieCapture so that the Retry button goes back to this step
step = .selfieCapture
onError(error: SmileIDError.unknown("Error getting selfie file"))
return
}

DispatchQueue.main.async {
self.step = .processing(.inProgress)
}

var allFiles = [URL]()
let frontDocumentUrl = try LocalStorage.createDocumentFile(
jobId: jobId,
Expand Down Expand Up @@ -286,9 +299,11 @@ internal class IOrchestratedDocumentVerificationViewModel<T, U: JobResult>: Obse
}

extension IOrchestratedDocumentVerificationViewModel: SmartSelfieResultDelegate {
func didSucceed(selfieImage: URL, livenessImages: [URL], apiResponse _: SmartSelfieResponse?) {
selfieFile = selfieImage
livenessFiles = livenessImages
func didSucceed(
selfieImage _: URL,
livenessImages _: [URL],
apiResponse _: SmartSelfieResponse?
) {
submitJob()
}

Expand Down

0 comments on commit 26957bf

Please sign in to comment.