import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:hive_flutter/hive_flutter.dart'; import '../models/user_profile.dart'; import '../models/cycle_entry.dart'; import '../services/cycle_service.dart'; import '../services/sync_service.dart'; /// Provider for the user profile final userProfileProvider = StateNotifierProvider((ref) { return UserProfileNotifier(); }); /// Notifier for the user profile class UserProfileNotifier extends StateNotifier { UserProfileNotifier() : super(null) { _loadProfile(); } void _loadProfile() { final box = Hive.box('user_profile'); state = box.get('current_user'); } Future updateProfile(UserProfile profile) async { final box = Hive.box('user_profile'); await box.put('current_user', profile); state = profile; } Future updateThemeMode(AppThemeMode themeMode) async { if (state != null) { await updateProfile(state!.copyWith(themeMode: themeMode)); } } Future updateAccentColor(String accentColor) async { if (state != null) { await updateProfile(state!.copyWith(accentColor: accentColor)); } } Future updateRelationshipStatus( RelationshipStatus relationshipStatus) async { if (state != null) { await updateProfile( state!.copyWith(relationshipStatus: relationshipStatus)); } } Future clearProfile() async { final box = Hive.box('user_profile'); await box.clear(); state = null; } } /// Provider for cycle entries final cycleEntriesProvider = StateNotifierProvider>((ref) { return CycleEntriesNotifier(); }); /// Notifier for cycle entries class CycleEntriesNotifier extends StateNotifier> { CycleEntriesNotifier() : super([]) { _loadEntries(); syncData(); // Auto-sync on load } void _loadEntries() { final box = Hive.box('cycle_entries'); state = box.values.toList()..sort((a, b) => b.date.compareTo(a.date)); } Future addEntry(CycleEntry entry) async { final box = Hive.box('cycle_entries'); await box.put(entry.id, entry); _loadEntries(); _push(); } Future updateEntry(CycleEntry entry) async { final box = Hive.box('cycle_entries'); await box.put(entry.id, entry); _loadEntries(); _push(); } Future deleteEntry(String id) async { final box = Hive.box('cycle_entries'); await box.delete(id); _loadEntries(); _push(); } Future deleteEntriesForMonth(int year, int month) async { final box = Hive.box('cycle_entries'); final keysToDelete = []; for (var entry in box.values) { if (entry.date.year == year && entry.date.month == month) { keysToDelete.add(entry.key); } } await box.deleteAll(keysToDelete); _loadEntries(); _push(); } Future clearEntries() async { final box = Hive.box('cycle_entries'); await box.clear(); state = []; _push(); } // Sync Logic Future syncData() async { await _pull(); // After pull, we might want to push any local changes not in remote? // For now, simpler consistency: Pull then Push current state? // Or just Pull. Push happens on edit. // Let's just Pull. } Future _push() async { final userBox = Hive.box('user_profile'); final user = userBox.get('current_user'); if (user != null) { await SyncService().pushSyncData(user.id, state); } } Future _pull() async { final userBox = Hive.box('user_profile'); final user = userBox.get('current_user'); if (user == null) return; final remoteEntries = await SyncService().pullSyncData(user.id); if (remoteEntries.isNotEmpty) { final box = Hive.box('cycle_entries'); // Simple merge: Remote wins or Union? // Union: Upsert all. for (var entry in remoteEntries) { await box.put(entry.id, entry); } _loadEntries(); } } // Example data generation removed } /// Computed provider for current cycle info final currentCycleInfoProvider = Provider((ref) { final user = ref.watch(userProfileProvider); final entries = ref.watch(cycleEntriesProvider); return CycleService.calculateCycleInfo(user, entries); });