Skip to content

Commit ca1f967

Browse files
authored
chore: add error handling to create session in sample app (#121)
1 parent bdd3dc8 commit ca1f967

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

HostApp/HostApp/Views/StartSessionView.swift

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct StartSessionView: View {
1313
@ObservedObject var viewModel = StartSessionViewModel()
1414
@Binding var sessionID: String
1515
@Binding var isPresentingContainerView: Bool
16+
@State private var showAlert = false
1617

1718
var body: some View {
1819
VStack {
@@ -31,18 +32,34 @@ struct StartSessionView: View {
3132
dark: .hex("#7dd6e8")
3233
),
3334
action: {
34-
viewModel.createSession {
35-
sessionID = $0
36-
isPresentingContainerView = true
35+
viewModel.createSession { sessionId, err in
36+
if let sessionId = sessionId {
37+
sessionID = sessionId
38+
isPresentingContainerView = true
39+
}
40+
41+
showAlert = err != nil
3742
}
3843
},
3944
enabled: viewModel.isSignedIn
4045
)
46+
.alert(isPresented: $showAlert) {
47+
Alert(
48+
title: Text("Error Creating Liveness Session"),
49+
message: Text("Unable to create a liveness session id. Please try again."),
50+
dismissButton: .default(
51+
Text("OK"),
52+
action: {
53+
isPresentingContainerView = false
54+
}
55+
)
56+
)
57+
}
4158

4259
Spacer()
4360
HStack {
4461
Spacer()
45-
Text("v0.1.16")
62+
Text("v0.1.19")
4663
.font(.callout)
4764
.padding()
4865
}

HostApp/HostApp/Views/StartSessionViewModel.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ class StartSessionViewModel: ObservableObject {
2525
? .signedIn(action: signOut)
2626
: .signedOut(action: signIn)
2727
} catch {
28+
presentationState = .signedOut(action: signIn)
2829
print("Error fetching auth session", error)
2930
}
3031

3132
}
3233
}
3334

34-
func createSession(_ completion: @escaping (String) -> Void) {
35+
func createSession(_ completion: @escaping (String?, Error?) -> Void) {
3536
Task { @MainActor in
37+
let currentPresentationState = presentationState
3638
presentationState = .loading
3739
let request = RESTRequest(
3840
apiName: "liveness",
@@ -45,9 +47,12 @@ class StartSessionViewModel: ObservableObject {
4547
CreateSessionResponse.self,
4648
from: data
4749
)
48-
completion(response.sessionId)
50+
presentationState = currentPresentationState
51+
completion(response.sessionId, nil)
4952
} catch {
53+
presentationState = currentPresentationState
5054
print("Error creating session", error)
55+
completion(nil, error)
5156
}
5257
}
5358
}

0 commit comments

Comments
 (0)