Skip to content

Commit a08aedf

Browse files
committed
update: Weather service
1 parent 64573f4 commit a08aedf

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

src/app/core/services/weather.service.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Injectable, inject } from "@angular/core";
22
import { HttpClient } from "@angular/common/http";
3-
import { Observable, forkJoin, of, switchMap } from "rxjs";
3+
import { Observable, delay, forkJoin, of, switchMap } from "rxjs";
44
import { LangService } from "../lang/lang.service";
5-
import { GeolocationService } from "./geolocation.service";
65
import { IWeatherAqi, IWeatherOne } from "../interface/weather-owm.interface";
76
import { environment } from "../../../environments/environment.development";
87

@@ -23,11 +22,6 @@ export class WeatherService {
2322
*/
2423
private langService: LangService = inject(LangService);
2524

26-
/**
27-
* Service to manage geolocation.
28-
*/
29-
private geolocationService: GeolocationService = inject(GeolocationService);
30-
3125
/**
3226
* API key for OpenWeatherMap.
3327
*/
@@ -41,43 +35,45 @@ export class WeatherService {
4135
/**
4236
* URL for the OpenWeatherMap One Call endpoint.
4337
*/
44-
private readonly weatherOwmOne: string = environment.WEATHER_OWM;
38+
private readonly weatherOwmOne_mock: string = environment.MOCK.WEATHER_OWM_ONE;
39+
private readonly weatherOwmOne_endpoint: string = environment.ENDPOINT.WEATHER_OWM_ONE;
4540

4641
/**
4742
* URL for the OpenWeatherMap AQI endpoint.
4843
*/
49-
private readonly weatherOwmAqi: string = environment.WEATHER_OWM_AQI;
44+
private readonly weatherOwmAqi_mock: string = environment.MOCK.WEATHER_OWM_AQI;
45+
private readonly weatherOwmAqi_endpoint: string = environment.ENDPOINT.WEATHER_OWM_AQI;
5046

5147
/**
5248
* Retrieves comprehensive weather data in a single request.
5349
* @returns {Observable<IWeatherOne>} An Observable emitting the weather data.
5450
*/
55-
private getWeatherOne(): Observable<IWeatherOne> {
51+
private getWeatherOne(lat: number, lon: number): Observable<IWeatherOne> {
5652
const url = this.isProduction
57-
? `${this.weatherOwmOne}lat=${this.geolocationService.lat()}&lon=${this.geolocationService.lon()}&units=metric&lang=${this.langService.currentLangSig()}&appid=${this.weatherOwmKey}`
58-
: this.weatherOwmOne;
53+
? `${this.weatherOwmOne_endpoint}lat=${lat}&lon=${lon}&units=metric&lang=${this.langService.currentLangSig()}&appid=${this.weatherOwmKey}`
54+
: this.weatherOwmOne_mock;
5955
return this.http.get<IWeatherOne>(url);
6056
}
6157

6258
/**
6359
* Retrieves air quality index (AQI) data.
6460
* @returns {Observable<IWeatherAqi>} An Observable emitting the AQI data.
6561
*/
66-
private getWeatherAqi(): Observable<IWeatherAqi> {
62+
private getWeatherAqi(lat: number, lon: number): Observable<IWeatherAqi> {
6763
const url = this.isProduction
68-
? `${this.weatherOwmAqi}lat=${this.geolocationService.lon()}&lon=${this.geolocationService.lon()}&appid=${this.weatherOwmKey}`
69-
: this.weatherOwmAqi;
64+
? `${this.weatherOwmAqi_endpoint}lat=${lat}&lon=${lon}&appid=${this.weatherOwmKey}`
65+
: this.weatherOwmAqi_mock;
7066
return this.http.get<IWeatherAqi>(url);
7167
}
7268

7369
/**
7470
* Combines weather and AQI data into a single object.
7571
* @returns {Observable<any>} An Observable emitting an object containing both weather data and AQI information.
7672
*/
77-
public weatherMergeData(): Observable<any> {
73+
public weatherMergeData(lat: number, lon: number): Observable<any> {
7874
return forkJoin({
79-
weather: this.getWeatherOne(),
80-
aqi: this.getWeatherAqi(),
75+
weather: this.getWeatherOne(lat, lon),
76+
aqi: this.getWeatherAqi(lat, lon),
8177
}).pipe(
8278
switchMap((results) => {
8379
const combinedData = {

0 commit comments

Comments
 (0)