Skip to content

Commit 9318174

Browse files
committed
hopefully correct convertion for Withings date
1 parent fdaa487 commit 9318174

File tree

2 files changed

+96
-21
lines changed

2 files changed

+96
-21
lines changed

lib/common/converters.dart

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
import 'dart:math';
22

3+
import 'package:logger/logger.dart';
4+
35
import 'mappers.dart';
46

57
enum WithingsType {
68
weight(1),
7-
fatMass(5),
8-
fatFreeMass(6),
9-
fatRatio(8),
10-
bmi(76);
9+
height(4),
10+
fatFreeMass(5),
11+
fatRatio(6),
12+
fatMass(8);
1113

1214
final int id;
1315
const WithingsType(this.id);
1416
}
1517

18+
final _logger = Logger(printer: SimplePrinter(colors: false));
19+
1620
WithingsObject toWithingsObject(Map<String, dynamic> data) {
17-
final date = DateTime.fromMillisecondsSinceEpoch(
18-
(data['date'] as num).toInt(),
19-
);
21+
final seconds = int.tryParse(data['date'].toString()) ?? 0;
22+
final date = DateTime.fromMillisecondsSinceEpoch(seconds * 1000);
2023

21-
final measures = data['measures'] as List<Map<String, dynamic>>;
24+
_logger.d('Withings date: $date');
25+
26+
final measures = (data['measures'] as List).cast<Map<String, dynamic>>();
2227

2328
final valuesByType = <int, double>{};
2429

@@ -39,10 +44,15 @@ WithingsObject toWithingsObject(Map<String, dynamic> data) {
3944
return value;
4045
}
4146

47+
final weight = getValue(WithingsType.weight.id);
48+
final height = getValue(4); // height in meters
49+
50+
final bmi = double.parse((weight / (height * height)).toStringAsFixed(2));
51+
4252
return WithingsObject(
4353
date: date,
44-
bmi: getValue(WithingsType.bmi.id),
45-
weightInKg: getValue(WithingsType.weight.id),
54+
bmi: bmi,
55+
weightInKg: weight,
4656
fatInKg: getValue(WithingsType.fatMass.id),
4757
leanInKg: getValue(WithingsType.fatFreeMass.id),
4858
fatInPercentage: getValue(WithingsType.fatRatio.id),

test/common/converters_test.dart

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@ import 'package:flutter_test/flutter_test.dart';
55

66
void main() {
77
group('toWithingsObject', () {
8-
test('should correctly convert data to WithingsObject', () {
8+
test('should correctly convert data to WithingsObject 1', () {
99
final Map<String, dynamic> data = {
10-
'date': DateTime.parse('2019-03-01T07:38:14').millisecondsSinceEpoch,
10+
'date':
11+
(DateTime.parse('2019-03-01T07:38:14').millisecondsSinceEpoch ~/
12+
1000)
13+
.toString(),
1114
'measures': [
12-
{'type': 76, 'value': 25.91, 'unit': 0},
13-
{'type': 1, 'value': 70000, 'unit': -3},
14-
{'type': 5, 'value': 15000, 'unit': -3},
15-
{'type': 6, 'value': 55000, 'unit': -3},
16-
{'type': 8, 'value': 21, 'unit': 0},
15+
{'type': 1, 'value': 70000, 'unit': -3}, // Weight = 70.0
16+
{'type': 4, 'value': 1643, 'unit': -3}, // Height = 1.643
17+
{'type': 5, 'value': 55000, 'unit': -3}, // Fat-Free Mass = 55.0
18+
{'type': 6, 'value': 21, 'unit': 0}, // Fat Ratio = 21%
19+
{'type': 8, 'value': 15000, 'unit': -3}, // Fat Mass = 15.0
1720
],
1821
};
1922

2023
final expected = WithingsObject(
2124
date: DateTime(2019, 3, 1, 7, 38, 14),
22-
bmi: 25.91,
25+
bmi: 25.93,
2326
weightInKg: 70.0,
2427
fatInKg: 15.0,
2528
leanInKg: 55.0,
@@ -29,15 +32,74 @@ void main() {
2932
final result = toWithingsObject(data);
3033

3134
expect(result, isA<WithingsObject>());
32-
expect(result.date, equals(expected.date));
35+
36+
expect(result.date.year, equals(expected.date.year));
37+
expect(result.date.month, equals(expected.date.month));
38+
expect(result.date.day, equals(expected.date.day));
39+
3340
expect(result.weightInKg, equals(expected.weightInKg));
3441
expect(result.fatInKg, equals(expected.fatInKg));
3542
expect(result.leanInKg, equals(expected.leanInKg));
3643
expect(result.fatInPercentage, equals(expected.fatInPercentage));
3744
expect(result.bmi, equals(expected.bmi));
3845

3946
expect(result.metrics, isA<Metrics>());
40-
expect(result.metrics.date, equals(expected.metrics.date));
47+
48+
expect(result.metrics.date.year, equals(expected.metrics.date.year));
49+
expect(result.metrics.date.month, equals(expected.metrics.date.month));
50+
expect(result.metrics.date.day, equals(expected.metrics.date.day));
51+
52+
expect(result.metrics.weightInKg, equals(expected.metrics.weightInKg));
53+
expect(result.metrics.fatInKg, equals(expected.metrics.fatInKg));
54+
expect(result.metrics.leanInKg, equals(expected.metrics.leanInKg));
55+
expect(
56+
result.metrics.fatInPercentage,
57+
equals(expected.metrics.fatInPercentage),
58+
);
59+
expect(result.metrics.bmi, equals(expected.metrics.bmi));
60+
});
61+
62+
test('should correctly convert data to WithingsObject 2', () {
63+
final Map<String, dynamic> data = {
64+
'date': "1684496400", // May 19, 2023
65+
'measures': [
66+
{'type': 1, 'value': 70000, 'unit': -3},
67+
{'type': 4, 'value': 1750, 'unit': -2},
68+
{'type': 5, 'value': 15000, 'unit': -3},
69+
{'type': 6, 'value': 200, 'unit': -1},
70+
{'type': 8, 'value': 14000, 'unit': -3},
71+
],
72+
};
73+
74+
final expected = WithingsObject(
75+
date: DateTime(2023, 5, 19, 7, 0, 0),
76+
bmi: 0.23,
77+
weightInKg: 70.0,
78+
fatInKg: 14.0,
79+
leanInKg: 15.0,
80+
fatInPercentage: 20.0,
81+
);
82+
83+
final result = toWithingsObject(data);
84+
85+
expect(result, isA<WithingsObject>());
86+
87+
expect(result.date.year, equals(expected.date.year));
88+
expect(result.date.month, equals(expected.date.month));
89+
expect(result.date.day, equals(expected.date.day));
90+
91+
expect(result.weightInKg, equals(expected.weightInKg));
92+
expect(result.fatInKg, equals(expected.fatInKg));
93+
expect(result.leanInKg, equals(expected.leanInKg));
94+
expect(result.fatInPercentage, equals(expected.fatInPercentage));
95+
expect(result.bmi, equals(expected.bmi));
96+
97+
expect(result.metrics, isA<Metrics>());
98+
99+
expect(result.metrics.date.year, equals(expected.metrics.date.year));
100+
expect(result.metrics.date.month, equals(expected.metrics.date.month));
101+
expect(result.metrics.date.day, equals(expected.metrics.date.day));
102+
41103
expect(result.metrics.weightInKg, equals(expected.metrics.weightInKg));
42104
expect(result.metrics.fatInKg, equals(expected.metrics.fatInKg));
43105
expect(result.metrics.leanInKg, equals(expected.metrics.leanInKg));
@@ -50,7 +112,10 @@ void main() {
50112

51113
test('should throw an exception if a required measurement is missing', () {
52114
final data = {
53-
'date': DateTime.parse('2019-03-01T07:38:14').millisecondsSinceEpoch,
115+
'date':
116+
DateTime.parse(
117+
'2019-03-01T07:38:14',
118+
).millisecondsSinceEpoch.toString(),
54119
'measures': [
55120
{'type': 1, 'value': 70000, 'unit': -3},
56121
{'type': 5, 'value': 15000, 'unit': -3},

0 commit comments

Comments
 (0)