import { Injectable, inject } from "@angular/core"; import { PublicationRestService } from "../../core/rest-services/publications/publication.rest-service"; import { BehaviorSubject, Observable } from "rxjs"; import { MatSnackBar } from "@angular/material/snack-bar" import { Publication } from "../../core/rest-services/publications/model/publication"; @Injectable() export class HomeService { private publicationRestService = inject(PublicationRestService); private snackBar = inject(MatSnackBar); private publicationsSubject = new BehaviorSubject([]); private isLoadingSubject = new BehaviorSubject(false); get isLoading$(): Observable { return this.isLoadingSubject.asObservable(); } get publications$(): Observable { return this.publicationsSubject.asObservable(); } startLatestPublicationsRetrieving(): void { this.isLoadingSubject.next(true); this.publicationRestService.getLatest() .then(publications => this.publicationsSubject.next(publications)) .catch(error => { this.snackBar.open('An error occurred while retrieving latest publications...'); console.error('An error occurred while retrieving latest publications...', error); }) .finally(() => this.isLoadingSubject.next(false)); } }