import 'package:bloc/bloc.dart'; import 'package:Rabbit_Habit/models/habit_sammler_model.dart'; import 'package:Rabbit_Habit/models/new_habit_model.dart'; import 'package:Rabbit_Habit/repo/habit_repository.dart'; import 'package:hive_flutter/hive_flutter.dart'; class NewHabitCubit extends Cubit { NewHabitCubit() : super(const NewHabit(name: '', description: '', iconCodePoint: 57825)); setNewHabitName(String name) { emit(state.copyWith(name: name)); } setNewHabitDescription(String description) { emit(state.copyWith(description: description)); } setNewHabitIconCodePoint(int iconCodePoint) { emit(state.copyWith(iconCodePoint: iconCodePoint)); } Future getHabitToEdit(String id) async { Box box = await Hive.openBox('habits'); try { HabeichHabit? habit = box.get(id); if (habit == null) return; emit( state.copyWith( name: habit.name, description: habit.description, iconCodePoint: habit.icon, ), ); } catch (e) { // add error handling to crashlytics } finally { await box.close(); } } Future addNewHabit( String name, String description, int iconCodePoint, ) async { Box box = await Hive.openBox('habits'); try { final DateTime now = DateTime.now(); await HabitRepository(box).addHabit( now.millisecondsSinceEpoch.toString(), HabeichHabit( name: name, description: description, icon: iconCodePoint, frequency: 'daily', goal: null, streak: 0, onlyOn: [], doneOn: [], createdAt: now, updatedAt: now, ), ); } catch (e) { // add error handling to crashlytics } finally { await box.close(); } } Future updateHabit( String id, String name, String description, int iconCodePoint, ) async { Box box = await Hive.openBox('habits'); try { final DateTime now = DateTime.now(); HabeichHabit prevHabit = box.get(id)!; await HabitRepository(box).updateHabit( id, prevHabit.copyWith( name: name, description: description, icon: iconCodePoint, updatedAt: now, ), ); } catch (e) { // add error handling to crashlytics } finally { await box.close(); } } Future deleteHabit(String id) async { Box box = await Hive.openBox('habits'); try { await HabitRepository(box).deleteHabit(id); } catch (e) { // add error handling to crashlytics } finally { await box.close(); } } }