-
Notifications
You must be signed in to change notification settings - Fork 436
Expand file tree
/
Copy pathBarcodeScannerView.swift
More file actions
150 lines (138 loc) · 5.02 KB
/
BarcodeScannerView.swift
File metadata and controls
150 lines (138 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import Shared
import SwiftUI
struct BarcodeScannerView: View {
static let cameraSquareSize: CGFloat = 320
@Environment(\.dismiss) private var dismiss
@StateObject private var viewModel: BarcodeScannerViewModel
// Use single data model so both camera previews use same camera stream
@StateObject private var cameraDataModel = BarcodeScannerDataModel()
private let flashlightIcon = MaterialDesignIcons.flashlightIcon.image(
ofSize: .init(width: 24, height: 24),
color: .white
)
private let title: String
private let description: String
private let alternativeOptionLabel: String?
init(
title: String,
description: String,
alternativeOptionLabel: String? = nil,
incomingMessageId: Int
) {
self.title = title
self.description = description
self.alternativeOptionLabel = alternativeOptionLabel
self._viewModel = .init(wrappedValue: .init(incomingMessageId: incomingMessageId))
}
var body: some View {
ZStack(alignment: .top) {
ZStack {
cameraBackground
cameraSquare
}
.ignoresSafeArea()
.frame(maxWidth: .infinity)
.frame(maxHeight: .infinity)
topInformation
}
.onAppear {
cameraDataModel.delegate = viewModel
}
.onDisappear {
cameraDataModel.turnOffFlashlight()
}
}
private var topInformation: some View {
VStack(spacing: 8) {
HStack {
Spacer()
CloseButton(tint: .white, size: .medium) {
viewModel.aborted(.canceled)
dismiss()
}
.accessibilityHint(.init(L10n.closeLabel))
}
Group {
Text(title)
.padding(.top)
.font(DesignSystem.Font.title2.bold())
Text(description)
.font(DesignSystem.Font.subheadline)
}
.foregroundColor(.white)
if let alternativeOptionLabel {
Button {
viewModel.aborted(.alternativeOptions)
dismiss()
} label: {
Text(alternativeOptionLabel)
.font(.subheadline)
.foregroundColor(.accentColor)
}
.padding(.top)
}
}
.padding()
}
private var cameraBackground: some View {
GeometryReader { proxy in
BarcodeScannerCameraView(screenSize: proxy.size, model: cameraDataModel)
.ignoresSafeArea()
.frame(maxWidth: .infinity)
.frame(maxHeight: .infinity)
.overlay {
Color.black.opacity(0.8)
}
}
}
private var cameraSquare: some View {
// No size needs to be provided here since it wont forward to rectOfInterest
BarcodeScannerCameraView(screenSize: .zero, model: cameraDataModel, shouldStartCamera: false)
.ignoresSafeArea()
.frame(maxWidth: .infinity)
.frame(maxHeight: .infinity)
.mask {
RoundedRectangle(cornerSize: CGSize(width: 20, height: 20))
.frame(
width: BarcodeScannerView.cameraSquareSize,
height: BarcodeScannerView.cameraSquareSize
)
}
.overlay {
RoundedRectangle(cornerSize: CGSize(width: 20, height: 20))
.stroke(Color.clear, lineWidth: 1)
.frame(
width: BarcodeScannerView.cameraSquareSize,
height: BarcodeScannerView.cameraSquareSize
)
}
.shadow(color: .haPrimary.opacity(0.8), radius: 10, x: 0, y: 0)
.overlay {
VStack {
Spacer()
Button(action: {
cameraDataModel.toggleFlashlight()
}, label: {
Image(uiImage: flashlightIcon)
.padding()
.background(Color(uiColor: .init(hex: "#384956")))
.mask(Circle())
.padding([.trailing, .bottom], DesignSystem.Spaces.two)
})
.frame(maxWidth: .infinity, alignment: .trailing)
}
.frame(
width: BarcodeScannerView.cameraSquareSize,
height: BarcodeScannerView.cameraSquareSize
)
}
}
}
#Preview {
BarcodeScannerView(title: "Scan QR-code", description: "Find the code on your device", incomingMessageId: 1)
}
final class BarcodeScannerHostingController: UIHostingController<BarcodeScannerView> {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
[.portrait]
}
}