Quit-Your-Addictions/lib/blocs/selected_day_cubit.dart

81 lines
2.3 KiB
Dart

import 'package:bloc/bloc.dart';
import 'package:Rabbit_Habit/models/habit_sammler_model.dart';
import 'package:Rabbit_Habit/models/habit_model.dart';
import 'package:Rabbit_Habit/models/selected_day_model.dart';
import 'package:Rabbit_Habit/repo/habit_repository.dart';
import 'package:hive_flutter/hive_flutter.dart';
class SelectedDayCubit extends Cubit<SelectedDay> {
SelectedDayCubit()
: super(SelectedDay(selectedDay: DateTime.now(), habits: [])) {
changeSelectedDay(
DateTime.utc(
DateTime.now().year,
DateTime.now().month,
DateTime.now().day,
),
);
}
Future<void> changeSelectedDay(DateTime newDay) async {
if (newDay == state.selectedDay) return;
List<Rabbit> habits = [];
Box<HabeichHabit> box = await Hive.openBox('habits');
try {
habits = HabitRepository(box).getHabitsForDate(newDay);
} catch (e) {
// add error handling to crashlytics
} finally {
await box.close();
}
emit(state.copyWith(selectedDay: newDay, habits: habits));
}
void changeHabitExpanded(int index) {
List<Rabbit> newHabits = state.habits.toList();
Rabbit habit =
newHabits[index].copyWith(isExpanded: !newHabits[index].isExpanded);
newHabits[index] = habit;
if (newHabits != state.habits) {
emit(state.copyWith(habits: newHabits));
}
}
Future<void> changeHabitDone(int index) async {
List<Rabbit> newHabits = state.habits.toList();
Rabbit habit = newHabits[index].copyWith(
isDone: !newHabits[index].isDone,
);
Box<HabeichHabit> box = await Hive.openBox('habits');
try {
HabeichHabit? oldHabit = box.get(habit.id);
if (oldHabit == null) return;
List<DateTime> doneOn = oldHabit.doneOn.toList();
if (habit.isDone) {
doneOn.add(
state.selectedDay,
);
} else {
doneOn.remove(state.selectedDay);
}
await HabitRepository(box).updateHabit(
habit.id,
oldHabit.copyWith(
doneOn: doneOn,
updatedAt: DateTime.now(),
),
);
habit = habit.copyWith(
doneOn: doneOn,
);
newHabits[index] = habit;
} catch (e) {
// add error handling to crashlytics
} finally {
await box.close();
}
if (newHabits != state.habits) {
emit(state.copyWith(habits: newHabits));
}
}
}