main
1import 'package:health_data_store/health_data_store.dart';
2
3/// A unit [Weight] can be in.
4enum WeightUnit {
5 /// Kilograms, SI unit
6 kg,
7
8 /// Pounds, Defined by the Units of Measurement Regulations 1994
9 lbs,
10
11 /// Stone, the imperial unit of mass
12 st;
13
14 /// Restore from [serialized].
15 static WeightUnit? deserialize(int? value) => switch(value) {
16 0 => WeightUnit.kg,
17 1 => WeightUnit.lbs,
18 2 => WeightUnit.st,
19 _ => null,
20 };
21
22 /// Create a [WeightUnit.deserialize]able number.
23 int get serialized => switch(this) {
24 WeightUnit.kg => 0,
25 WeightUnit.lbs => 1,
26 WeightUnit.st => 2,
27 };
28
29 /// Create a [Weight] from a double in this unit.
30 Weight store(double value) => switch(this) {
31 WeightUnit.kg => Weight.kg(value),
32 WeightUnit.lbs => Weight.kg(value * 2.2046226218488),
33 WeightUnit.st => Weight.kg(value * 6.350),
34 };
35
36 /// Extract a weight to the preferred unit.
37 double extract(Weight w) => switch(this) {
38 WeightUnit.kg => w.kg,
39 WeightUnit.lbs => w.kg / 2.2046226218488,
40 WeightUnit.st => w.kg / 6.350,
41 };
42}