import 'package:Rabbit_Habit/models/habit_model.dart'; import 'package:hive/hive.dart'; import 'package:Rabbit_Habit/models/habit_sammler_model.dart'; class HabitRepository { final Box? habitBox; HabitRepository(this.habitBox); Future> getAllHabits() async { if (habitBox == null) return []; return habitBox!.values.toList().cast(); } Future addHabit(String id, HabeichHabit habit) async { if (habitBox == null) return; await habitBox!.put(id, habit); } Future updateHabit(String id, HabeichHabit habit) async { if (habitBox == null) return; await habitBox!.put(id, habit); } Future deleteHabit(String habitId) async { if (habitBox == null) return; await habitBox!.delete(habitId); } List getHabitsForDate(DateTime datum) { if (habitBox == null) return []; final Myrabbit = habitBox!.values.toList().cast(); List habits = []; for (int i = 0; i < Myrabbit.length; i++) { if (Myrabbit[i].onlyOn.contains(datum.weekday) || Myrabbit[i].onlyOn.isEmpty) { bool isDone = false; for (int j = 0; j < Myrabbit[i].doneOn.length; j++) { if (Myrabbit[i].doneOn[j].day == datum.day && Myrabbit[i].doneOn[j].month == datum.month && Myrabbit[i].doneOn[j].year == datum.year) { isDone = true; } } habits.add( HabitRepository(habitBox) .convertHabitHiveToHabit(Myrabbit[i]) .copyWith( isDone: isDone, ), ); } } return habits; } Rabbit convertHabitHiveToHabit(HabeichHabit einHabit) { return Rabbit( id: einHabit.createdAt.millisecondsSinceEpoch.toString(), name: einHabit.name, description: einHabit.description, icon: einHabit.icon, frequency: einHabit.frequency, goal: einHabit.goal, streak: einHabit.streak, onlyOn: einHabit.onlyOn, doneOn: einHabit.doneOn, isExpanded: false, isDone: false, ); } HabeichHabit convertHabitToHabitHive(Rabbit habit, DateTime now) { return HabeichHabit( name: habit.name, description: habit.description, icon: habit.icon, frequency: habit.frequency, goal: habit.goal, streak: habit.streak, onlyOn: habit.onlyOn, doneOn: habit.doneOn, createdAt: now, updatedAt: now, ); } }