@@ -21,6 +21,8 @@ function useAndroidX() {
21
21
return global . androidx && global . androidx . core . view ;
22
22
}
23
23
24
+ const HIDE_RETRY_MS = 100 ;
25
+
24
26
export class LoadingIndicator {
25
27
private _popOver : android . widget . PopupWindow ;
26
28
private _currentProgressColor : Color ;
@@ -30,6 +32,7 @@ export class LoadingIndicator {
30
32
private _detailsId : number ;
31
33
private _customViewId : number ;
32
34
private _loadersInstances : android . widget . PopupWindow [ ] ;
35
+ private _isCreatingPopOver : boolean ;
33
36
34
37
constructor ( ) {
35
38
this . _defaultProgressColor = new Color ( '#007DD6' ) ;
@@ -38,6 +41,7 @@ export class LoadingIndicator {
38
41
this . _detailsId = android . view . View . generateViewId ( ) ;
39
42
this . _customViewId = android . view . View . generateViewId ( ) ;
40
43
this . _loadersInstances = [ ] ;
44
+ this . _isCreatingPopOver = false ;
41
45
}
42
46
43
47
show ( options ?: OptionsCommon ) {
@@ -47,18 +51,36 @@ export class LoadingIndicator {
47
51
options . android = options . android || { } ;
48
52
options . userInteractionEnabled =
49
53
options . userInteractionEnabled !== undefined || true ;
54
+
50
55
if ( ! this . _popOver ) {
51
- setTimeout ( ( ) => {
56
+ this . _isCreatingPopOver = true ;
57
+ new Promise ( ( resolve ) => {
52
58
this . _createPopOver ( context , options ) ;
53
59
this . _loadersInstances . push ( this . _popOver ) ;
60
+ resolve ( ) ;
61
+ } ) . then ( ( ) => {
62
+ this . _isCreatingPopOver = false ;
63
+ } ) . catch ( ( error ) => {
64
+ // Ensure this is left in a clean state.
65
+ this . _isCreatingPopOver = false ;
66
+ const message = error && error . message ? `: ${ error . message } ` : '' ;
67
+ console . error ( `Error creating Loading Indicator Pop Over${ message } ` ) ;
54
68
} ) ;
55
- } else {
56
- this . _updatePopOver ( context , options ) ;
69
+ return ;
57
70
}
71
+ this . _updatePopOver ( context , options ) ;
72
+ }
73
+ }
74
+
75
+ hide ( targetView ?: any , attemptTimeout : number = 1000 ) : void {
76
+ if ( this . _isCreatingPopOver ) {
77
+ this . _waitForCreatePopOver ( attemptTimeout ) ;
78
+ return ;
58
79
}
80
+ this . _tryHide ( ) ;
59
81
}
60
82
61
- hide ( ) {
83
+ private _tryHide ( ) : void {
62
84
try {
63
85
for ( let i = 0 ; i < this . _loadersInstances . length ; i ++ ) {
64
86
const loader = this . _loadersInstances [ i ] ;
@@ -77,6 +99,23 @@ export class LoadingIndicator {
77
99
}
78
100
}
79
101
102
+ private _waitForCreatePopOver ( attemptTimeout : number ) {
103
+ const startTime = Date . now ( ) ;
104
+
105
+ const awaitCreation = async ( ) => {
106
+ if ( ! this . _isCreatingPopOver ) {
107
+ return this . _tryHide ( ) ;
108
+ }
109
+ if ( Date . now ( ) > startTime + attemptTimeout ) {
110
+ console . warn ( 'Hide attempt timeout exceeded' ) ;
111
+ return ;
112
+ }
113
+ await new Promise ( ( resolve ) => setTimeout ( resolve , HIDE_RETRY_MS ) ) ;
114
+ return awaitCreation ( ) ;
115
+ } ;
116
+ return awaitCreation ( ) ;
117
+ }
118
+
80
119
private _isShowing ( loader : android . widget . PopupWindow ) {
81
120
return loader . isShowing ( ) ;
82
121
}
0 commit comments