mirror of
https://github.com/vleeuwenmenno/supplements.git
synced 2025-09-11 18:29:12 +02:00
feat: adds RDA for intake of vitamins and certain elements based on
canada health values
This commit is contained in:
@@ -10,6 +10,10 @@ enum ThemeOption {
|
||||
class SettingsProvider extends ChangeNotifier {
|
||||
ThemeOption _themeOption = ThemeOption.system;
|
||||
|
||||
// Profile fields
|
||||
DateTime? _dateOfBirth;
|
||||
String? _gender;
|
||||
|
||||
// Time range settings (stored as hours, 0-23)
|
||||
int _morningStart = 5;
|
||||
int _morningEnd = 10;
|
||||
@@ -31,6 +35,19 @@ class SettingsProvider extends ChangeNotifier {
|
||||
|
||||
ThemeOption get themeOption => _themeOption;
|
||||
|
||||
// Profile getters
|
||||
DateTime? get dateOfBirth => _dateOfBirth;
|
||||
String? get gender => _gender;
|
||||
int? get age {
|
||||
if (_dateOfBirth == null) return null;
|
||||
final now = DateTime.now();
|
||||
int years = now.year - _dateOfBirth!.year;
|
||||
final hasHadBirthday = (now.month > _dateOfBirth!.month) ||
|
||||
(now.month == _dateOfBirth!.month && now.day >= _dateOfBirth!.day);
|
||||
if (!hasHadBirthday) years--;
|
||||
return years;
|
||||
}
|
||||
|
||||
// Time range getters
|
||||
int get morningStart => _morningStart;
|
||||
int get morningEnd => _morningEnd;
|
||||
@@ -76,6 +93,13 @@ class SettingsProvider extends ChangeNotifier {
|
||||
final themeIndex = prefs.getInt('theme_option') ?? 0;
|
||||
_themeOption = ThemeOption.values[themeIndex];
|
||||
|
||||
// Load profile fields
|
||||
final dobString = prefs.getString('date_of_birth');
|
||||
if (dobString != null) {
|
||||
_dateOfBirth = DateTime.tryParse(dobString);
|
||||
}
|
||||
_gender = prefs.getString('gender');
|
||||
|
||||
// Load time range settings
|
||||
_morningStart = prefs.getInt('morning_start') ?? 5;
|
||||
_morningEnd = prefs.getInt('morning_end') ?? 10;
|
||||
@@ -106,6 +130,16 @@ class SettingsProvider extends ChangeNotifier {
|
||||
await prefs.setInt('theme_option', option.index);
|
||||
}
|
||||
|
||||
Future<void> setDateOfBirthAndGender(DateTime dateOfBirth, String gender) async {
|
||||
_dateOfBirth = dateOfBirth;
|
||||
_gender = gender;
|
||||
notifyListeners();
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString('date_of_birth', dateOfBirth.toIso8601String());
|
||||
await prefs.setString('gender', gender);
|
||||
}
|
||||
|
||||
Future<void> setTimeRanges({
|
||||
required int morningStart,
|
||||
required int morningEnd,
|
||||
|
@@ -7,6 +7,7 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import '../models/supplement.dart';
|
||||
import '../models/supplement_intake.dart';
|
||||
import '../services/database_helper.dart';
|
||||
import '../services/database_sync_service.dart';
|
||||
import '../services/notification_service.dart';
|
||||
|
||||
class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
@@ -280,6 +281,28 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> duplicateSupplement(int supplementId) async {
|
||||
try {
|
||||
final originalSupplement = await _databaseHelper.getSupplement(supplementId);
|
||||
if (originalSupplement != null) {
|
||||
final newSupplement = originalSupplement.copyWith(
|
||||
setNullId: true, // This will be a new entry
|
||||
newSyncId: true, // Generate a new syncId
|
||||
name: '${originalSupplement.name} (Copy)',
|
||||
createdAt: DateTime.now(),
|
||||
lastModified: DateTime.now(),
|
||||
syncStatus: RecordSyncStatus.pending,
|
||||
isDeleted: false,
|
||||
);
|
||||
await addSupplement(newSupplement);
|
||||
}
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('SupplementsLog: Error duplicating supplement: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteSupplement(int id) async {
|
||||
try {
|
||||
await _databaseHelper.deleteSupplement(id);
|
||||
@@ -420,6 +443,22 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
return _todayIntakes.where((intake) => intake['supplement_id'] == supplementId).length;
|
||||
}
|
||||
|
||||
Map<String, double> get dailyIngredientIntake {
|
||||
final Map<String, double> ingredientIntake = {};
|
||||
|
||||
for (final intake in _todayIntakes) {
|
||||
final supplement = _supplements.firstWhere((s) => s.id == intake['supplement_id']);
|
||||
final unitsTaken = intake['unitsTaken'] as double;
|
||||
|
||||
for (final ingredient in supplement.ingredients) {
|
||||
final currentAmount = ingredientIntake[ingredient.name] ?? 0;
|
||||
ingredientIntake[ingredient.name] = currentAmount + (ingredient.amount * unitsTaken);
|
||||
}
|
||||
}
|
||||
|
||||
return ingredientIntake;
|
||||
}
|
||||
|
||||
// Method to manually refresh daily status (useful for testing or manual refresh)
|
||||
Future<void> refreshDailyStatus() async {
|
||||
if (kDebugMode) {
|
||||
|
Reference in New Issue
Block a user