Advertisement
harmonyV

GoalsService

Feb 15th, 2025
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.22 KB | None | 0 0
  1. @Riverpod()
  2. GoalsService goalsService(Ref ref) {
  3.   return GoalsService(appDatabase: ref.watch(appDatabaseProvider));
  4. }
  5.  
  6. class GoalsService {
  7.   final AppDatabase appDatabase;
  8.   GoalsService({required this.appDatabase});
  9.  
  10.   Stream<List<GoalObject>> getGoalsWithCompletion() {
  11.     final goalsStream = appDatabase.select(appDatabase.goals).watch();
  12.  
  13.     final completedDaysStream =
  14.         appDatabase.select(appDatabase.completedDays).watch();
  15.  
  16.     final tagRefferencesStream =
  17.         appDatabase.select(appDatabase.tagRefferences).watch();
  18.  
  19.     return Rx.combineLatest3(
  20.       goalsStream,
  21.       completedDaysStream,
  22.       tagRefferencesStream,
  23.       (goalsData, completedDays, tagRefferences) {
  24.         return goalsData.map((goal) {
  25.           final completedDaysList = completedDays
  26.               .where((day) => day.goalId == goal.id)
  27.               .map((day) => day.date)
  28.               .toList();
  29.  
  30.           final tagIds = tagRefferences
  31.               .where((tag) => tag.goalId == goal.id)
  32.               .map((tag) => tag.tagRefferenceId)
  33.               .toList();
  34.  
  35.           return GoalObject(
  36.             goalData: goal,
  37.             completedDays: completedDaysList,
  38.             tagIds: tagIds,
  39.           );
  40.         }).toList();
  41.       },
  42.     );
  43.   }
  44.  
  45.   Future<void> createGoal({
  46.     required GoalCategory category,
  47.     required String title,
  48.     String? details,
  49.     required IList<int> tagIds,
  50.   }) async {
  51.     final goal = GoalsCompanion(
  52.       category: Value(category),
  53.       title: Value(title),
  54.       details: Value(details),
  55.     );
  56.  
  57.     await appDatabase.transaction(
  58.       () async {
  59.         final goalId = await appDatabase.into(appDatabase.goals).insert(goal);
  60.  
  61.         if (tagIds.isNotEmpty) {
  62.           for (final tagId in tagIds) {
  63.             await appDatabase.into(appDatabase.tagRefferences).insert(
  64.                   TagRefferencesCompanion(
  65.                     goalId: Value(goalId),
  66.                     tagRefferenceId: Value(tagId),
  67.                   ),
  68.                 );
  69.           }
  70.         }
  71.       },
  72.     );
  73.   }
  74.  
  75.   Future<void> updateGoal({
  76.     required GoalData goalToUpdate,
  77.     required GoalCategory category,
  78.     required String title,
  79.     String? details,
  80.     required IList<int> tagIds,
  81.   }) async {
  82.     await appDatabase.transaction(() async {
  83.       final goal = goalToUpdate.copyWith(
  84.         category: category,
  85.         title: title,
  86.         details: Value(details),
  87.       );
  88.  
  89.       await appDatabase.update(appDatabase.goals).replace(goal);
  90.  
  91.       if (tagIds.isNotEmpty) {
  92.         await (appDatabase.delete(appDatabase.tagRefferences)
  93.               ..where((t) => t.goalId.equals(goal.id)))
  94.             .go();
  95.  
  96.         for (final tagId in tagIds) {
  97.           await appDatabase.into(appDatabase.tagRefferences).insert(
  98.                 TagRefferencesCompanion(
  99.                   goalId: Value(goal.id),
  100.                   tagRefferenceId: Value(tagId),
  101.                 ),
  102.               );
  103.         }
  104.       }
  105.     });
  106.   }
  107.  
  108.   Future<void> deleteGoal(GoalData goal) async {
  109.     await appDatabase.transaction(() async {
  110.       // Delete all completed days for the goal
  111.       await (appDatabase.delete(appDatabase.completedDays)
  112.             ..where((t) => t.goalId.equals(goal.id)))
  113.           .go();
  114.  
  115.       // Delete all tag refferences for the goal
  116.       await (appDatabase.delete(appDatabase.tagRefferences)
  117.             ..where((t) => t.goalId.equals(goal.id)))
  118.           .go();
  119.  
  120.       // Delete the goal
  121.       await (appDatabase.delete(appDatabase.goals)
  122.             ..where((t) => t.id.equals(goal.id)))
  123.           .go();
  124.     });
  125.   }
  126.  
  127.   Future<void> updateDay(int goalId, DateTime date) async {
  128.     final day = date.withoutTime();
  129.  
  130.     final isCompleted = await (appDatabase.select(appDatabase.completedDays)
  131.           ..where((t) => t.goalId.equals(goalId) & t.date.equals(day))
  132.           ..limit(1))
  133.         .getSingleOrNull();
  134.  
  135.     if (isCompleted != null) {
  136.       await appDatabase.delete(appDatabase.completedDays).delete(isCompleted);
  137.       return;
  138.     }
  139.  
  140.     await appDatabase
  141.         .into(appDatabase.completedDays)
  142.         .insert(CompletedDaysCompanion(
  143.           goalId: Value(goalId),
  144.           date: Value(day),
  145.         ));
  146.   }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement