-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add BarcodeScanner module (#6)
This PR adds support for the [BarcodeScanner](https://learn.microsoft.com/en-us/uwp/api/windows.devices.pointofservice.barcodescanner?view=winrt-22621) class from the [Windows.Devices.PointOfService](https://learn.microsoft.com/en-us/uwp/api/windows.devices.pointofservice?view=winrt-22621) namespace. It also adds a hook and an example.
- Loading branch information
Showing
5 changed files
with
151 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { TurboModule } from 'react-native/Libraries/TurboModule/RCTExport'; | ||
import { TurboModuleRegistry } from 'react-native'; | ||
|
||
export interface BarcodeScannerSpec extends TurboModule { | ||
/** | ||
* Returns the first available barcode scanner. | ||
* @throws {Error} A device is not found. | ||
* @see https://learn.microsoft.com/en-us/uwp/api/windows.devices.pointofservice.barcodescanner.getdefaultasync?view=winrt-22621#windows-devices-pointofservice-barcodescanner-getdefaultasync | ||
*/ | ||
getDefaultAsync(): Promise<ReactBarcodeScanner>; | ||
} | ||
|
||
export default TurboModuleRegistry.get<BarcodeScannerSpec>( | ||
'BarcodeScanner' | ||
) as BarcodeScannerSpec | null; | ||
|
||
/** @link windows/ReactNativePosTools/NativeBarcodeScanner.cs */ | ||
export interface ReactBarcodeScanner { | ||
/** @see https://learn.microsoft.com/en-us/uwp/api/windows.devices.pointofservice.barcodescanner.deviceid?view=winrt-22621 */ | ||
DeviceId: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
using Microsoft.ReactNative.Managed; | ||
|
||
using Windows.Devices.PointOfService; | ||
|
||
namespace ReactNativePosTools | ||
{ | ||
// This is the structure that will be returned to the JavaScript side | ||
// src/NativeBarcodeScanner.ts | ||
struct ReactBarcodeScanner | ||
{ | ||
public string DeviceId { get; set; } | ||
|
||
public ReactBarcodeScanner(string deviceId) | ||
{ | ||
DeviceId = deviceId; | ||
} | ||
}; | ||
|
||
[ReactModule("BarcodeScanner")] | ||
internal class NativeBarcodeScanner | ||
{ | ||
private BarcodeScanner scanner = null; | ||
|
||
[ReactMethod("getDefaultAsync")] | ||
public async void GetDefaultAsync(ReactPromise<ReactBarcodeScanner> result) | ||
{ | ||
try | ||
{ | ||
scanner = await BarcodeScanner.GetDefaultAsync(); | ||
if (scanner == null) | ||
{ | ||
result.Reject(new ReactError { Message = "Scanner was not found" }); | ||
} | ||
else | ||
{ | ||
result.Resolve(new ReactBarcodeScanner { DeviceId = scanner.DeviceId }); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
result.Reject(new ReactError { Message = ex.Message }); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters