@@ -6,16 +6,7 @@ import {
66 DatePickerOptions , TimePickerOptions , PickerOptions
77} from "./datetimepicker.common" ;
88
9- interface DialogClickListener {
10- new ( datePicker : any , dateTime : Date , callback : Function ) : android . content . DialogInterface . OnClickListener ;
11- }
12-
13- interface DialogDismissListener {
14- new ( callback : Function ) : android . content . DialogInterface . OnDismissListener ;
15- }
16-
17- let DialogClickListener : DialogClickListener ;
18- let DialogDismissListener : DialogDismissListener ;
9+ let DialogListener : any ;
1910let AppCompatNamespace : any ;
2011declare let global : any ;
2112
@@ -30,13 +21,14 @@ function initializeAppCompatNamespace(): void {
3021 }
3122}
3223
33- function initializeDialogClickListener ( ) : void {
34- if ( DialogClickListener ) {
24+ function initializeDialogListener ( ) : void {
25+ if ( DialogListener ) {
3526 return ;
3627 }
3728
38- @Interfaces ( [ android . content . DialogInterface . OnClickListener ] )
39- class DialogClickListenerImpl extends java . lang . Object implements android . content . DialogInterface . OnClickListener {
29+ @Interfaces ( [ android . content . DialogInterface . OnClickListener , android . content . DialogInterface . OnDismissListener ] )
30+ class DialogListenerImpl extends java . lang . Object implements android . content . DialogInterface . OnClickListener , android . content . DialogInterface . OnDismissListener {
31+ private _isClicked = false ;
4032 constructor ( public nativePicker : any , public dateTime : Date , public callback : Function ) {
4133 super ( ) ;
4234 return global . __native ( this ) ;
@@ -46,6 +38,7 @@ function initializeDialogClickListener(): void {
4638 const callback = this . callback ;
4739 const dateTime = this . dateTime ;
4840 const nativePicker = this . nativePicker ;
41+ this . _isClicked = true ;
4942 switch ( which ) {
5043 case android . content . DialogInterface . BUTTON_POSITIVE : {
5144 if ( nativePicker instanceof android . widget . DatePicker ) {
@@ -63,30 +56,19 @@ function initializeDialogClickListener(): void {
6356 }
6457 callback ( null ) ;
6558 }
66- }
67-
68- DialogClickListener = DialogClickListenerImpl ;
69- }
70-
71- function initializeDialogDismissListener ( ) : void {
72- if ( DialogDismissListener ) {
73- return ;
74- }
75-
76- @Interfaces ( [ android . content . DialogInterface . OnDismissListener ] )
77- class DialogDismissListenerImpl extends java . lang . Object implements android . content . DialogInterface . OnDismissListener {
78- constructor ( public callback : Function ) {
79- super ( ) ;
80- return global . __native ( this ) ;
81- }
8259
8360 onDismiss ( dialog : android . content . DialogInterface ) {
61+ if ( this . _isClicked ) {
62+ // The Picker is dismissed due to a button click,
63+ // so the callback is already called.
64+ return ;
65+ }
8466 const callback = this . callback ;
8567 callback ( null ) ;
8668 }
8769 }
8870
89- DialogDismissListener = DialogDismissListenerImpl ;
71+ DialogListener = DialogListenerImpl ;
9072}
9173
9274export class DateTimePickerStyle extends DateTimePickerStyleBase {
@@ -167,8 +149,7 @@ export class DateTimePicker extends DateTimePickerBase {
167149 }
168150
169151 static _createNativeDialog ( nativePicker : android . view . View , options : PickerOptions , value : Date , callback : Function ) : android . app . AlertDialog . Builder {
170- initializeDialogClickListener ( ) ;
171- initializeDialogDismissListener ( ) ;
152+ initializeDialogListener ( ) ;
172153 initializeAppCompatNamespace ( ) ;
173154 DateTimePicker . _initializeTextResources ( options . context ) ;
174155 const context = options . context ;
@@ -179,16 +160,15 @@ export class DateTimePicker extends DateTimePickerBase {
179160 dateTime = ( nativePicker instanceof android . widget . DatePicker ) ? getDateToday ( ) : getDateNow ( ) ;
180161 }
181162 const alertDialog = new android . app . AlertDialog . Builder ( context ) ;
182- const dialogClickListener = new DialogClickListener ( nativePicker , dateTime , callback ) ;
183- const dialogDismissListener = new DialogDismissListener ( callback ) ;
163+ const dialogListener = new DialogListener ( nativePicker , dateTime , callback ) ;
184164 if ( options . title ) {
185165 alertDialog . setTitle ( options . title ) ;
186166 }
187- alertDialog . setOnDismissListener ( dialogDismissListener ) ;
167+ alertDialog . setOnDismissListener ( dialogListener ) ;
188168 const cancelButtonText = options . cancelButtonText ? options . cancelButtonText : this . _defaultCancelText ;
189169 const okButtonText = options . okButtonText ? options . okButtonText : this . _defaultOkText ;
190- alertDialog . setNegativeButton ( cancelButtonText , dialogClickListener ) ;
191- alertDialog . setPositiveButton ( okButtonText , dialogClickListener ) ;
170+ alertDialog . setNegativeButton ( cancelButtonText , dialogListener ) ;
171+ alertDialog . setPositiveButton ( okButtonText , dialogListener ) ;
192172 alertDialog . setView ( nativePicker ) ;
193173 return alertDialog ;
194174 }
0 commit comments