Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/data/weather/mapper/location_mapper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:weather_app/data/weather/model/location_dto.dart';
import 'package:weather_app/domain/entity/location.dart';

class LocationMapper {
static Location toDomain(LocationDto dto) {
return Location(
latitude: dto.latitude,
longitude: dto.longitude,
country: dto.country,
city: dto.city,
);
}
}
220 changes: 220 additions & 0 deletions lib/data/weather/mapper/weather_mapper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
import 'package:weather_app/data/weather/model/weather_dto.dart';
import 'package:weather_app/domain/entity/weather.dart';
import 'package:weather_app/domain/model/temperature.dart';
import 'package:weather_app/domain/model/current_day_weather_status.dart';
import 'package:weather_app/domain/model/hourly_status.dart';
import 'package:weather_app/domain/model/day_forecast.dart';
import 'package:weather_app/domain/model/speed.dart';
import 'package:weather_app/domain/model/atmospheric_pressure.dart';

class WeatherMapper {
static Weather toDomain(WeatherDto dto) {
return Weather(
currentTemperature: _mapCurrentTemperature(dto),
currentDayWeatherStatus: _mapCurrentDayWeatherStatus(dto),
hourlyStatus: _mapHourlyStatus(dto),
currentDayForecast: _mapCurrentDayForecast(dto),
nextDaysForecast: _mapNextDaysForecast(dto),
isDaytime: _mapIsDaytime(dto),
);
}

static Temperature _mapCurrentTemperature(WeatherDto dto) {
final temperature = dto.currentWeather?.temperature ?? 0.0;
final unit = _parseTemperatureUnit(dto.currentUnits?.temperatureUnit);
return Temperature(temperature: temperature, unit: unit);
}

static TodayWeatherStatus _mapCurrentDayWeatherStatus(WeatherDto dto) {
final windSpeed = Speed(
speed: dto.currentWeather?.windSpeed ?? 0.0,
unit: _parseSpeedUnit(dto.currentUnits?.windSpeedUnit),
);

final feelsLike = Temperature(
temperature: dto.currentWeather?.apparentTemperature ?? 0.0,
unit: _parseTemperatureUnit(dto.currentUnits?.apparentTemperatureUnit),
);

final pressure = AtmosphericPressure(
pressure: dto.currentWeather?.surfacePressure ?? 0.0,
unit: _parsePressureUnit(dto.currentUnits?.surfacePressureUnit),
);

final uvIndex = dto.dailyWeather?.uvIndexMax?.isNotEmpty == true
? dto.dailyWeather!.uvIndexMax!.first
: 0.0;

return TodayWeatherStatus(
windSpeed: windSpeed,
humidity: dto.currentWeather?.relativeHumidity ?? 0.0,
rain: dto.currentWeather?.rain ?? 0.0,
uvIndex: uvIndex,
pressure: pressure,
feelsLike: feelsLike,
);
}

static List<HourlyStatus> _mapHourlyStatus(WeatherDto dto) {
final hourly = dto.hourly;
if (hourly?.time == null ||
hourly?.temperature == null ||
hourly?.weatherCode == null) {
return [];
}

final List<HourlyStatus> hourlyStatuses = [];
final timeList = hourly!.time!;
final tempList = hourly.temperature!;
final codeList = hourly.weatherCode!;

final maxLength = [
timeList.length,
tempList.length,
codeList.length,
].reduce((a, b) => a < b ? a : b);

for (int i = 0; i < maxLength; i++) {
final timeString = timeList[i];
final hour = DateTime.parse(timeString).hour;

final temperature = Temperature(
temperature: tempList[i],
unit: _parseTemperatureUnit(dto.currentUnits?.temperatureUnit),
);

hourlyStatuses.add(
HourlyStatus(
hour: hour,
temperature: temperature,
weatherCode: codeList[i],
),
);
}

return hourlyStatuses;
}

static DayForecast _mapCurrentDayForecast(WeatherDto dto) {
final daily = dto.dailyWeather;
if (daily?.temperatureMin == null ||
daily?.temperatureMax == null ||
daily?.weatherCode == null) {
return DayForecast(
minTemperature: Temperature(
temperature: 0.0,
unit: TemperatureUnit.celecus,
),
maxTemperature: Temperature(
temperature: 0.0,
unit: TemperatureUnit.celecus,
),
weatherConditionCode: 0,
);
}

final tempUnit = _parseTemperatureUnit(dto.currentUnits?.temperatureUnit);

return DayForecast(
minTemperature: Temperature(
temperature: daily!.temperatureMin!.first,
unit: tempUnit,
),
maxTemperature: Temperature(
temperature: daily.temperatureMax!.first,
unit: tempUnit,
),
weatherConditionCode: daily.weatherCode!.first,
);
}

static List<DayForecast> _mapNextDaysForecast(WeatherDto dto) {
final daily = dto.dailyWeather;
if (daily?.temperatureMin == null ||
daily?.temperatureMax == null ||
daily?.weatherCode == null) {
return [];
}

final List<DayForecast> forecasts = [];
final minTempList = daily!.temperatureMin!;
final maxTempList = daily.temperatureMax!;
final codeList = daily.weatherCode!;
final tempUnit = _parseTemperatureUnit(dto.currentUnits?.temperatureUnit);

final maxLength = [
minTempList.length,
maxTempList.length,
codeList.length,
].reduce((a, b) => a < b ? a : b);

// Skip the first day (current day) and get the next days
for (int i = 1; i < maxLength; i++) {
forecasts.add(
DayForecast(
minTemperature: Temperature(
temperature: minTempList[i],
unit: tempUnit,
),
maxTemperature: Temperature(
temperature: maxTempList[i],
unit: tempUnit,
),
weatherConditionCode: codeList[i],
),
);
}

return forecasts;
}

static bool _mapIsDaytime(WeatherDto dto) {
return dto.currentWeather?.isDay == 1;
}

static TemperatureUnit _parseTemperatureUnit(String? unit) {
switch (unit?.toLowerCase()) {
case '°f':
case 'fahrenheit':
return TemperatureUnit.fehrenhite;
case '°c':
case 'celsius':
default:
return TemperatureUnit.celecus;
}
}

static SpeedUnit _parseSpeedUnit(String? unit) {
switch (unit?.toLowerCase()) {
case 'mph':
case 'mi/h':
return SpeedUnit.mph;
case 'km/h':
case 'kmph':
default:
return SpeedUnit.kmph;
}
}

static AtmosphericPressureUnit _parsePressureUnit(String? unit) {
switch (unit?.toLowerCase()) {
case 'hpa':
return AtmosphericPressureUnit.hPa;
case 'kpa':
return AtmosphericPressureUnit.kPa;
case 'mbar':
return AtmosphericPressureUnit.mbar;
case 'atm':
return AtmosphericPressureUnit.atm;
case 'psi':
return AtmosphericPressureUnit.psi;
case 'mmhg':
return AtmosphericPressureUnit.mmHg;
case 'inhg':
return AtmosphericPressureUnit.inHg;
case 'pa':
default:
return AtmosphericPressureUnit.pa;
}
}
}
31 changes: 31 additions & 0 deletions lib/data/weather/model/current_units_dto.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class CurrentUnitsDto {
final String? apparentTemperatureUnit;
final String? rainUnit;
final String? relativeHumidityUnit;
final String? surfacePressureUnit;
final String? temperatureUnit;
final String? timeUnit;
final String? windSpeedUnit;

CurrentUnitsDto({
this.apparentTemperatureUnit,
this.rainUnit,
this.relativeHumidityUnit,
this.surfacePressureUnit,
this.temperatureUnit,
this.timeUnit,
this.windSpeedUnit,
});

factory CurrentUnitsDto.fromJson(Map<String, dynamic> json) {
return CurrentUnitsDto(
apparentTemperatureUnit: json['apparent_temperature'] as String?,
rainUnit: json['rain'] as String?,
relativeHumidityUnit: json['relative_humidity_2m'] as String?,
surfacePressureUnit: json['surface_pressure'] as String?,
temperatureUnit: json['temperature_2m'] as String?,
timeUnit: json['time'] as String?,
windSpeedUnit: json['wind_speed_10m'] as String?,
);
}
}
40 changes: 40 additions & 0 deletions lib/data/weather/model/current_weather_dto.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class CurrentWeatherDto {
final double? apparentTemperature;
final double? interval;
final double? rain;
final double? relativeHumidity;
final double? surfacePressure;
final double? temperature;
final String? time;
final int? weatherCode;
final double? windSpeed;
final int? isDay;

CurrentWeatherDto({
this.apparentTemperature,
this.interval,
this.rain,
this.relativeHumidity,
this.surfacePressure,
this.temperature,
this.time,
this.weatherCode,
this.windSpeed,
this.isDay,
});

factory CurrentWeatherDto.fromJson(Map<String, dynamic> json) {
return CurrentWeatherDto(
apparentTemperature: (json['apparent_temperature'] as num?)?.toDouble(),
interval: (json['interval'] as num?)?.toDouble(),
rain: (json['rain'] as num?)?.toDouble(),
relativeHumidity: (json['relative_humidity_2m'] as num?)?.toDouble(),
surfacePressure: (json['surface_pressure'] as num?)?.toDouble(),
temperature: (json['temperature_2m'] as num?)?.toDouble(),
time: json['time'] as String?,
weatherCode: json['weather_code'] as int?,
windSpeed: (json['wind_speed_10m'] as num?)?.toDouble(),
isDay: json['is_day'] as int?,
);
}
}
32 changes: 32 additions & 0 deletions lib/data/weather/model/daily_weather_dto.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class DailyWeatherDto {
final List<double>? temperatureMax;
final List<double>? temperatureMin;
final List<String>? time;
final List<double>? uvIndexMax;
final List<int>? weatherCode;

DailyWeatherDto({
this.temperatureMax,
this.temperatureMin,
this.time,
this.uvIndexMax,
this.weatherCode,
});

factory DailyWeatherDto.fromJson(Map<String, dynamic> json) {
return DailyWeatherDto(
temperatureMax: (json['temperature_2m_max'] as List?)
?.map((e) => (e as num).toDouble())
.toList(),
temperatureMin: (json['temperature_2m_min'] as List?)
?.map((e) => (e as num).toDouble())
.toList(),
time: (json['time'] as List?)?.map((e) => e as String).toList(),
uvIndexMax: (json['uv_index_max'] as List?)
?.map((e) => (e as num).toDouble())
.toList(),
weatherCode:
(json['weather_code'] as List?)?.map((e) => e as int).toList(),
);
}
}
22 changes: 22 additions & 0 deletions lib/data/weather/model/daily_weather_units_dto.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class DailyWeatherUnitsDto {
final String? temperatureMaxUnit;
final String? temperatureMinUnit;
final String? timeUnit;
final String? uvIndexUnit;

DailyWeatherUnitsDto({
this.temperatureMaxUnit,
this.temperatureMinUnit,
this.timeUnit,
this.uvIndexUnit,
});

factory DailyWeatherUnitsDto.fromJson(Map<String, dynamic> json) {
return DailyWeatherUnitsDto(
temperatureMaxUnit: json['temperature_2m_max'] as String?,
temperatureMinUnit: json['temperature_2m_min'] as String?,
timeUnit: json['time'] as String?,
uvIndexUnit: json['uv_index_max'] as String?,
);
}
}
25 changes: 25 additions & 0 deletions lib/data/weather/model/hourly_weather_dto.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class HourlyWeatherDto {
final List<double>? temperature;
final List<String>? time;
final List<int>? weatherCode;

HourlyWeatherDto({
this.temperature,
this.time,
this.weatherCode,
});

factory HourlyWeatherDto.fromJson(Map<String, dynamic> json) {
return HourlyWeatherDto(
temperature: (json['temperature_2m'] as List?)
?.map((e) => (e as num).toDouble())
.toList(),
time: (json['time'] as List?)
?.map((e) => e as String)
.toList(),
weatherCode: (json['weather_code'] as List?)
?.map((e) => e as int)
.toList(),
);
}
}
Loading
Loading