@@ -21,39 +21,39 @@ export function onNavigatingTo(args: NavigatedData) {
21
21
22
22
page . bindingContext = new HomeViewModel ( ) ;
23
23
24
+ let preparing = true ;
24
25
let locationSubscription : Subscription ;
25
-
26
26
Application . on ( Application . resumeEvent , ( ) => {
27
- if ( ! _preparing ) {
27
+ if ( ! preparing ) {
28
28
printCurrentLocation ( ) . catch ( ( err ) => {
29
29
console . error ( `Could not print current location: ${ err } ` ) ;
30
- } ) ;
31
- printLocationUpdates ( )
30
+ } ) . then ( ( ) => printLocationUpdates ( )
32
31
. then ( ( subscription ) => ( locationSubscription = subscription ) )
33
32
. catch (
34
33
( err ) =>
35
- `An error occurred while getting location updates: ${ err } `
36
- ) ;
34
+ `An error occurred while getting location updates: ${ err } ` )
35
+ ) . then ( ( ) => listenToActivityChanges ( ) ) ;
37
36
}
38
- listenToActivityChanges ( ) ;
39
37
} ) ;
40
38
41
39
Application . on ( Application . suspendEvent , ( ) => {
42
- if ( locationSubscription ) {
43
- locationSubscription . unsubscribe ( ) ;
40
+ if ( ! preparing ) {
41
+ if ( locationSubscription ) {
42
+ locationSubscription . unsubscribe ( ) ;
43
+ }
44
+ stopListeningToChanges ( ) ;
44
45
}
45
- stopListeningToChanges ( ) ;
46
46
} ) ;
47
47
48
48
printCurrentLocation ( ) . catch ( ( err ) => {
49
49
console . error ( `Could not print current location: ${ err } ` ) ;
50
- } ) ;
51
- printLocationUpdates ( )
50
+ } ) . then ( ( ) => printLocationUpdates ( )
52
51
. then ( ( subscription ) => ( locationSubscription = subscription ) )
53
52
. catch (
54
53
( err ) => `An error occurred while getting location updates: ${ err } `
55
- ) ;
56
- listenToActivityChanges ( true ) ;
54
+ )
55
+ ) . then ( ( ) => listenToActivityChanges ( true ) )
56
+ . then ( ( ) => preparing = false ) ;
57
57
}
58
58
59
59
async function printCurrentLocation ( ) {
@@ -90,14 +90,16 @@ async function printLocationUpdates(): Promise<Subscription> {
90
90
} ) ;
91
91
}
92
92
93
- export function listenToActivityChanges ( addListener = false ) {
94
- activityRecognizers . forEach ( ( recognizerType ) => {
95
- listenToActivityChangesFor ( recognizerType , addListener ) . catch ( ( err ) => {
93
+ export async function listenToActivityChanges ( addListener = false ) {
94
+ for ( const recognizerType of activityRecognizers ) {
95
+ try {
96
+ await listenToActivityChangesFor ( recognizerType , addListener ) ;
97
+ } catch ( err ) {
96
98
console . error (
97
99
`An error occurred while listening to ${ recognizerType } res activity changes: ${ JSON . stringify ( err ) } `
98
100
) ;
99
- } ) ;
100
- } ) ;
101
+ }
102
+ }
101
103
}
102
104
103
105
function stopListeningToChanges ( ) {
0 commit comments