Skip to content

Commit cf3b269

Browse files
committed
connect to state in app component, add triggers fore foreground and background events
1 parent de320d1 commit cf3b269

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

package-lock.json

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"@angular/platform-browser-dynamic": "^19.0.0",
2727
"@angular/router": "^19.0.0",
2828
"@capacitor/android": "^7.2.0",
29-
"@capacitor/app": "7.0.1",
29+
"@capacitor/app": "^7.0.1",
3030
"@capacitor/core": "^7.2.0",
3131
"@capacitor/haptics": "^7.0.1",
3232
"@capacitor/ios": "^7.2.0",

src/app/app.component.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Component } from '@angular/core';
1+
import { Component, NgZone } from '@angular/core';
2+
import { App } from '@capacitor/app';
3+
import { SnapcastService } from './services/snapcast.service';
24

35
@Component({
46
selector: 'app-root',
@@ -8,7 +10,30 @@ import { Component } from '@angular/core';
810
})
911
export class AppComponent {
1012
constructor(
13+
private ngZone: NgZone,
14+
private snapcastService: SnapcastService
1115
) {
16+
this.initializeApp();
17+
}
18+
19+
initializeApp() {
20+
App.addListener('appStateChange', (state) => {
21+
// NOTE: This is my backup plan if state is still not refreshed
22+
this.ngZone.run(() => {
23+
if (state.isActive) {
24+
// App has come to the foreground
25+
console.log('App is in the foreground');
26+
// TODO: Refresh or establish connections to snapcast server
27+
this.snapcastService.connect();
1228

29+
// Add your logic here to refresh data, update UI, etc.
30+
} else {
31+
// App has gone to the background
32+
console.log('App is in the background');
33+
// TODO: Destroy connections to snapcast server. But will see if this is a good idea. Maybe good to safe some pi resources.
34+
this.snapcastService.disconnect();
35+
}
36+
});
37+
});
1338
}
1439
}

src/app/tabs/tabs.page.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ export class TabsPage implements OnInit, AfterViewInit {
1717
constructor(private snapcastService: SnapcastService) {}
1818

1919
ngOnInit(): void {
20-
this.snapcastService.connect();
2120

2221
}
2322

23+
ionViewDidEnter() {
24+
// this.snapcastService.connect();
25+
}
26+
2427
ngAfterViewInit() {
2528
}
2629

0 commit comments

Comments
 (0)