Skip to content

Commit 9b1e958

Browse files
authored
Merge pull request #18 from byrdsandbytes/feature/sc-2037-handle-mobile-page-lifcycles
Feature/sc 2037 handle mobile page lifcycles
2 parents e65dfad + cf3b269 commit 9b1e958

File tree

5 files changed

+45
-32
lines changed

5 files changed

+45
-32
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/services/snapcast.service.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,9 @@ export class SnapcastService implements OnDestroy {
295295
);
296296
}
297297

298-
// --- Simplified Action Methods ---
298+
// --- "Simplified" Action Methods ---
299+
300+
// CLIENT ACTIONS
299301

300302
public setClientVolumePercent(clientId: string, percent: number): Observable<void> {
301303
if (percent < 0 || percent > 100) return throwError(() => new Error('Volume percentage must be between 0 and 100.'));
@@ -321,27 +323,17 @@ export class SnapcastService implements OnDestroy {
321323
);
322324
}
323325

324-
public setClientMute(clientId: string, mute: boolean): Observable<void> {
325-
return this.state$.pipe(
326-
take(1),
327-
switchMap(currentState => {
328-
const client = currentState?.server.groups.flatMap(g => g.clients).find(c => c.id === clientId);
329-
if (!client) return throwError(() => new Error(`Client ${clientId} not found.`));
330-
331-
const volumePayload: Volume = {
332-
percent: client.config.volume.percent, // Use current volume percent
333-
muted: mute,
334-
};
335-
return this.rpc('Client.SetVolume', { id: clientId, volume: volumePayload });
336-
}),
326+
setClientName(clientId: string, name: string): Observable<void> {
327+
return this.rpc('Client.SetName', { id: clientId, name }).pipe(
337328
map((): void => void 0),
338329
catchError(err => {
339-
console.error(`SnapcastService: Failed to set mute for client ${clientId}`, err);
330+
console.error(`SnapcastService: Failed to set name for client ${clientId}`, err);
340331
return throwError(() => err);
341332
})
342333
);
343334
}
344335

336+
345337
setGroupName(groupId: string, name: string): Observable<void> {
346338
return this.rpc('Group.SetName', { id: groupId, name }).pipe(
347339
map((): void => void 0),
@@ -370,18 +362,10 @@ export class SnapcastService implements OnDestroy {
370362

371363
}
372364

373-
setClientName(clientId: string, name: string): Observable<void> {
374-
return this.rpc('Client.SetName', { id: clientId, name }).pipe(
375-
map((): void => void 0),
376-
catchError(err => {
377-
console.error(`SnapcastService: Failed to set name for client ${clientId}`, err);
378-
return throwError(() => err);
379-
})
380-
);
381-
}
365+
366+
367+
382368

383-
// TODO ... Implement other action methods like.
384-
// They just need to call `this.rpc` with the correct parameters.
385369

386370
// --- Data Access Helpers ---
387371
public getClient(clientId: string): Observable<Client | undefined> {

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)