@@ -40,90 +40,15 @@ struct BarcodeScannerContainerRepresentable: UIViewControllerRepresentable {
4040 let onScan : ( Result < String , Error > ) -> Void
4141
4242 func makeUIViewController( context: Context ) -> UIViewController {
43- let featureFlagService = ServiceLocator . featureFlagService
44-
45- if featureFlagService. isFeatureFlagEnabled ( . pointOfSaleBarcodeScanningi2) {
46- return GameControllerBarcodeScannerHostingController (
47- configuration: configuration,
48- onScan: onScan
49- )
50- } else {
51- return BarcodeScannerHostingController (
52- configuration: configuration,
53- onScan: onScan
54- )
55- }
43+ return GameControllerBarcodeScannerHostingController (
44+ configuration: configuration,
45+ onScan: onScan
46+ )
5647 }
5748
5849 func updateUIViewController( _ uiViewController: UIViewController , context: Context ) { }
5950}
6051
61- /// A UIHostingController that handles keyboard input events for barcode scanning.
62- /// This controller captures keyboard input and interprets it as barcode data when a terminating
63- /// character is detected.
64- class BarcodeScannerHostingController : UIHostingController < EmptyView > {
65- private let configuration : HIDBarcodeParserConfiguration
66- private let scanner : HIDBarcodeParser
67-
68- init (
69- configuration: HIDBarcodeParserConfiguration ,
70- onScan: @escaping ( Result < String , Error > ) -> Void
71- ) {
72- self . configuration = configuration
73- self . scanner = HIDBarcodeParser ( configuration: configuration,
74- onScan: onScan)
75- super. init ( rootView: EmptyView ( ) )
76- }
77-
78- @MainActor required dynamic init ? ( coder aDecoder: NSCoder ) {
79- fatalError ( " init(coder:) has not been implemented " )
80- }
81-
82- override var canBecomeFirstResponder : Bool { true }
83-
84- override func viewDidAppear( _ animated: Bool ) {
85- super. viewDidAppear ( animated)
86- becomeFirstResponder ( )
87- }
88-
89- override func viewDidDisappear( _ animated: Bool ) {
90- super. viewDidDisappear ( animated)
91- resignFirstResponder ( )
92- }
93-
94- override func pressesBegan( _ presses: Set < UIPress > , with event: UIPressesEvent ? ) {
95- /// We don't call super here because it helps prevent the system from hiding the software keyboard when
96- /// a textfield is next used.
97- }
98-
99- /// Handles the end of keyboard press events, interpreting them as barcode input.
100- /// When a terminating character is detected, the accumulated buffer is treated as a complete
101- /// barcode and passed to the onScan callback.
102- /// We don't call `super` here because we don't other responder chain items to handle our barcode as well,
103- /// as this could cause unexpected behavior.
104- override func pressesEnded( _ presses: Set < UIPress > , with event: UIPressesEvent ? ) {
105- /// While a scanner should just "press" each key once, in theory it's possible for presses to be cancelled
106- /// or change between the `began` call and the `ended` call.
107- /// It's better practice for barcode scanning to only consider the presses when they end.
108- for press in presses {
109- guard let key = press. key else { continue }
110- scanner. processKeyPress ( key)
111- }
112- }
113-
114- override func pressesChanged( _ presses: Set < UIPress > , with event: UIPressesEvent ? ) {
115- super. pressesChanged ( presses, with: event)
116- }
117-
118- /// `pressesCancelled` is rarely called, but Apple's documentation suggests it's possible and that crashes may occur if it's not handled.
119- /// It makes sense to clear the buffer when this happens.
120- /// We call super in case other presses are handled elsewhere in the responder chain.
121- override func pressesCancelled( _ presses: Set < UIPress > , with event: UIPressesEvent ? ) {
122- scanner. cancel ( )
123- super. pressesCancelled ( presses, with: event)
124- }
125- }
126-
12752/// A UIHostingController that handles GameController keyboard input events for barcode scanning.
12853/// This controller uses GameController framework exclusively for language-independent barcode scanning.
12954final class GameControllerBarcodeScannerHostingController : UIHostingController < EmptyView > {
0 commit comments