Skip to content

Commit

Permalink
- update nativescript 8 dependencies #260
Browse files Browse the repository at this point in the history
- Crash on scan (NS7) #251 (and others)
  • Loading branch information
EddyVerbruggen committed Apr 10, 2021
1 parent 636d557 commit 34dff3b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

> 💡 Plugin version 4.0.0+ is compatible with NativeScript 7+. If you need to target older NativeScript versions, please stick to plugin version 3.4.2.
> ⚠️ If you app crashes with the 4.x version on iOS, try setting `beepOnScan` to `false`. And please open an issue if so.
> ⚠️ With 4.1.0+ you can use `beepOnScan` on iOS again (it used to crash in older 4.x versions).
## Want a quick demo?

Expand Down
2 changes: 1 addition & 1 deletion demo/app/main-page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<Barcode:BarcodeScannerView
class="scanner-round"
formats="QR_CODE, EAN_13, CODE_128"
beepOnScan="false"
beepOnScan="true"
reportDuplicates="true"
preferFrontCamera="false"
scanResult="{{ onScanResult }}" />
Expand Down
12 changes: 6 additions & 6 deletions demo/package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"dependencies": {
"nativescript-barcodescanner": "file:../src",
"@nativescript/theme": "~2.3.0",
"@nativescript/theme": "~2.5.0",
"nativescript-unit-test-runner": "~0.7.0",
"@nativescript/core": "~7.0.0"
"@nativescript/core": "~8.0.0"
},
"devDependencies": {
"@nativescript/android": "7.0.0",
"@nativescript/ios": "7.0.0",
"@nativescript/types": "^7.0.0",
"@nativescript/android": "8.0.0",
"@nativescript/ios": "8.0.0",
"@nativescript/types": "~8.0.0",
"@nativescript/webpack": "^3.0.4",
"jasmine-core": "^2.5.2",
"karma": "4.1.0",
"karma-jasmine": "2.0.1",
"karma-nativescript-launcher": "^0.4.0",
"karma-webpack": "3.0.5",
"tslint": "~5.11.0",
"typescript": "~3.9.7"
"typescript": "~4.0.7"
},
"scripts": {
"build.plugin": "cd ../src && npm run build"
Expand Down
4 changes: 2 additions & 2 deletions src/barcodescanner-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ export declare class BarcodeScanner {

hasCameraPermission(): Promise<boolean>;

requestCameraPermission(): Promise<boolean>;
requestCameraPermission(): Promise<void>;

stop(): Promise<any>;
stop(): Promise<void>;

scan(arg: ScanOptions): Promise<ScanResult>;
}
Expand Down
8 changes: 4 additions & 4 deletions src/barcodescanner.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ export class BarcodeScanner {
});
}

