|
| 1 | +import { Component, OnInit } from '@angular/core'; |
| 2 | +import { ActivatedRoute } from '@angular/router'; |
| 3 | +import { Observable } from 'rxjs'; |
| 4 | +import { Client, SnapCastServerStatusResponse } from 'src/app/model/snapcast.model'; |
| 5 | +import { SnapcastService } from 'src/app/services/snapcast.service'; |
| 6 | + |
| 7 | +@Component({ |
| 8 | + selector: 'app-client-details', |
| 9 | + templateUrl: './client-details.page.html', |
| 10 | + styleUrls: ['./client-details.page.scss'], |
| 11 | + standalone: false |
| 12 | +}) |
| 13 | +export class ClientDetailsPage implements OnInit { |
| 14 | + |
| 15 | + |
| 16 | + id?: string; |
| 17 | + |
| 18 | + serverState?: Observable<SnapCastServerStatusResponse>; |
| 19 | + client?: Client; |
| 20 | + |
| 21 | + |
| 22 | + constructor( |
| 23 | + private avtivateRoute: ActivatedRoute, |
| 24 | + private snapcastService: SnapcastService |
| 25 | + ) { } |
| 26 | + |
| 27 | + async ngOnInit() { |
| 28 | + this.serverState = this.snapcastService.state$; |
| 29 | + this.id = this.avtivateRoute.snapshot.paramMap.get('id') || undefined; |
| 30 | + if (!this.id) { |
| 31 | + console.error('ClientDetailsPage: No ID found in route parameters'); |
| 32 | + return; |
| 33 | + } |
| 34 | + console.log('ClientDetailsPage: ID from route parameters:', this.id); |
| 35 | + this.subscribeToClient(); |
| 36 | + } |
| 37 | + |
| 38 | + subscribeToClient() { |
| 39 | + if (!this.id) { |
| 40 | + console.error('ClientDetailsPage: No ID available to subscribe to client'); |
| 41 | + return; |
| 42 | + } |
| 43 | + this.serverState.subscribe((state) => { |
| 44 | + this.client = state.server.groups.flatMap(group => group.clients).find(client => client.id === this.id); |
| 45 | + if (!this.client) { |
| 46 | + console.error(`ClientDetailsPage: Client with ID ${this.id} not found in server state`); |
| 47 | + } else { |
| 48 | + console.log('ClientDetailsPage: Found client:', this.client); |
| 49 | + } |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + setClientName() { |
| 54 | + if (!this.client || !this.client.id) { |
| 55 | + console.error('ClientDetailsPage: No client or client ID available to set name'); |
| 56 | + return; |
| 57 | + } |
| 58 | + this.snapcastService.setClientName(this.client.id, this.client.config.name).subscribe({ |
| 59 | + next: () => { |
| 60 | + console.log(`ClientDetailsPage: Successfully set name for client ${this.client.id} to ${name}`); |
| 61 | + }, |
| 62 | + error: (err) => { |
| 63 | + console.error(`ClientDetailsPage: Failed to set name for client ${this.client.id}`, err); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +} |
0 commit comments