Skip to content

Offline gym log for Android. No account, no ads, no internet.

Track ten body measurements, each with its own curve

InlitX01d858abdf1a
diff --git a/lib/models/measure.dart b/lib/models/measure.dart
new file mode 100644
index 0000000..65c36e1
--- /dev/null
+++ b/lib/models/measure.dart
@@ -0,0 +1,28 @@
+const List<String> kMeasureKeys = [
+ 'neck',
+ 'shoulders',
+ 'chest',
+ 'arm',
+ 'forearm',
+ 'waist',
+ 'hips',
+ 'thigh',
+ 'calf',
+ 'bodyfat',
+];
+
+class BodyMeasure {
+ const BodyMeasure(this.date, this.key, this.value);
+
+ final DateTime date;
+ final String key;
+ final double value;
+
+ Map<String, dynamic> toJson() => {'d': date.toIso8601String(), 'k': key, 'v': value};
+
+ factory BodyMeasure.fromJson(Map<String, dynamic> j) => BodyMeasure(
+ DateTime.tryParse((j['d'] ?? '') as String) ?? DateTime.now(),
+ (j['k'] ?? '') as String,
+ ((j['v'] ?? 0) as num).toDouble(),
+ );
+}
diff --git a/lib/screens/measures_screen.dart b/lib/screens/measures_screen.dart
new file mode 100644
index 0000000..2b5150b
--- /dev/null
+++ b/lib/screens/measures_screen.dart
@@ -0,0 +1,287 @@
+import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
+import 'package:phosphor_flutter/phosphor_flutter.dart';
+
+import '../l10n/l10n.dart';
+import '../models/measure.dart';
+import '../state/fit_state.dart';
+import '../theme/app_colors.dart';
+import '../theme/app_theme.dart';
+import '../widgets/charts.dart';
+import '../widgets/svg_icon.dart';
+import '../widgets/ui_kit.dart';
+
+class MeasuresScreen extends StatelessWidget {
+ const MeasuresScreen({super.key});
+
+ @override
+ Widget build(BuildContext context) {
+ final gc = context.gc;
+
+ return SafeArea(
+ bottom: false,
+ child: SingleChildScrollView(
+ padding: const EdgeInsets.fromLTRB(20, 12, 20, 40),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.stretch,
+ children: [
+ Row(
+ children: [
+ RoundBtn(icon: Ic.chevronLeft, onTap: fit.backFromMeasures),
+ const SizedBox(width: 12),
+ Expanded(
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ ScreenTitle(t.measures, size: 20),
+ const SizedBox(height: 2),
+ Text(t.measureCount(fit.measures.length),
+ style: AppTheme.s(12.5, color: gc.textSecondary)),
+ ],
+ ),
+ ),
+ ],
+ ),
+ const SizedBox(height: 14),
+ Text(t.measuresHint, style: AppTheme.s(13, color: gc.textSecondary, height: 1.5)),
+ const SizedBox(height: 20),
+ for (final key in kMeasureKeys) _MeasureRow(measureKey: key),
+ ],
+ ),
+ ),
+ );
+ }
+}
+
+class _MeasureRow extends StatelessWidget {
+ const _MeasureRow({required this.measureKey});
+
+ final String measureKey;
+
+ @override
+ Widget build(BuildContext context) {
+ final gc = context.gc;
+ final latest = fit.latestMeasure(measureKey);
+ final change = fit.measureChange(measureKey);
+ final series = fit.measureSeries(measureKey);
+
+ return Padding(
+ padding: const EdgeInsets.only(bottom: 12),
+ child: GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onTap: () => showMeasureSheet(context, measureKey),
+ child: SoftCard(
+ radius: 18,
+ padding: const EdgeInsets.fromLTRB(18, 16, 18, 16),
+ child: Row(
+ children: [
+ Expanded(
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Text(t.measureName(measureKey),
+ style: AppTheme.s(12,
+ weight: FontWeight.w600, color: gc.textSecondary, letterSpacing: 1)),
+ const SizedBox(height: 6),
+ if (latest == null)
+ Text(t.measureNoneYet, style: AppTheme.s(14, color: gc.textTertiary))
+ else
+ Row(
+ crossAxisAlignment: CrossAxisAlignment.end,
+ children: [
+ Text(fit.measureValue(measureKey, latest.value),
+ style: AppTheme.d(26, weight: FontWeight.w700, color: gc.text)),
+ const SizedBox(width: 4),
+ Padding(
+ padding: const EdgeInsets.only(bottom: 3),
+ child: Text(fit.measureUnit(measureKey),
+ style: AppTheme.d(13,
+ weight: FontWeight.w600, color: gc.textSecondary)),
+ ),
+ if (change != null && change != 0) ...[
+ const SizedBox(width: 10),
+ Padding(
+ padding: const EdgeInsets.only(bottom: 3),
+ child: Text(
+ '${change > 0 ? '+' : ''}${fmt(change)}',
+ style: AppTheme.s(12.5,
+ weight: FontWeight.w600,
+ color: change > 0 ? gc.sage : gc.accent),
+ ),
+ ),
+ ],
+ ],
+ ),
+ ],
+ ),
+ ),
+ if (series.length > 1)
+ SizedBox(
+ width: 84,
+ child: Sparkline(values: series, height: 34, color: gc.accent),
+ )
+ else
+ Icon(PhosphorIconsRegular.plusCircle, size: 20, color: gc.textTertiary),
+ ],
+ ),
+ ),
+ ),
+ );
+ }
+}
+
+Future<void> showMeasureSheet(BuildContext context, String key) => showModalBottomSheet<void>(
+ context: context,
+ isScrollControlled: true,
+ backgroundColor: Colors.transparent,
+ builder: (_) => _MeasureSheet(measureKey: key),
+ );
+
+class _MeasureSheet extends StatefulWidget {
+ const _MeasureSheet({required this.measureKey});
+
+ final String measureKey;
+
+ @override
+ State<_MeasureSheet> createState() => _MeasureSheetState();
+}
+
+class _MeasureSheetState extends State<_MeasureSheet> {
+ late final TextEditingController _value = TextEditingController(
+ text: () {
+ final latest = fit.latestMeasure(widget.measureKey);
+ return latest == null ? '' : fit.measureValue(widget.measureKey, latest.value);
+ }(),
+ );
+
+ @override
+ void dispose() {
+ _value.dispose();
+ super.dispose();
+ }
+
+ void _save() {
+ final parsed = double.tryParse(_value.text.trim().replaceAll(',', '.'));
+ if (parsed == null || parsed <= 0) return;
+ fit.addMeasure(widget.measureKey, parsed);
+ Navigator.of(context).pop();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ final gc = context.gc;
+ final history = fit.measureHistory(widget.measureKey);
+
+ return Padding(
+ padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
+ child: Container(
+ decoration: BoxDecoration(
+ color: gc.bg,
+ border: Border.all(color: gc.border),
+ borderRadius: const BorderRadius.vertical(top: Radius.circular(26)),
+ ),
+ clipBehavior: Clip.antiAlias,
+ child: SafeArea(
+ top: false,
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ Container(
+ width: 40,
+ height: 4,
+ margin: const EdgeInsets.symmetric(vertical: 12),
+ decoration: BoxDecoration(color: gc.border, borderRadius: BorderRadius.circular(2)),
+ ),
+ Padding(
+ padding: const EdgeInsets.fromLTRB(20, 4, 20, 20),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.stretch,
+ children: [
+ Text(t.measureName(widget.measureKey).toUpperCase(),
+ style: AppTheme.d(18,
+ weight: FontWeight.w700, color: gc.text, letterSpacing: 1)),
+ const SizedBox(height: 16),
+ Row(
+ children: [
+ Expanded(
+ child: TextField(
+ controller: _value,
+ autofocus: true,
+ keyboardType: const TextInputType.numberWithOptions(decimal: true),
+ inputFormatters: [
+ FilteringTextInputFormatter.allow(RegExp(r'[0-9.,]')),
+ ],
+ onSubmitted: (_) => _save(),
+ cursorColor: gc.accent,
+ style: AppTheme.d(22, weight: FontWeight.w700, color: gc.text),
+ decoration: InputDecoration(
+ hintText: '0',
+ hintStyle: AppTheme.d(22, weight: FontWeight.w700, color: gc.textTertiary),
+ suffixText: fit.measureUnit(widget.measureKey),
+ suffixStyle: AppTheme.d(14, weight: FontWeight.w600, color: gc.textSecondary),
+ filled: true,
+ fillColor: gc.bgRaised,
+ contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
+ enabledBorder: OutlineInputBorder(
+ borderRadius: BorderRadius.circular(14),
+ borderSide: BorderSide(color: gc.border)),
+ focusedBorder: OutlineInputBorder(
+ borderRadius: BorderRadius.circular(14),
+ borderSide: BorderSide(color: gc.accent)),
+ ),
+ ),
+ ),
+ const SizedBox(width: 12),
+ SizedBox(
+ width: 120,
+ child: PrimaryButton(label: t.save, onTap: _save, height: 52),
+ ),
+ ],
+ ),
+ if (history.isNotEmpty) ...[
+ const SizedBox(height: 22),
+ Text(t.measureHistory,
+ style: AppTheme.d(12,
+ weight: FontWeight.w600, color: gc.textSecondary, letterSpacing: 2)),
+ const SizedBox(height: 10),
+ for (final m in history.take(6)) _historyRow(gc, m),
+ ],
+ ],
+ ),
+ ),
+ ],
+ ),
+ ),
+ ),
+ );
+ }
+
+ Widget _historyRow(GymColors gc, BodyMeasure m) => Padding(
+ padding: const EdgeInsets.symmetric(vertical: 7),
+ child: Row(
+ children: [
+ Expanded(
+ child: Text(t.shortDateYear(m.date), style: AppTheme.s(12.5, color: gc.textSecondary)),
+ ),
+ Text(fit.measureLabel(m.key, m.value),
+ style: AppTheme.s(13.5, weight: FontWeight.w600, color: gc.text)),
+ const SizedBox(width: 8),
+ Semantics(
+ button: true,
+ label: t.delete,
+ child: GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onTap: () {
+ fit.deleteMeasure(m);
+ setState(() {});
+ },
+ child: Padding(
+ padding: const EdgeInsets.all(6),
+ child: SvgPathIcon(Ic.close, size: 12, color: gc.textTertiary),
+ ),
+ ),
+ ),
+ ],
+ ),
+ );
+}
diff --git a/lib/state/measures_state.dart b/lib/state/measures_state.dart
new file mode 100644
index 0000000..74c0653
--- /dev/null
+++ b/lib/state/measures_state.dart
@@ -0,0 +1,78 @@
+part of 'fit_state.dart';
+
+const double _cmPerInch = 2.54;
+
+mixin MeasuresState on FitCore {
+ bool get isInches => units == 'lb';
+
+ String get lengthUnit => isInches ? 'in' : 'cm';
+
+ bool isPercent(String key) => key == 'bodyfat';
+
+ double toDisplayMeasure(String key, double raw) =>
+ isPercent(key) || !isInches ? raw : raw / _cmPerInch;
+
+ double fromDisplayMeasure(String key, double shown) =>
+ isPercent(key) || !isInches ? shown : shown * _cmPerInch;
+
+ double toDisplayCm(double cm) => isInches ? cm / _cmPerInch : cm;
+
+ double fromDisplayCm(double shown) => isInches ? shown * _cmPerInch : shown;
+
+ String cmLabel(double cm) => '${fmt(_round1(toDisplayCm(cm)))} $lengthUnit';
+
+ double get cmStep => isInches ? _cmPerInch / 2 : 1;
+
+ double get girthStep => isInches ? _cmPerInch / 4 : 0.5;
+
+ String measureUnit(String key) => isPercent(key) ? '%' : lengthUnit;
+
+ String measureValue(String key, double raw) => fmt(_round1(toDisplayMeasure(key, raw)));
+
+ String measureLabel(String key, double raw) => '${measureValue(key, raw)} ${measureUnit(key)}';
+
+ List<BodyMeasure> measureHistory(String key) {
+ final out = measures.where((m) => m.key == key).toList()
+ ..sort((a, b) => b.date.compareTo(a.date));
+ return out;
+ }
+
+ BodyMeasure? latestMeasure(String key) {
+ final h = measureHistory(key);
+ return h.isEmpty ? null : h.first;
+ }
+
+ double? measureChange(String key) {
+ final h = measureHistory(key);
+ if (h.length < 2) return null;
+ return _round1(toDisplayMeasure(key, h.first.value) - toDisplayMeasure(key, h[1].value));
+ }
+
+ List<double> measureSeries(String key) =>
+ measureHistory(key).reversed.map((m) => toDisplayMeasure(key, m.value)).toList();
+
+ List<String> get trackedMeasures =>
+ kMeasureKeys.where((k) => latestMeasure(k) != null).toList();
+
+ bool get hasMeasures => measures.isNotEmpty;
+
+ void addMeasure(String key, double shown, {DateTime? date}) {
+ if (!kMeasureKeys.contains(key) || shown <= 0) return;
+ final when = date ?? DateTime.now();
+ final raw = _round1(fromDisplayMeasure(key, shown));
+ measures.removeWhere((m) => m.key == key && _dayKey(m.date) == _dayKey(when));
+ measures.add(BodyMeasure(when, key, raw));
+ _persist();
+ notifyListeners();
+ }
+
+ void deleteMeasure(BodyMeasure m) {
+ measures.remove(m);
+ _persist();
+ notifyListeners();
+ }
+
+ void goMeasures() => pushRoute('measures');
+
+ void backFromMeasures() => popRoute(fallback: 'progress');
+}
diff --git a/test/measures_test.dart b/test/measures_test.dart
new file mode 100644
index 0000000..981ebf1
--- /dev/null
+++ b/test/measures_test.dart
@@ -0,0 +1,85 @@
+import 'package:flutter_test/flutter_test.dart';
+import 'package:gymmane/services/local_store.dart';
+import 'package:gymmane/state/fit_state.dart';
+import 'package:shared_preferences/shared_preferences.dart';
+
+void main() {
+ TestWidgetsFlutterBinding.ensureInitialized();
+
+ setUp(() async {
+ SharedPreferences.setMockInitialValues({});
+ await Store.instance.init();
+ fit.resetAllData();
+ fit.setUnits('kg');
+ });
+
+ test('a reading is kept and read back with its unit', () {
+ fit.addMeasure('waist', 82.5);
+ expect(fit.latestMeasure('waist')!.value, closeTo(82.5, 0.001));
+ expect(fit.measureLabel('waist', 82.5), '82.5 cm');
+ expect(fit.measureUnit('bodyfat'), '%');
+ });
+
+ test('two readings the same day replace each other instead of piling up', () {
+ fit.addMeasure('chest', 100);
+ fit.addMeasure('chest', 101);
+ expect(fit.measureHistory('chest'), hasLength(1));
+ expect(fit.latestMeasure('chest')!.value, closeTo(101, 0.001));
+ });
+
+ test('the change compares the last two readings', () {
+ fit.addMeasure('arm', 38, date: DateTime.now().subtract(const Duration(days: 30)));
+ fit.addMeasure('arm', 39.5);
+ expect(fit.measureChange('arm'), closeTo(1.5, 0.001));
+ expect(fit.measureChange('calf'), isNull);
+ });
+
+ test('inches go in and come back out as inches, stored as cm', () {
+ fit.setUnits('lb');
+ fit.addMeasure('waist', 32);
+ expect(fit.lengthUnit, 'in');
+ expect(fit.latestMeasure('waist')!.value, closeTo(81.3, 0.05));
+ expect(fit.measureValue('waist', fit.latestMeasure('waist')!.value), '32');
+
+ fit.setUnits('kg');
+ expect(fit.measureValue('waist', fit.latestMeasure('waist')!.value), '81.3');
+ });
+
+ test('body fat is a percentage and never gets converted', () {
+ fit.setUnits('lb');
+ fit.addMeasure('bodyfat', 14.2);
+ expect(fit.latestMeasure('bodyfat')!.value, closeTo(14.2, 0.001));
+ expect(fit.measureLabel('bodyfat', 14.2), '14.2 %');
+ });
+
+ test('junk readings are turned away', () {
+ fit.addMeasure('waist', 0);
+ fit.addMeasure('waist', -5);
+ fit.addMeasure('nose', 10);
+ expect(fit.measures, isEmpty);
+ });
+
+ test('readings survive a save and a reload', () {
+ fit.addMeasure('thigh', 60);
+ fit.addMeasure('bodyfat', 15);
+ fit.persistNow();
+
+ fit.measures.clear();
+ fit.loadFromStore();
+
+ expect(fit.trackedMeasures, ['thigh', 'bodyfat']);
+ expect(fit.latestMeasure('thigh')!.value, closeTo(60, 0.001));
+ });
+
+ test('a reading can be dropped again', () {
+ fit.addMeasure('neck', 40);
+ fit.deleteMeasure(fit.latestMeasure('neck')!);
+ expect(fit.hasMeasures, false);
+ });
+
+ test('reset takes the readings with it', () {
+ fit.addMeasure('hips', 95);
+ fit.resetAllData();
+ expect(fit.measures, isEmpty);
+ });
+}