import { Injectable , OnDestroy } from '@angular/core' ; import { UserSettingsDBService } from '../../management/user-settings/services/user-settings-db.service' ; import { TableState , TableStatePayload } from './table.types' ; import { Observable , ReplaySubject } from 'rxjs' ; import { UserSetting } from '../../management/user-settings/models/user-setting' ; import { map } from 'rxjs/operators' ; import { convertResourceURL } from '../../management/user-settings/services/user-settings.service' ; @ Injectable ( { providedIn : 'root' } ) export class TableService < C = TableState > implements OnDestroy { private private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; constructor ( private indexDB ) { } param url param id public getTableStatePayload ( url , id ) { return this . indexDB . getSetting ( url ) . pipe ( map ( ( setting ) => { return setting . payloads . find ( payload => payload . id === id ) ; } ) ) ; } param url param id public getTableStates ( url , id ) { return this . indexDB . getSetting ( url ) . pipe ( map ( ( setting ) => { return setting . payloads . find ( payload => payload . id === id ) . tableStates ; } ) ) ; } param url param id public getTableState ( url , id ) { return this . indexDB . getSetting ( url ) . pipe ( map ( ( setting ) => { return setting . payloads . find ( payload => payload . id === id ) . currentState ; } ) ) ; } param url param id param state public addTableState ( url , id , state ) { const resource = convertResourceURL ( url ) ; this . indexDB . getSetting ( resource ) . subscribe ( ( setting ) => { setting . payloads . find ( payload => payload . id === id ) . tableStates . push ( state ) ; this . indexDB . updateSetting ( setting ) ; } ) ; } param url param id param state public setCurrentTableState ( url , id , state ) { const resource = convertResourceURL ( url ) ; this . indexDB . getSetting ( resource ) . subscribe ( ( setting ) => { setting . payloads . find ( payload => payload . id === id ) . currentState = state ; this . indexDB . updateSetting ( setting ) ; } ) ; } public updateTableState ( url , id , state , update ) { const resource = convertResourceURL ( url ) ; this . indexDB . getSetting ( resource ) . subscribe ( ( setting ) => { const tableStates = setting . payloads . find ( payload => payload . id === id ) . tableStates ; const stateIDX = tableStates . findIndex ( s => s . name === state . name ) ; setting . payloads . find ( payload => payload . id === id ) . tableStates [ stateIDX ] = update ; this . indexDB . updateSetting ( setting ) ; } ) ; } param url param id param state public removeTableState ( url , id , state ) { const resource = convertResourceURL ( url ) ; this . indexDB . getSetting ( resource ) . subscribe ( ( setting ) => { const states = setting . payloads . find ( payload => payload . id === id ) . tableStates ; const stateIdx = states . findIndex ( s => s . name === state . name ) ; if ( stateIdx > - 1 ) { states . splice ( stateIdx , 1 ) ; this . indexDB . updateSetting ( setting ) ; } } ) ; } public ngOnDestroy ( ) { this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } }