initial commit

Signed-off-by: Menno van Leeuwen <menno@vleeuwen.me>
This commit is contained in:
2025-08-26 01:21:26 +02:00
commit f8c19f9051
132 changed files with 7054 additions and 0 deletions

View File

@@ -0,0 +1,176 @@
import 'package:flutter/foundation.dart';
import '../models/supplement.dart';
import '../models/supplement_intake.dart';
import '../services/database_helper.dart';
import '../services/notification_service.dart';
class SupplementProvider with ChangeNotifier {
final DatabaseHelper _databaseHelper = DatabaseHelper.instance;
final NotificationService _notificationService = NotificationService();
List<Supplement> _supplements = [];
List<Map<String, dynamic>> _todayIntakes = [];
List<Map<String, dynamic>> _monthlyIntakes = [];
bool _isLoading = false;
List<Supplement> get supplements => _supplements;
List<Map<String, dynamic>> get todayIntakes => _todayIntakes;
List<Map<String, dynamic>> get monthlyIntakes => _monthlyIntakes;
bool get isLoading => _isLoading;
Future<void> initialize() async {
await _notificationService.initialize();
await _notificationService.requestPermissions();
await loadSupplements();
await loadTodayIntakes();
}
Future<void> loadSupplements() async {
_isLoading = true;
notifyListeners();
try {
print('Loading supplements from database...');
_supplements = await _databaseHelper.getAllSupplements();
print('Loaded ${_supplements.length} supplements');
for (var supplement in _supplements) {
print('Supplement: ${supplement.name}');
}
} catch (e) {
print('Error loading supplements: $e');
if (kDebugMode) {
print('Error loading supplements: $e');
}
} finally {
_isLoading = false;
notifyListeners();
}
}
Future<void> addSupplement(Supplement supplement) async {
try {
print('Adding supplement: ${supplement.name}');
final id = await _databaseHelper.insertSupplement(supplement);
print('Supplement inserted with ID: $id');
final newSupplement = supplement.copyWith(id: id);
// Schedule notifications (skip if there's an error)
try {
await _notificationService.scheduleSupplementReminders(newSupplement);
print('Notifications scheduled');
} catch (notificationError) {
print('Warning: Could not schedule notifications: $notificationError');
}
await loadSupplements();
print('Supplements reloaded, count: ${_supplements.length}');
} catch (e) {
print('Error adding supplement: $e');
if (kDebugMode) {
print('Error adding supplement: $e');
}
rethrow;
}
}
Future<void> updateSupplement(Supplement supplement) async {
try {
await _databaseHelper.updateSupplement(supplement);
// Reschedule notifications
await _notificationService.scheduleSupplementReminders(supplement);
await loadSupplements();
} catch (e) {
if (kDebugMode) {
print('Error updating supplement: $e');
}
}
}
Future<void> deleteSupplement(int id) async {
try {
await _databaseHelper.deleteSupplement(id);
// Cancel notifications
await _notificationService.cancelSupplementReminders(id);
await loadSupplements();
} catch (e) {
if (kDebugMode) {
print('Error deleting supplement: $e');
}
}
}
Future<void> recordIntake(int supplementId, double dosage, {int? unitsTaken, String? notes}) async {
try {
final intake = SupplementIntake(
supplementId: supplementId,
takenAt: DateTime.now(),
dosageTaken: dosage,
unitsTaken: unitsTaken ?? 1,
notes: notes,
);
await _databaseHelper.insertIntake(intake);
await loadTodayIntakes();
// Show confirmation notification
final supplement = _supplements.firstWhere((s) => s.id == supplementId);
final unitsText = unitsTaken != null && unitsTaken > 1 ? '$unitsTaken ${supplement.unitType}' : '';
await _notificationService.showInstantNotification(
'Supplement Taken',
'Recorded ${supplement.name}${unitsText.isNotEmpty ? ' - $unitsText' : ''} ($dosage ${supplement.unit})',
);
} catch (e) {
if (kDebugMode) {
print('Error recording intake: $e');
}
}
}
Future<void> loadTodayIntakes() async {
try {
_todayIntakes = await _databaseHelper.getIntakesWithSupplementsForDate(DateTime.now());
notifyListeners();
} catch (e) {
if (kDebugMode) {
print('Error loading today\'s intakes: $e');
}
}
}
Future<void> loadMonthlyIntakes(int year, int month) async {
try {
_monthlyIntakes = await _databaseHelper.getIntakesWithSupplementsForMonth(year, month);
notifyListeners();
} catch (e) {
if (kDebugMode) {
print('Error loading monthly intakes: $e');
}
}
}
Future<List<Map<String, dynamic>>> getIntakesForDate(DateTime date) async {
try {
return await _databaseHelper.getIntakesWithSupplementsForDate(date);
} catch (e) {
if (kDebugMode) {
print('Error loading intakes for date: $e');
}
return [];
}
}
Future<void> deleteIntake(int intakeId) async {
try {
await _databaseHelper.deleteIntake(intakeId);
await loadTodayIntakes();
} catch (e) {
if (kDebugMode) {
print('Error deleting intake: $e');
}
}
}
}