tidev / ti.barcode

QR and Barcode Scanner
Other
66 stars 90 forks source link

Issue with use ti.barcode in two separated windows #65

Open jimmyegc opened 6 years ago

jimmyegc commented 6 years ago

Hi,

I just want to tell you something about the module, I´m working with an application in order to scan a QR code in 2 separated profiles, the second Barcode.addEventListener('success', call at the first option even when I tell to the application "I´m in window 1" so you need to fire this function and not of the Window #2.

Topener commented 6 years ago

You'll either need to remove the scanner from your second window when not using it, or you'll need to keep track of this yourself. If you initiate 2 scanners, both will work.

Correct UX flow would be to close the one you don't need.

sglok commented 6 years ago

How should i remove the the scanner? I tried to null the scanner instance like below:

var Barcode = require('ti.barcode'); ......

Barcode.addEventListener('cancel', function (e) { ..... Barcode = null;

});

However, the instance seemed still remain.

Even i tried to call

var success_callback = function(e){ .... });

var cancel_callback = function(e){ ....
Barcode.removeEventListener('cancel', cancel_callback); Barcode.removeEventListener('success', success_callback);

Barcode = null; }

Barcode.addEventListener('cancel', cancel_callback); Barcode.addEventListener('success', success_callback);

The .removeEventListener seemed not working. The .addEventListener will keep adding event and cause double, triple call for event function

The problem only exist in android platform, iOS is working good.

Topener commented 6 years ago

@sglok can you give me the code (as minimal as possible) on which you have this issue? Then I'll try to fix it :)

sglok commented 6 years ago

@Topener

//common_util.js

function createTiBarCodeQRCodeScanner(options){ options = options || {};

var scanned_callback    = options.scanned_callback || function(){};
var closed_callback     = options.closed_callback || function(){};
var camera_facing       = options.camera_facing;

var useFrontCamera      = camera_facing==Alloy.CFG.Settings.SCANNER_CAMERA_FONT;

var Barcode                     = require('ti.barcode');
Barcode.allowRotation       = true;
Barcode.displayedMessage    = ' ';
Barcode.allowMenu           = false;
Barcode.allowInstructions   = false;
Barcode.useLED              = false;
Barcode.useFrontCamera      = useFrontCamera;

Ti.API.info('Barcode='+Barcode);

var overlay = Ti.UI.createView({
    backgroundColor: 'transparent',
    top: 0, right: 0, bottom: 0, left: 0
});

var overlay = Ti.UI.createView({
    backgroundColor: 'transparent',
    top: 0, right: 0, bottom: 0, left: 0
});

var close_button = Ti.UI.createButton({
    title           : L('close'), 
    textAlign       : 'center',
    color           : '#000', 
    backgroundColor : '#fff', style: 0,
    font            : { fontWeight: 'bold', fontSize: 16 },
    borderColor     : '#000', 
    borderRadius    : 10, 
    borderWidth     : 1,
    opacity         : 0.7,
    width           : 200, 
    height          : 40,
    bottom          : 20,
});

close_button.addEventListener('click', function () {
    Barcode.cancel();
});

overlay.add(close_button);

var success_scan = function(e){
    var scanned_code = e.result;
    Ti.API.info('Success called with barcode: ' + scanned_code);

    if(scanned_callback){
            //scanned_callback(scanned_code);
    }
    alert('scanned_code='+scanned_code);

    Barcode.cancel();
};

var cancel_scan = function(e){
    Ti.API.info('Cancel received');

    if(closed_callback){
        closed_callback();
    }

    Barcode.removeEventListener('success', success_scan);
    Barcode.removeEventListener('cancel', cancel_scan);

    Barcode = null;

};

Barcode.addEventListener('cancel', cancel_scan);
Barcode.addEventListener('success', success_scan);

Barcode.capture({
    animate         : true,
    overlay         : overlay,
    showCancel      : false,
    showRectangle   : true,
    keepOpen            : false
    /*,
    acceptedFormats: [
        Barcode.FORMAT_QR_CODE
    ]*/
});

exports.createQRCodeScanner = createTiBarCodeQRCodeScanner;

sglok commented 6 years ago

I think the instance of BarCode is remain in runtime. So what i did to overcome this issue in android, where:

var success_callback = function(e){ .... Barcode.removeEventListener('cancel', cancel_callback); Barcode.removeEventListener('success', success_callback); });

var cancel_callback = function(e){ .... Barcode.removeEventListener('cancel', cancel_callback); Barcode.removeEventListener('success', success_callback); }

Barcode.addEventListener('cancel', cancel_callback); Barcode.addEventListener('success', success_callback);

Even the scanner instance is remain in runtime. The above code will remove the success and cancel event function in anyhow.