Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:habit_tracker/models/app_settings.dart';
- import 'package:habit_tracker/services/extensions/day.dart';
- import 'package:habit_tracker/models/habit.dart';
- import 'package:habit_tracker/services/lists_enums_maps.dart';
- import 'package:isar/isar.dart';
- import 'package:path_provider/path_provider.dart';
- class HabitDatabase extends ChangeNotifier {
- static late Isar isar;
- // I N I T I A L I Z E - D A T A B A S E
- HabitDatabase() {
- readHabitsFromDatabase();
- }
- static Future<void> initialize() async {
- final directory = await getApplicationCacheDirectory();
- isar = await Isar.open(
- [HabitSchema, AppSettingsSchema],
- directory: directory.path,
- );
- }
- // save first date of app startup (for heatmap)
- static Future<void> saveFirstLaunchDate() async {
- final existingSettings = await isar.appSettings.where().findFirst();
- if (existingSettings == null) {
- final settings = AppSettings()..firstLaunchDate = DateTime.now();
- await isar.writeTxn(() => isar.appSettings.put(settings));
- }
- }
- // get first date of app startup (for heatmap)
- Future<DateTime?> getFirstLaunchDate() async {
- final settings = await isar.appSettings.where().findFirst();
- return settings?.firstLaunchDate;
- }
- final _listOfHabits = <Habit>[];
- List<Habit> get listOfHabits => _listOfHabits;
- final _listOfHabitsInProgress = <Habit>[];
- List<Habit> get listOfHabitsInProgress => _listOfHabitsInProgress;
- final _listOfCompletedHabits = <Habit>[];
- List<Habit> get listOfCompletedHabits => _listOfCompletedHabits;
- var _selection = Selection.inProgress;
- Selection get selection => _selection;
- void setSelection(Selection newSelection) {
- switch (newSelection) {
- case Selection.inProgress:
- _selection = newSelection;
- break;
- case Selection.completed:
- _selection = newSelection;
- break;
- case Selection.overdue:
- _selection = newSelection;
- break;
- default:
- }
- notifyListeners();
- }
- // C R E A T E - add a new habit
- Future<void> addHabit(
- String habitName,
- String? details,
- List<int> itemDataIntKeys,
- HabitCategory habitCategory,
- ) async {
- debugPrint(
- "USED FOR CREATING HABIT; DATA_KEY_STRING_LIST; $itemDataIntKeys");
- debugPrint("USED FOR CREATING HABIT; HABIT_CATEGORY; $habitCategory");
- final newHabit = Habit(name: habitName, category: habitCategory);
- newHabit.details = details;
- newHabit.itemDataIntKeys.addAll(itemDataIntKeys);
- // save to database
- await isar.writeTxn(() => isar.habits.put(newHabit));
- readHabitsFromDatabase();
- }
- // R E A D - read saved habits from database
- Future<void> readHabitsFromDatabase() async {
- final fetchedHabits = await isar.habits.where().findAll();
- _listOfHabits.clear();
- _listOfHabits.addAll(fetchedHabits);
- _listOfHabitsInProgress.clear();
- _listOfCompletedHabits.clear();
- // check each habit in _listOfHabits for todays completion status
- for (final habitToCheck in _listOfHabits) {
- debugPrint("${habitToCheck.completedDays.contains(Day.now())}");
- // if habit is NOT completed today => add it to _listOfHabitsInProgress
- if (!habitToCheck.completedDays.contains(Day.now())) {
- _listOfHabitsInProgress.add(habitToCheck);
- }
- // if habit IS completed today => add it to the _listOfCompletedHabits
- else if (habitToCheck.completedDays.contains(Day.now())) {
- _listOfCompletedHabits.add(habitToCheck);
- }
- }
- setSelection(_selection);
- notifyListeners();
- }
- // U P D A T E - check habit on and off
- Future<void> updateHabitCompletion(int id, bool isCompleted) async {
- // find the specific habit
- final habit = await isar.habits.get(id);
- // update completion status
- if (habit != null) {
- await isar.writeTxn(() async {
- // if habit is completed => add the current date to the completedDays list
- if (isCompleted && !habit.completedDays.contains(Day.now())) {
- // addd the current date if it's not already in the list
- habit.completedDays.checkDay(Day.now());
- }
- // if habit is NOT completed => remove current date from the completedDays list
- else {
- // remove the current date if the habit is marked as NOT completed
- habit.completedDays.uncheckDay(Day.now());
- //habit.completedDays.removeWhere((date) => date == Day.now());
- }
- // save the updated habits back to the database
- await isar.habits.put(habit);
- });
- }
- await readHabitsFromDatabase();
- }
- // U P D A T E - edit habit name
- Future<void> editHabit(
- int id,
- String newName,
- String? newDetails,
- List<int> newitemDataIntKeys,
- HabitCategory newHabitCategory,
- ) async {
- // find the specific habit
- final habit = await isar.habits.get(id);
- // update habit name, itemKeyStringsList, category
- if (habit != null) {
- await isar.writeTxn(() async {
- habit.name = newName;
- habit.details = newDetails;
- habit.itemDataIntKeys = [];
- habit.itemDataIntKeys.addAll(newitemDataIntKeys);
- habit.category = newHabitCategory;
- // save updated habit back to database
- await isar.habits.put(habit);
- });
- }
- readHabitsFromDatabase();
- }
- // D E L E T E
- Future<void> deleteHabit(int id) async {
- await isar.writeTxn(() async {
- await isar.habits.delete(id);
- });
- readHabitsFromDatabase();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement