90 lines
2.5 KiB
Dart
90 lines
2.5 KiB
Dart
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<HabeichHabit>? habitBox;
|
|
|
|
HabitRepository(this.habitBox);
|
|
|
|
Future<List<HabeichHabit>> getAllHabits() async {
|
|
if (habitBox == null) return [];
|
|
return habitBox!.values.toList().cast<HabeichHabit>();
|
|
}
|
|
|
|
Future<void> addHabit(String id, HabeichHabit habit) async {
|
|
if (habitBox == null) return;
|
|
await habitBox!.put(id, habit);
|
|
}
|
|
|
|
Future<void> updateHabit(String id, HabeichHabit habit) async {
|
|
if (habitBox == null) return;
|
|
await habitBox!.put(id, habit);
|
|
}
|
|
|
|
Future<void> deleteHabit(String habitId) async {
|
|
if (habitBox == null) return;
|
|
await habitBox!.delete(habitId);
|
|
}
|
|
|
|
List<Rabbit> getHabitsForDate(DateTime datum) {
|
|
if (habitBox == null) return [];
|
|
final Myrabbit = habitBox!.values.toList().cast<HabeichHabit>();
|
|
List<Rabbit> 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,
|
|
);
|
|
}
|
|
}
|
|
|
|
|