public requestCameraPermission(): Promise<boolean> {
return new Promise((resolve, reject) => {
public requestCameraPermission(): Promise<void> {
return new Promise<void>((resolve, reject) => {
try {
this.requestCameraPermissionInternal(resolve, reject);
} catch (ex) {
Expand All @@ -89,8 +89,8 @@ export class BarcodeScanner {
});
}

public stop(): Promise<any> {
return new Promise((resolve, reject) => {
public stop(): Promise<void> {
return new Promise<void>((resolve, reject) => {
try {
if (!this.broadcastManager) {
reject("You found a bug in the plugin, please report that calling stop() failed with this message.");
Expand Down
54 changes: 16 additions & 38 deletions src/barcodescanner.ios.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import {
BarcodeFormat,
BarcodeScannerView as BarcodeScannerBaseView,
ScanOptions,
ScanResult
} from "./barcodescanner-common";
import { Frame } from "@nativescript/core";
import { BarcodeFormat, BarcodeScannerView as BarcodeScannerBaseView, ScanOptions, ScanResult } from "./barcodescanner-common";

export class BarcodeScannerView extends BarcodeScannerBaseView {

Expand Down Expand Up @@ -179,8 +175,8 @@ export class BarcodeScanner {
});
}

public requestCameraPermission(): Promise<boolean> {
return new Promise((resolve, reject) => {
public requestCameraPermission(): Promise<void> {
return new Promise<void>((resolve, reject) => {
const cameraStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo);

switch (cameraStatus) {
Expand All @@ -204,8 +200,8 @@ export class BarcodeScanner {
});
}

public stop(): Promise<any> {
return new Promise((resolve, reject) => {
public stop(): Promise<void> {
return new Promise<void>((resolve, reject) => {
try {
this.close();
this._removeVolumeObserver();
Expand Down Expand Up @@ -320,11 +316,10 @@ export class BarcodeScanner {
}

private isPresentingModally(): boolean {
let frame = require("@nativescript/core/ui/frame");
let viewController: UIViewController;
let topMostFrame = frame.topmost();
const topMostFrame = Frame.topmost();

if (frame.topmost()) {
if (topMostFrame) {
viewController = topMostFrame.currentPage && topMostFrame.currentPage.ios;

if (viewController) {
Expand All @@ -349,9 +344,8 @@ export class BarcodeScanner {
}

private getViewControllerToPresentFrom(presentInRootViewController?: boolean): UIViewController {
let frame = require("@nativescript/core/ui/frame");
let viewController: UIViewController;
let topMostFrame = frame.topmost();
const topMostFrame = Frame.topmost();

if (topMostFrame && presentInRootViewController !== true) {
viewController = topMostFrame.currentPage && topMostFrame.currentPage.ios;
Expand Down Expand Up @@ -454,7 +448,6 @@ class QRCodeReaderDelegateImpl extends NSObject implements QRCodeReaderDelegate
private _reportDuplicates: boolean;
private _requestedFormats: string;
private _scannedArray: Array<string>;
private _player: AVAudioPlayer;
// initializing this value may prevent recognizing too quickly
private _lastScanResultTs: number = new Date().getTime();

Expand All @@ -464,13 +457,6 @@ class QRCodeReaderDelegateImpl extends NSObject implements QRCodeReaderDelegate
this._requestedFormats = requestedFormats;
this._callback = callback;
this._beepOnScan = beepOnScan;
if (this._beepOnScan) {
const barcodeBundlePath = NSBundle.bundleWithIdentifier("com.telerik.BarcodeScannerFramework").bundlePath;
this._player = new AVAudioPlayer({contentsOfURL: NSURL.fileURLWithPath(barcodeBundlePath + "/beep.caf")});
this._player.numberOfLoops = 1;
this._player.volume = 0.7; // this is not the actual volume, as that really depends on the device volume
this._player.prepareToPlay();
}
}

public readerDidCancel(reader: QRCodeReaderViewController): void {
Expand All @@ -480,7 +466,6 @@ class QRCodeReaderDelegateImpl extends NSObject implements QRCodeReaderDelegate

readerDidScanResultForType(reader: QRCodeReaderViewController, result: string, type: string): void {
let validResult: boolean = false;
console.log(1);

let barcodeFormat = getBarcodeFormat(type);
let value = result;
Expand All @@ -489,44 +474,37 @@ class QRCodeReaderDelegateImpl extends NSObject implements QRCodeReaderDelegate
barcodeFormat = "UPC_A";
value = value.substring(1);
}
console.log(2);

if (this._isContinuous) {
console.log(6);
if (!this._scannedArray) {
this._scannedArray = Array<string>();
}
console.log(7);
// don't report duplicates unless explicitly requested
let newResult: boolean = this._scannedArray.indexOf("[" + value + "][" + barcodeFormat + "]") === -1;
console.log(8);
if (newResult || this._reportDuplicates) {
let now: number = new Date().getTime();
// prevent flooding the callback
console.log(9);
if (now - this._lastScanResultTs < 1700) {
return;
}
this._lastScanResultTs = now;
validResult = true;
console.log(10);
this._scannedArray.push("[" + value + "][" + barcodeFormat + "]");
console.log(11);
this._callback(value, barcodeFormat);
console.log(12);
}
} else {
validResult = true;
console.log(3);
this._owner.get().close();
console.log(4);
console.log(this._callback);
this._callback(value, barcodeFormat);
console.log(5);
}

if (validResult && this._player) {
this._player.play();
if (validResult && this._beepOnScan) {
// tone
AudioServicesPlaySystemSound(1200);
// weak-boom (taptic feedback)
setTimeout(() => {
AudioServicesPlaySystemSound(1519);
});
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-barcodescanner",
"version": "4.0.1",
"version": "4.1.0",
"description": "Scan QR/barcodes with your NativeScript app.",
"main": "barcodescanner",
"typings": "index.d.ts",
Expand Down Expand Up @@ -67,10 +67,10 @@
},
"homepage": "https://github.com/eddyverbruggen/nativescript-barcodescanner",
"devDependencies": {
"@nativescript/core": "^7.0.0",
"@nativescript/types": "^7.0.0",
"@nativescript/webpack": "~3.0.0",
"typescript": "~3.9.7",
"@nativescript/core": "^8.0.0",
"@nativescript/types": "^8.0.0",
"@nativescript/webpack": "^3.0.0",
"typescript": "~4.0.7",
"prompt": "^1.0.0",
"rimraf": "^2.6.3",
"tslint": "^5.12.1",
Expand Down

0 comments on commit 34dff3b

Please sign in to comment.