Skip to content

Commit

Permalink
chore: add error handling to create session in sample app (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
phantumcode authored Apr 1, 2024
1 parent bdd3dc8 commit ca1f967
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
25 changes: 21 additions & 4 deletions HostApp/HostApp/Views/StartSessionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct StartSessionView: View {
@ObservedObject var viewModel = StartSessionViewModel()
@Binding var sessionID: String
@Binding var isPresentingContainerView: Bool
@State private var showAlert = false

var body: some View {
VStack {
Expand All @@ -31,18 +32,34 @@ struct StartSessionView: View {
dark: .hex("#7dd6e8")
),
action: {
viewModel.createSession {
sessionID = $0
isPresentingContainerView = true
viewModel.createSession { sessionId, err in
if let sessionId = sessionId {
sessionID = sessionId
isPresentingContainerView = true
}

showAlert = err != nil
}
},
enabled: viewModel.isSignedIn
)
.alert(isPresented: $showAlert) {
Alert(
title: Text("Error Creating Liveness Session"),
message: Text("Unable to create a liveness session id. Please try again."),
dismissButton: .default(
Text("OK"),
action: {
isPresentingContainerView = false
}
)
)
}

Spacer()
HStack {
Spacer()
Text("v0.1.16")
Text("v0.1.19")
.font(.callout)
.padding()
}
Expand Down
9 changes: 7 additions & 2 deletions HostApp/HostApp/Views/StartSessionViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ class StartSessionViewModel: ObservableObject {
? .signedIn(action: signOut)
: .signedOut(action: signIn)
} catch {
presentationState = .signedOut(action: signIn)
print("Error fetching auth session", error)
}

}
}

func createSession(_ completion: @escaping (String) -> Void) {
func createSession(_ completion: @escaping (String?, Error?) -> Void) {
Task { @MainActor in
let currentPresentationState = presentationState
presentationState = .loading
let request = RESTRequest(
apiName: "liveness",
Expand All @@ -45,9 +47,12 @@ class StartSessionViewModel: ObservableObject {
CreateSessionResponse.self,
from: data
)
completion(response.sessionId)
presentationState = currentPresentationState
completion(response.sessionId, nil)
} catch {
presentationState = currentPresentationState
print("Error creating session", error)
completion(nil, error)
}
}
}
Expand Down

0 comments on commit ca1f967

Please sign in to comment.