PackRuble / weather_pack

The project is designed to obtain weather via the OpenWeatherMap API. With handy features. :)
https://pub.dev/packages/weather_pack
MIT License
4 stars 3 forks source link

Relationship between weather model fields and various units of measurement #16

Open PackRuble opened 7 months ago

PackRuble commented 7 months ago

Currently, weather models have fields in standard units of measurement. This

Using weather model fields can be quite inconvenient at the moment:

WeatherCurrent weather;
const tempUnits = Temp.celsius;

// conversion to desired units
final double temp = tempUnits.value(weather.temp!)

I'll omit the ! for now in what follows, as it will probably be addressed in #11

There are perhaps a great number of solutions to this boilerplate. It comes to mind:

final double temp = tempUnits.value(weather.temp.inCelsius)

However, this option is not very successful because it does not take into account the tempUnits parameter (imagine that the user has selected the desired units of measurement in the settings)

In addition, make this field changeable (right now our models are intentionally immutable, then we can play copyWith to change only the parameter of this state). I like this option the best as it elegantly allows you to do everything. I don't see any restrictions.


This change will also affect #12

PackRuble commented 3 months ago

I found a great solution!

The units of measurement of the fields will be according to the model field UnitsMeasure units:

class WeatherCurrent {
  // in those units represented in `units`
  final double temp;
  final UnitsMeasure units;
}

class UnitsMeasure {
  final Temp temp;
  final Speed speed;
  // ...
}

And to convert model into the required units, just use the copyWith(units: newUnits) method.