Skip to content

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

Add dose, unit and stock to supplements

InlitXd224b6a5449b
diff --git a/lib/models/nutrition.dart b/lib/models/nutrition.dart
index 931efbc..bcaf1d3 100644
--- a/lib/models/nutrition.dart
+++ b/lib/models/nutrition.dart
@@ -1,3 +1,5 @@
+import 'dart:math' as math;
+
extension type const Meal(String id) {
static const breakfast = Meal('breakfast');
static const lunch = Meal('lunch');
@@ -473,11 +475,41 @@ class WaterSip {
);
}
+const kSuppUnits = ['capsule', 'pill', 'scoop', 'g', 'mg', 'ml', 'drop', 'iu', 'serving'];
+
+const _suppUnitWords = {
+ 'capsule': ['capsule', 'capsules', 'caps', 'cap', 'cápsula', 'cápsulas', 'capsula', 'capsulas'],
+ 'pill': ['pill', 'pills', 'tablet', 'tablets', 'tab', 'tabs', 'pastilla', 'pastillas', 'comprimido', 'comprimidos'],
+ 'scoop': ['scoop', 'scoops', 'cazo', 'cazos', 'cacito', 'cacitos', 'medida', 'medidas'],
+ 'g': ['g', 'gr', 'grs', 'gram', 'grams', 'gramo', 'gramos'],
+ 'mg': ['mg'],
+ 'ml': ['ml'],
+ 'drop': ['drop', 'drops', 'gota', 'gotas'],
+ 'iu': ['iu', 'ui'],
+ 'serving': ['serving', 'servings', 'ración', 'raciones', 'racion'],
+};
+
+({double amount, String unit})? parseSuppDose(String text) {
+ final m = RegExp(r'^(\d+(?:[.,]\d+)?)\s*([^\d\s].*)?$').firstMatch(text.trim().toLowerCase());
+ if (m == null) return null;
+ final amount = double.tryParse(m.group(1)!.replaceAll(',', '.'));
+ if (amount == null || amount <= 0) return null;
+ final word = (m.group(2) ?? '').trim();
+ if (word.isEmpty) return (amount: amount, unit: '');
+ for (final e in _suppUnitWords.entries) {
+ if (e.value.contains(word)) return (amount: amount, unit: e.key);
+ }
+ return null;
+}
+
class Supplement {
const Supplement({
required this.id,
required this.name,
this.dose = '',
+ this.amount,
+ this.unit = '',
+ this.stock,
this.photo = '',
this.at,
this.days = 0,
@@ -489,6 +521,9 @@ class Supplement {
final String id;
final String name;
final String dose;
+ final double? amount;
+ final String unit;
+ final double? stock;
final String photo;
final int? at;
final int days;
@@ -498,6 +533,8 @@ class Supplement {
bool get everyDay => days == 0 && every <= 1;
+ double get perTake => amount ?? 1;
+
bool onWeekday(int weekday) => days == 0 || days & (1 << weekday) != 0;
bool dueOn(DateTime day) {
@@ -513,6 +550,11 @@ class Supplement {
Supplement copyWith({
String? name,
String? dose,
+ double? amount,
+ bool clearAmount = false,
+ String? unit,
+ double? stock,
+ bool clearStock = false,
String? photo,
int? at,
bool clearAt = false,
@@ -526,6 +568,9 @@ class Supplement {
id: id,
name: name ?? this.name,
dose: dose ?? this.dose,
+ amount: clearAmount ? null : (amount ?? this.amount),
+ unit: unit ?? this.unit,
+ stock: clearStock ? null : (stock ?? this.stock),
photo: photo ?? this.photo,
at: clearAt ? null : (at ?? this.at),
days: days ?? this.days,
@@ -546,6 +591,9 @@ class Supplement {
'id': id,
'n': name,
if (dose.isNotEmpty) 'd': dose,
+ 'q': ?amount,
+ if (unit.isNotEmpty) 'un': unit,
+ 'st': ?stock,
if (photo.isNotEmpty) 'p': photo,
'at': ?at,
if (days != 0) 'w': days,
@@ -554,10 +602,19 @@ class Supplement {
if (until != null) 'u': _date(until!),
};
- factory Supplement.fromJson(Map<String, dynamic> j) => Supplement(
+ factory Supplement.fromJson(Map<String, dynamic> j) {
+ final dose = j['d'] as String? ?? '';
+ final q = (j['q'] as num?)?.toDouble();
+ final unit = j['un'] as String? ?? '';
+ final parsed = q == null ? parseSuppDose(dose) : null;
+ final stock = (j['st'] as num?)?.toDouble();
+ return Supplement(
id: j['id'] as String? ?? '',
name: j['n'] as String? ?? '',
- dose: j['d'] as String? ?? '',
+ dose: dose,
+ amount: q != null && q > 0 ? q : parsed?.amount,
+ unit: q != null ? (kSuppUnits.contains(unit) ? unit : '') : parsed?.unit ?? '',
+ stock: stock == null ? null : math.max(0.0, stock),
photo: j['p'] as String? ?? '',
at: (j['at'] as num?)?.toInt().clamp(0, 24 * 60 - 1),
days: ((j['w'] as num?)?.toInt() ?? 0) & 0xFE,
@@ -565,6 +622,7 @@ class Supplement {
from: _readDate(j['f']),
until: _readDate(j['u']),
);
+ }
}
class NutritionGoals {
diff --git a/test/nutrition_test.dart b/test/nutrition_test.dart
index 761e2d3..5d163d7 100644
--- a/test/nutrition_test.dart
+++ b/test/nutrition_test.dart
@@ -690,6 +690,50 @@ void main() {
});
});
+ group('suplementos con cantidad y existencias', () {
+ test('las dosis escritas a mano se leen como cantidad y unidad', () {
+ expect(parseSuppDose('5 g'), (amount: 5.0, unit: 'g'));
+ expect(parseSuppDose('2 cápsulas'), (amount: 2.0, unit: 'capsule'));
+ expect(parseSuppDose('1,5 scoops'), (amount: 1.5, unit: 'scoop'));
+ expect(parseSuppDose('1000 UI'), (amount: 1000.0, unit: 'iu'));
+ expect(parseSuppDose('3'), (amount: 3.0, unit: ''));
+ expect(parseSuppDose('una cucharadita'), isNull);
+ expect(parseSuppDose('2 con el desayuno'), isNull);
+ final old = Supplement.fromJson({'id': 'a', 'n': 'Creatina', 'd': '5 g'});
+ expect((old.amount, old.unit), (5.0, 'g'));
+ final odd = Supplement.fromJson({'id': 'b', 'n': 'Zinc', 'd': 'una al día'});
+ expect(odd.amount, isNull);
+ expect(fit.suppDoseLabel(odd), 'una al día');
+ });
+
+ test('marcar descuenta la dosis y desmarcar la devuelve', () {
+ final s = fit.addSupplement('Omega-3', amount: 2, unit: 'capsule', stock: 10)!;
+ fit.toggleSupplement(s.id);
+ expect(fit.supplements.single.stock, 8);
+ fit.toggleSupplement(s.id);
+ expect(fit.supplements.single.stock, 10);
+ final saved = Supplement.fromJson(fit.supplements.single.toJson());
+ expect((saved.amount, saved.unit, saved.stock), (2.0, 'capsule', 10.0));
+ });
+
+ test('sabe para cuántos días queda y avisa cuando falta poco', () {
+ final s = fit.addSupplement('Creatina', amount: 5, unit: 'g', stock: 50)!;
+ expect(fit.suppDaysLeft(s), 10);
+ expect(fit.suppRunningLow(s), isFalse);
+ fit.updateSupplement(s.id, stock: 12);
+ expect(fit.suppDaysLeft(fit.supplements.single), 2);
+ expect(fit.suppRunningLow(fit.supplements.single), isTrue);
+ fit.updateSupplement(s.id, stock: 3);
+ expect(fit.suppDaysLeft(fit.supplements.single), 0);
+ fit.updateSupplement(s.id, every: 2);
+ fit.updateSupplement(s.id, stock: 15);
+ expect(fit.suppDaysLeft(fit.supplements.single), 6);
+ fit.updateSupplement(s.id, clearStock: true);
+ expect(fit.suppDaysLeft(fit.supplements.single), isNull);
+ expect(fit.suppDoseLabel(fit.supplements.single), '5 g');
+ });
+ });
+
group('suplementos con hora', () {
test('cada suplemento guarda su foto, su dosis y su hora', () {
final s = fit.addSupplement('Creatina', dose: '5 g', at: 9 * 60)!;