ionic-team / ng-cordova

OBSOLETE: Please move to Ionic Native https://github.com/ionic-team/ionic-native
https://github.com/ionic-team/ionic-native
MIT License
3.48k stars 1.06k forks source link

$cordovaDatePicker.show() callback is never called and datepicker does not appear on the device #1461

Closed khashashin closed 5 years ago

khashashin commented 5 years ago

Device: Google Pixel 2XL Ionic: 1.7.15 cordova: 7.1.0 plugin: cordova-plugin-datepicker 0.9.3 "DatePicker"

In my ionic v1 app im trying to handle choose of date through $cordovaDatePicker. I have very minimal options like in the following code:

$scope.validFrom = function () {
    var options = {
        mode: 'date',
        allowOldDates: false
    };

    $cordovaDatePicker.show(options).then(function (date) {
        $scope.ticketData.validFrom = DateHelper.formatDateDDMMYYYY(date);
    });
};

And im calling this validFrom function through $ionicPopup template like this:

$scope.ticketClicked = function (ticket) {
    var save = ;
    $ionicPopup.show({
        template: `
            <button ng-click="validFrom()">{{ticketData.validFrom}}</button>`,
        title: ticket.name,
        subTitle: 'Valid From',
        scope: $scope,
        buttons: [
            {
                text: 'Cancel'
            },
            {
                text: `Save`,
                onTap: function (event) {
                    return $scope.ticketData.validFrom;
                }
            }
        ]
    }).then(function (response) {
        if (response) {
            ticket.valid_from_date = response;
            $state.go('tab.customer_ticket_payment_methods');
        }
        else {
            return;
        }
    });
};

When I click on <button ng-click="validFrom()">{{ticketData.validFrom}}</button> nothing is shown. But in logcat I see following error:

2019-02-19 12:18:59.140 29533-29712/com.myapp.app E/PluginManager: Uncaught exception from plugin
    java.lang.NumberFormatException: For input string: ""
        at java.lang.Integer.parseInt(Integer.java:627)
        at java.lang.Integer.parseInt(Integer.java:650)
        at com.plugin.datepicker.DatePickerPlugin$JsonDate.fromJson(DatePickerPlugin.java:387)
        at com.plugin.datepicker.DatePickerPlugin.show(DatePickerPlugin.java:70)
        at com.plugin.datepicker.DatePickerPlugin.execute(DatePickerPlugin.java:60)
        at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:98)
        at org.apache.cordova.PluginManager.exec(PluginManager.java:132)
        at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59)
        at org.apache.cordova.engine.SystemExposedJsApi.exec(SystemExposedJsApi.java:41)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:326)
        at android.os.Looper.loop(Looper.java:160)
        at android.os.HandlerThread.run(HandlerThread.java:65)

Im not sure if it relevant to my project or my device, so any help would be very helpful. Thanks!

khashashin commented 5 years ago

Adding a date object solved the problem. The documentation states that date value in options has new Date() as default value, but somehow in my case I had to define it. So my final code looks like:

$scope.validFrom = function () {
    var options = {
        date: new Date(),
        mode: 'date',  // or 'time'
        allowOldDates: false,
    };

    $cordovaDatePicker.show(options).then(function (date) {
        $scope.ticketData.validFrom = DateHelper.formatDateDDMMYYYY(date);
    });
};