syncthing/next-gen-gui/src/app/services/system-config.service.ts

60 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-03-11 03:51:50 +01:00
import { Injectable } from '@angular/core';
2020-04-09 19:02:34 +02:00
import { HttpClient } from '@angular/common/http';
2020-03-11 03:51:50 +01:00
2020-04-09 19:02:34 +02:00
import { Observable, ReplaySubject } from 'rxjs';
import { map } from 'rxjs/operators';
2020-03-11 03:51:50 +01:00
2020-03-25 14:41:22 +01:00
import Folder from '../folder';
import Device from '../device';
2020-03-25 14:41:22 +01:00
import { environment } from '../../environments/environment'
import { apiURL } from '../api-utils'
import { ProgressService } from './progress.service';
2020-03-11 03:51:50 +01:00
@Injectable({
providedIn: 'root'
})
export class SystemConfigService {
private folders: Folder[];
private devices: Device[];
2020-04-09 19:02:34 +02:00
private foldersSubject: ReplaySubject<Folder[]> = new ReplaySubject(1);
private devicesSubject: ReplaySubject<Device[]> = new ReplaySubject(1);
2020-03-11 03:51:50 +01:00
2020-03-15 22:00:56 +01:00
private systemConfigUrl = environment.production ? apiURL + 'rest/system/config' : 'api/config';
private guiConfigUrl = environment.production ? apiURL + 'rest/config/gui' : 'api/config/gui';
constructor(
private http: HttpClient,
private progressService: ProgressService,
) { }
getSystemConfig(): Observable<any> {
return this.http
.get(this.systemConfigUrl)
2020-03-18 03:36:42 +01:00
.pipe(
map(res => {
this.folders = res['folders'];
this.devices = res['devices'];
// Set the total for the progress service
this.progressService.total = this.folders.length + this.devices.length;
2020-03-18 03:36:42 +01:00
this.foldersSubject.next(this.folders);
this.devicesSubject.next(this.devices);
2020-03-18 03:36:42 +01:00
return res;
})
);
}
2020-03-11 03:51:50 +01:00
getFolders(): Observable<Folder[]> {
2020-04-09 19:02:34 +02:00
return this.foldersSubject.asObservable();
2020-03-11 03:51:50 +01:00
}
getDevices(): Observable<Device[]> {
2020-04-09 19:02:34 +02:00
return this.devicesSubject.asObservable();
}
setGUITheme(theme: String): Observable<any> {
return this.http.patch(this.guiConfigUrl, { theme: theme })
}
}