Implement data sync and cleanup
This commit is contained in:
@@ -3,8 +3,8 @@ import 'package:hive_flutter/hive_flutter.dart';
|
||||
import '../models/user_profile.dart';
|
||||
import '../models/cycle_entry.dart';
|
||||
|
||||
import 'package:uuid/uuid.dart';
|
||||
import '../services/cycle_service.dart';
|
||||
import '../services/sync_service.dart';
|
||||
|
||||
/// Provider for the user profile
|
||||
final userProfileProvider =
|
||||
@@ -66,6 +66,7 @@ final cycleEntriesProvider =
|
||||
class CycleEntriesNotifier extends StateNotifier<List<CycleEntry>> {
|
||||
CycleEntriesNotifier() : super([]) {
|
||||
_loadEntries();
|
||||
syncData(); // Auto-sync on load
|
||||
}
|
||||
|
||||
void _loadEntries() {
|
||||
@@ -77,18 +78,21 @@ class CycleEntriesNotifier extends StateNotifier<List<CycleEntry>> {
|
||||
final box = Hive.box<CycleEntry>('cycle_entries');
|
||||
await box.put(entry.id, entry);
|
||||
_loadEntries();
|
||||
_push();
|
||||
}
|
||||
|
||||
Future<void> updateEntry(CycleEntry entry) async {
|
||||
final box = Hive.box<CycleEntry>('cycle_entries');
|
||||
await box.put(entry.id, entry);
|
||||
_loadEntries();
|
||||
_push();
|
||||
}
|
||||
|
||||
Future<void> deleteEntry(String id) async {
|
||||
final box = Hive.box<CycleEntry>('cycle_entries');
|
||||
await box.delete(id);
|
||||
_loadEntries();
|
||||
_push();
|
||||
}
|
||||
|
||||
Future<void> deleteEntriesForMonth(int year, int month) async {
|
||||
@@ -101,63 +105,52 @@ class CycleEntriesNotifier extends StateNotifier<List<CycleEntry>> {
|
||||
}
|
||||
await box.deleteAll(keysToDelete);
|
||||
_loadEntries();
|
||||
_push();
|
||||
}
|
||||
|
||||
Future<void> clearEntries() async {
|
||||
final box = Hive.box<CycleEntry>('cycle_entries');
|
||||
await box.clear();
|
||||
state = [];
|
||||
_push();
|
||||
}
|
||||
|
||||
Future<void> generateExampleData(String userId) async {
|
||||
await clearEntries();
|
||||
final box = Hive.box<CycleEntry>('cycle_entries');
|
||||
final today = DateTime.now();
|
||||
// Sync Logic
|
||||
|
||||
// Generate 3 past cycles (~28 days each)
|
||||
DateTime cycleStart = today.subtract(const Duration(days: 84)); // 3 * 28
|
||||
Future<void> 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.
|
||||
}
|
||||
|
||||
while (cycleStart.isBefore(today)) {
|
||||
// Create Period (5 days)
|
||||
for (int i = 0; i < 5; i++) {
|
||||
final date = cycleStart.add(Duration(days: i));
|
||||
if (date.isAfter(today)) break;
|
||||
|
||||
final isHeavy = i == 1 || i == 2;
|
||||
final entry = CycleEntry(
|
||||
id: const Uuid().v4(),
|
||||
date: date,
|
||||
isPeriodDay: true,
|
||||
flowIntensity: isHeavy ? FlowIntensity.heavy : FlowIntensity.medium,
|
||||
mood: i == 1 ? MoodLevel.sad : null,
|
||||
crampIntensity: i == 0 ? 3 : null,
|
||||
hasHeadache: i == 0,
|
||||
createdAt: date,
|
||||
updatedAt: date,
|
||||
);
|
||||
await box.put(entry.id, entry);
|
||||
}
|
||||
|
||||
// Add random ovulation symptoms near day 14
|
||||
final ovulationDay = cycleStart.add(const Duration(days: 14));
|
||||
if (ovulationDay.isBefore(today)) {
|
||||
final entry = CycleEntry(
|
||||
id: const Uuid().v4(),
|
||||
date: ovulationDay,
|
||||
isPeriodDay: false,
|
||||
energyLevel: 4, // High energy
|
||||
mood: MoodLevel.veryHappy,
|
||||
createdAt: ovulationDay,
|
||||
updatedAt: ovulationDay,
|
||||
);
|
||||
await box.put(entry.id, entry);
|
||||
}
|
||||
|
||||
cycleStart = cycleStart.add(const Duration(days: 28));
|
||||
Future<void> _push() async {
|
||||
final userBox = Hive.box<UserProfile>('user_profile');
|
||||
final user = userBox.get('current_user');
|
||||
if (user != null) {
|
||||
await SyncService().pushSyncData(user.id, state);
|
||||
}
|
||||
|
||||
_loadEntries();
|
||||
}
|
||||
|
||||
Future<void> _pull() async {
|
||||
final userBox = Hive.box<UserProfile>('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<CycleEntry>('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
|
||||
|
||||
Reference in New Issue
Block a user