Skip to content

Commit 500522d

Browse files
committed
Fixes error alert
1 parent 595a523 commit 500522d

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

.DS_Store

0 Bytes
Binary file not shown.

IrProCapture/Camera/IrProError.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Foundation
66
/// - Initializing the IR camera
77
/// - Setting up capture sessions
88
/// - Configuring video input/output
9-
enum IrProError: String, Error {
9+
enum IrProError: String, Error, LocalizedError {
1010
/// No IR camera devices were detected on the system
1111
case noDevicesFound = "No IR camera devices found."
1212
/// Failed to create an AVCaptureDeviceInput for the IR camera
@@ -15,4 +15,6 @@ enum IrProError: String, Error {
1515
case failedToAddToSession = "Could not add to capture session."
1616
/// Failed to configure the video output for the capture session
1717
case failedToAddOutput = "Failed to set video output."
18-
}
18+
19+
var errorDescription: String? { rawValue }
20+
}

IrProCapture/Views/CaptureToolbar.swift

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ struct CaptureToolbar: View {
1818

1919
// Capture image button
2020
Button(action: {
21-
guard let success = try? model.start(),
22-
success else {
23-
alertMessage = "Failed to start camera."
24-
return
21+
do {
22+
if !(try model.start()) {
23+
alertMessage = "Failed to start camera."
24+
}
25+
} catch let error {
26+
alertMessage = error.localizedDescription
2527
}
2628
}) {
2729
Image(systemName: "play.square")
@@ -87,15 +89,18 @@ struct CaptureToolbar: View {
8789

8890
Spacer()
8991
}
90-
.alert(isPresented: Binding<Bool>(
91-
get: { alertMessage != nil },
92-
set: { _ in alertMessage = nil }
93-
)) {
94-
Alert(
95-
title: Text("Error"),
96-
message: Text(alertMessage ?? ""),
97-
dismissButton: .default(Text("OK"))
98-
)
92+
.alert(
93+
"Error",
94+
isPresented: Binding(
95+
get: { alertMessage != nil },
96+
set: { if !$0 { alertMessage = nil } }
97+
),
98+
presenting: alertMessage
99+
) { _ in
100+
// actions
101+
Button("OK", role: .cancel) { }
102+
} message: { msg in
103+
Text(msg)
99104
}
100105
}
101106

0 commit comments

Comments
 (0)