@@ -19,7 +19,7 @@ import {
19
19
} from '@nativescript/core' ;
20
20
import { ad } from '@nativescript/core/utils' ;
21
21
import { LoginOptions , MDCAlertControlerOptions , PromptOptions } from './dialogs' ;
22
- import { isDialogOptions } from './dialogs-common' ;
22
+ import { isDialogOptions , showingDialogs } from './dialogs-common' ;
23
23
24
24
export { capitalizationType , inputType } ;
25
25
@@ -152,6 +152,7 @@ function showDialog(dlg: androidx.appcompat.app.AlertDialog, options: DialogOpti
152
152
}
153
153
} ) ;
154
154
}
155
+ showingDialogs . push ( dlg ) ;
155
156
dlg . show ( ) ;
156
157
return dlg ;
157
158
}
@@ -164,11 +165,14 @@ function prepareAndCreateAlertDialog(
164
165
) {
165
166
// onDismiss will always be called. Prevent calling callback multiple times
166
167
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 ?) {
168
169
if ( options && options . shouldResolveOnAction && ! options . shouldResolveOnAction ( validationArgs ? validationArgs ( result ) : result ) ) {
169
170
return ;
170
171
}
171
172
if ( onDoneCalled ) {
173
+ if ( toBeCalledBeforeCallback ) {
174
+ toBeCalledBeforeCallback ( ) ;
175
+ }
172
176
return ;
173
177
}
174
178
//ensure we hide any keyboard
@@ -177,7 +181,7 @@ function prepareAndCreateAlertDialog(
177
181
Utils . android . dismissSoftInput ( options . view . nativeView ) ;
178
182
} else {
179
183
const activity = ( Application . android . foregroundActivity || Application . android . startActivity ) as globalAndroid . app . Activity ;
180
- const context = ad . getApplicationContext ( ) ;
184
+ const context = Utils . android . getApplicationContext ( ) ;
181
185
const view = activity != null ? activity . getCurrentFocus ( ) : null ;
182
186
if ( view ) {
183
187
const imm = context . getSystemService ( android . content . Context . INPUT_METHOD_SERVICE ) as android . view . inputmethod . InputMethodManager ;
@@ -197,7 +201,11 @@ function prepareAndCreateAlertDialog(
197
201
}
198
202
builder . setOnDismissListener (
199
203
new DialogInterface . OnDismissListener ( {
200
- onDismiss ( ) {
204
+ onDismiss ( dialog ) {
205
+ const index = showingDialogs . indexOf ( dialog ) ;
206
+ if ( index !== - 1 ) {
207
+ showingDialogs . splice ( index , 1 ) ;
208
+ }
201
209
// ensure callback is called after destroying the custom view
202
210
onDone ( false , undefined , ( ) => {
203
211
if ( ( builder as any ) . _currentModalCustomView ) {
@@ -213,17 +221,18 @@ function prepareAndCreateAlertDialog(
213
221
} )
214
222
) ;
215
223
const dlg = builder . create ( ) ;
224
+ dlg [ 'onDone' ] = onDone ;
216
225
if ( ! options ) {
217
226
return dlg ;
218
227
}
219
228
if ( ( builder as any ) . _currentModalCustomView ) {
220
229
const view = ( builder as any ) . _currentModalCustomView as View ;
221
230
const context = options . context || { } ;
222
231
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
+ // }
227
236
} ;
228
237
view . bindingContext = fromObject ( context ) ;
229
238
}
@@ -340,11 +349,15 @@ export class AlertDialog {
340
349
showDialog ( this . dialog , this . options ) ;
341
350
}
342
351
}
343
- async hide ( ) {
352
+ async hide ( result ) {
344
353
if ( this . dialog ) {
345
354
return new Promise < void > ( ( resolve ) => {
346
355
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
+ }
348
361
this . dialog = null ;
349
362
} ) ;
350
363
}
0 commit comments