I'm trying to figure out why the alert doesn't fire. The console.log statements work and the ObservableArray updates and that is shown on the ListView screen. I'm running NativeScript 6.3.3 from cli with the nativeScript-barcodescanner plugin on Android. What am I messing up?
barcode-view-model.ts:
/*
15 Jan 2020
Scan-View-Model is based upon demos found in nativescript-barcode scanner by
Eddie Verbruggen found at https://github.com/EddyVerbruggen/nativescript-barcodescanner
*/
import { Page } from "tns-core-modules/ui/page";
import { Observable, fromObject, EventData } from "@nativescript/core/data/observable";
import { ObservableArray, ChangedData } from "@nativescript/core/data/observable-array";
import { ItemEventData, ListView } from "@nativescript/core/ui/list-view";
import { alert } from "@nativescript/core/ui/dialogs";
import { BarcodeScanner } from "nativescript-barcodescanner";
class Item {
barcode: string;
format: string;
id: number;
constructor(barcode: string, format: string) {
this.barcode = barcode;
this.format = format;
this.id = new Date().getTime();
}
}
export class ScanViewModel extends Observable {
barcodeVersion = "Testing (r01 v20200115.2) " + new Date().getTime();
items: ObservableArray<Item>
newItem: string = '';
public message: string;
private barcodeScanner: BarcodeScanner;
constructor() {
super();
this.items = new ObservableArray<Item>([
new Item("1234A", "-Test-"),
new Item("0987Z", "-Test-")
]);
this.barcodeScanner = new BarcodeScanner();
}
addItem() {
this.items.push(new Item(this.newItem, "*Manual*"));
this.set('newItem','');
}
public scanBarcode() {
this.barcodeScanner.scan({
formats: "QR_CODE, EAN_13",
cancelLabel: "EXIT. Also, try the volume buttons!", // iOS only, default 'Close'
cancelLabelBackgroundColor: "#333333", // iOS only, default '#000000' (black)
message: "Use the volume buttons for extra light", // Android only, default is 'Place a barcode inside the viewfinder rectangle to scan it.'
preferFrontCamera: false, // Android only, default false
showFlipCameraButton: true, // default false
showTorchButton: true, // iOS only, default false
torchOn: false, // launch with the flashlight on (default false)
resultDisplayDuration: 500, // Android only, default 1500 (ms), set to 0 to disable echoing the scanned text
orientation: 'portrait', // Android only, default undefined (sensor-driven orientation), other options: portrait|landscape
beepOnScan: true, // Play or Suppress beep on scan (default true)
openSettingsIfPermissionWasPreviouslyDenied: true, // On iOS you can send the user to the settings app if access was previously denied
closeCallback: () => {
console.log("Scanner closed @ " + new Date().getTime());
}
}).then((result) => {
// Note that this Promise is never invoked when a 'continuousScanCallback' function is provided
console.log("Text: " + result.text + " Format: " + result.format);
this.items.push(new Item(result.text, result.format));
console.log(this.items);
alert({
title: "Scan result",
message: "Format: " + result.format + ",\nValue: " + result.text,
okButtonText: "OK"
});
}, (errorMessage) => {
console.log("No scan. " + errorMessage);
}
);
}
}
Figured this out after adding an alert to the addItem function to verify alert was working. Then I used Chrome debug to step thru the promise. I noticed the camera window appeared to stay open and the alert would reject. Went back to the nativescript-barcodescanner demo were the alert worked and found there was a setTimeout wrapped around the alert. Added that and things are working now.