@@ -19,7 +19,7 @@ import {
1919} from '@nativescript/core' ;
2020import { ad } from '@nativescript/core/utils' ;
2121import { LoginOptions , MDCAlertControlerOptions , PromptOptions } from './dialogs' ;
22- import { isDialogOptions } from './dialogs-common' ;
22+ import { isDialogOptions , showingDialogs } from './dialogs-common' ;
2323
2424export { capitalizationType , inputType } ;
2525
@@ -152,6 +152,7 @@ function showDialog(dlg: androidx.appcompat.app.AlertDialog, options: DialogOpti
152152 }
153153 } ) ;
154154 }
155+ showingDialogs . push ( dlg ) ;
155156 dlg . show ( ) ;
156157 return dlg ;
157158}
@@ -164,11 +165,14 @@ function prepareAndCreateAlertDialog(
164165) {
165166 // onDismiss will always be called. Prevent calling callback multiple times
166167 let onDoneCalled = false ;
167- const onDone = function ( result : boolean , dialog ?: android . content . DialogInterface , toBeCalledBeforeCallback ?) {
168+ const onDone = function ( result : any , dialog ?: android . content . DialogInterface , toBeCalledBeforeCallback ?) {
168169 if ( options && options . shouldResolveOnAction && ! options . shouldResolveOnAction ( validationArgs ? validationArgs ( result ) : result ) ) {
169170 return ;
170171 }
171172 if ( onDoneCalled ) {
173+ if ( toBeCalledBeforeCallback ) {
174+ toBeCalledBeforeCallback ( ) ;
175+ }
172176 return ;
173177 }
174178 //ensure we hide any keyboard
@@ -177,7 +181,7 @@ function prepareAndCreateAlertDialog(
177181 Utils . android . dismissSoftInput ( options . view . nativeView ) ;
178182 } else {
179183 const activity = ( Application . android . foregroundActivity || Application . android . startActivity ) as globalAndroid . app . Activity ;
180- const context = ad . getApplicationContext ( ) ;
184+ const context = Utils . android . getApplicationContext ( ) ;
181185 const view = activity != null ? activity . getCurrentFocus ( ) : null ;
182186 if ( view ) {
183187 const imm = context . getSystemService ( android . content . Context . INPUT_METHOD_SERVICE ) as android . view . inputmethod . InputMethodManager ;
@@ -197,7 +201,11 @@ function prepareAndCreateAlertDialog(
197201 }
198202 builder . setOnDismissListener (
199203 new DialogInterface . OnDismissListener ( {
200- onDismiss ( ) {
204+ onDismiss ( dialog ) {
205+ const index = showingDialogs . indexOf ( dialog ) ;
206+ if ( index !== - 1 ) {
207+ showingDialogs . splice ( index , 1 ) ;
208+ }
201209 // ensure callback is called after destroying the custom view
202210 onDone ( false , undefined , ( ) => {
203211 if ( ( builder as any ) . _currentModalCustomView ) {
@@ -213,17 +221,18 @@ function prepareAndCreateAlertDialog(
213221 } )
214222 ) ;
215223 const dlg = builder . create ( ) ;
224+ dlg [ 'onDone' ] = onDone ;
216225 if ( ! options ) {
217226 return dlg ;
218227 }
219228 if ( ( builder as any ) . _currentModalCustomView ) {
220229 const view = ( builder as any ) . _currentModalCustomView as View ;
221230 const context = options . context || { } ;
222231 context . closeCallback = function ( ...originalArgs ) {
223- dlg . dismiss ( ) ;
224- if ( callback ) {
225- callback . apply ( this , originalArgs ) ;
226- }
232+ onDone ( originalArgs ) ;
233+ // if (callback) {
234+ // callback.apply(this, originalArgs);
235+ // }
227236 } ;
228237 view . bindingContext = fromObject ( context ) ;
229238 }
@@ -340,11 +349,15 @@ export class AlertDialog {
340349 showDialog ( this . dialog , this . options ) ;
341350 }
342351 }
343- async hide ( ) {
352+ async hide ( result ) {
344353 if ( this . dialog ) {
345354 return new Promise < void > ( ( resolve ) => {
346355 this . onCloseListeners . push ( resolve ) ;
347- this . dialog . cancel ( ) ;
356+ if ( this . dialog [ 'onDone' ] ) {
357+ this . dialog [ 'onDone' ] ( result ) ;
358+ } else {
359+ this . dialog . cancel ( ) ;
360+ }
348361 this . dialog = null ;
349362 } ) ;
350363 }
0 commit comments