import { Injectable } from '@angular/core' ; import { MatSnackBar } from '@angular/material/snack-bar' ; import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject' ; import { Observable } from 'rxjs/internal/Observable' ; @ Injectable ( { providedIn : 'root' , } ) export class StateService { constructor ( private matSnackBar ) { } private readonly userProfileSubject : BehaviorSubject < SpotifyApi . CurrentUsersProfileResponse > = new BehaviorSubject < SpotifyApi . CurrentUsersProfileResponse > ( undefined ) ; private readonly loadingSubject : BehaviorSubject < boolean > = new BehaviorSubject < boolean > ( false ) ; readonly userProfile$ : Observable < SpotifyApi . CurrentUsersProfileResponse > = this . userProfileSubject . asObservable ( ) ; readonly loading$ : Observable < boolean > = this . loadingSubject . asObservable ( ) ; private get userProfile ( ) : SpotifyApi . CurrentUsersProfileResponse { return this . userProfileSubject . getValue ( ) ; } private set userProfile ( userProfile ) { this . userProfileSubject . next ( userProfile ) ; } private get loading ( ) : boolean { return this . loadingSubject . getValue ( ) ; } private set loading ( loading ) { setTimeout ( ( ) => { this . loadingSubject . next ( loading ) ; } ) ; } setUserProfile ( userProfile ) { this . userProfile = userProfile ; } resetUserProfile ( ) { this . userProfile = undefined ; } setLoading ( loading ) { this . loading = loading ; } setError ( errorSummary , errorObject ) { this . matSnackBar . open ( `${ errorSummary } :(` ) ; console . error ( errorObject ) ; } setSuccess ( message ) { this . matSnackBar . open ( message ) ; } }