Skip to content

Commit

Permalink
feat: add setIsLoading to manage loading state in settings with JSDocs
Browse files Browse the repository at this point in the history
- Added setIsLoading function to dynamically update and manage the loading state within settings.
- Documented the new function with JSDocs for clarity and maintainability.
- Improved state handling for better user feedback during settings interactions.
  • Loading branch information
mariokreitz committed Sep 19, 2024
1 parent cf96981 commit b7b18a7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/app/services/pokemon.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export class PokemonService {
* of Pokemon.
*/
async getPokemons(): Promise<Pokemon[]> {
this.settingsService.setIsLoading(true);

const lastPokemonLimit = this.interval;

const pokemons = await this.fetchPokemons();
Expand All @@ -108,6 +110,8 @@ export class PokemonService {
return this.getPokemons();
}

this.settingsService.setIsLoading(false);

return pokemons;
}

Expand Down
31 changes: 31 additions & 0 deletions src/app/services/settings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ export class SettingsService {
*/
private readonly language = new BehaviorSubject<string>('en');

/**
* A subject that emits a boolean indicating whether the application is currently loading
* data from PokeAPI.
*/
private readonly isLoading = new BehaviorSubject<boolean>(true);

/**
* An observable that emits the current audio volume whenever it changes.
*/
Expand All @@ -76,6 +82,12 @@ export class SettingsService {
*/
readonly currentLanguage = this.language.asObservable();

/**
* An observable that emits a boolean indicating whether the application is currently
* loading data from PokeAPI.
*/
readonly IsLoading = this.isLoading.asObservable();

/**
* Initializes the settings service by restoring saved state from local storage.
*
Expand Down Expand Up @@ -240,4 +252,23 @@ export class SettingsService {
getLanguage(): string {
return this.language.getValue();
}

/**
* Sets the current loading state.
*
* @param {boolean} isLoading - The new loading state to be set.
* @return {void} No return value.
*/
setIsLoading(isLoading: boolean): void {
this.isLoading.next(isLoading);
}

/**
* Retrieves the current loading state.
*
* @return {boolean} True if the application is currently loading, false otherwise.
*/
getIsLoading(): boolean {
return this.isLoading.getValue();
}
}

0 comments on commit b7b18a7

Please sign in to comment.