mirror of
https://github.com/vleeuwenmenno/supplements.git
synced 2025-12-08 14:05:56 +00:00
- Implemented SettingsProvider to manage user preferences for theme options and time ranges for reminders. - Added persistent reminder settings with configurable retry intervals and maximum attempts. - Created UI for settings screen to allow users to customize their preferences. - Integrated shared_preferences for persistent storage of user settings. feat: Introduce Ingredient model - Created Ingredient model to represent nutritional components with properties for id, name, amount, and unit. - Added methods for serialization and deserialization of Ingredient objects. feat: Develop Archived Supplements Screen - Implemented ArchivedSupplementsScreen to display archived supplements with options to unarchive or delete. - Added UI components for listing archived supplements and handling user interactions. chore: Update dependencies in pubspec.yaml and pubspec.lock - Updated shared_preferences dependency to the latest version. - Removed flutter_datetime_picker_plus dependency and added file dependency. - Updated Flutter SDK constraint to >=3.27.0.
132 lines
3.8 KiB
Dart
132 lines
3.8 KiB
Dart
import 'ingredient.dart';
|
|
import 'dart:convert';
|
|
|
|
class Supplement {
|
|
final int? id;
|
|
final String name;
|
|
final String? brand;
|
|
final List<Ingredient> ingredients;
|
|
final int numberOfUnits; // Number of units to take (e.g., 2 capsules)
|
|
final String unitType; // capsules, tablets, ml, etc.
|
|
final int frequencyPerDay;
|
|
final List<String> reminderTimes; // e.g., ['08:00', '20:00']
|
|
final String? notes;
|
|
final DateTime createdAt;
|
|
final bool isActive;
|
|
|
|
Supplement({
|
|
this.id,
|
|
required this.name,
|
|
this.brand,
|
|
this.ingredients = const [],
|
|
required this.numberOfUnits,
|
|
required this.unitType,
|
|
required this.frequencyPerDay,
|
|
required this.reminderTimes,
|
|
this.notes,
|
|
required this.createdAt,
|
|
this.isActive = true,
|
|
});
|
|
|
|
// Helper getters
|
|
double get totalDosagePerIntake {
|
|
// This concept doesn't apply well to multi-ingredient supplements
|
|
// Return 0 as it should be handled per ingredient
|
|
return 0.0;
|
|
}
|
|
|
|
// Get formatted ingredients string for display
|
|
String get ingredientsDisplay {
|
|
if (ingredients.isEmpty) {
|
|
return 'No ingredients specified';
|
|
}
|
|
return ingredients.map((ingredient) =>
|
|
'${ingredient.amount * numberOfUnits}${ingredient.unit} ${ingredient.name}'
|
|
).join(', ');
|
|
}
|
|
|
|
// Get ingredients per single unit
|
|
String get ingredientsPerUnit {
|
|
if (ingredients.isEmpty) {
|
|
return 'No ingredients specified';
|
|
}
|
|
return ingredients.map((ingredient) => ingredient.toString()).join(', ');
|
|
}
|
|
|
|
Map<String, dynamic> toMap() {
|
|
return {
|
|
'id': id,
|
|
'name': name,
|
|
'brand': brand,
|
|
'ingredients': jsonEncode(ingredients.map((ingredient) => ingredient.toMap()).toList()),
|
|
'numberOfUnits': numberOfUnits,
|
|
'unitType': unitType,
|
|
'frequencyPerDay': frequencyPerDay,
|
|
'reminderTimes': reminderTimes.join(','),
|
|
'notes': notes,
|
|
'createdAt': createdAt.toIso8601String(),
|
|
'isActive': isActive ? 1 : 0,
|
|
};
|
|
}
|
|
|
|
factory Supplement.fromMap(Map<String, dynamic> map) {
|
|
List<Ingredient> ingredients = [];
|
|
|
|
// Try to parse ingredients if they exist
|
|
if (map['ingredients'] != null && map['ingredients'].isNotEmpty) {
|
|
try {
|
|
final ingredientsJson = map['ingredients'] as String;
|
|
final ingredientsList = jsonDecode(ingredientsJson) as List;
|
|
ingredients = ingredientsList
|
|
.map((ingredient) => Ingredient.fromMap(ingredient as Map<String, dynamic>))
|
|
.toList();
|
|
} catch (e) {
|
|
// If parsing fails, fall back to empty list
|
|
ingredients = [];
|
|
}
|
|
}
|
|
|
|
return Supplement(
|
|
id: map['id'],
|
|
name: map['name'],
|
|
brand: map['brand'],
|
|
ingredients: ingredients,
|
|
numberOfUnits: map['numberOfUnits'] ?? 1,
|
|
unitType: map['unitType'] ?? 'units',
|
|
frequencyPerDay: map['frequencyPerDay'],
|
|
reminderTimes: map['reminderTimes'].split(','),
|
|
notes: map['notes'],
|
|
createdAt: DateTime.parse(map['createdAt']),
|
|
isActive: map['isActive'] == 1,
|
|
);
|
|
}
|
|
|
|
Supplement copyWith({
|
|
int? id,
|
|
String? name,
|
|
String? brand,
|
|
List<Ingredient>? ingredients,
|
|
int? numberOfUnits,
|
|
String? unitType,
|
|
int? frequencyPerDay,
|
|
List<String>? reminderTimes,
|
|
String? notes,
|
|
DateTime? createdAt,
|
|
bool? isActive,
|
|
}) {
|
|
return Supplement(
|
|
id: id ?? this.id,
|
|
name: name ?? this.name,
|
|
brand: brand ?? this.brand,
|
|
ingredients: ingredients ?? this.ingredients,
|
|
numberOfUnits: numberOfUnits ?? this.numberOfUnits,
|
|
unitType: unitType ?? this.unitType,
|
|
frequencyPerDay: frequencyPerDay ?? this.frequencyPerDay,
|
|
reminderTimes: reminderTimes ?? this.reminderTimes,
|
|
notes: notes ?? this.notes,
|
|
createdAt: createdAt ?? this.createdAt,
|
|
isActive: isActive ?? this.isActive,
|
|
);
|
|
}
|
|
}
|