Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Riverpod()
- GoalsService goalsService(Ref ref) {
- return GoalsService(appDatabase: ref.watch(appDatabaseProvider));
- }
- class GoalsService {
- final AppDatabase appDatabase;
- GoalsService({required this.appDatabase});
- Stream<List<GoalObject>> getGoalsWithCompletion() {
- final goalsStream = appDatabase.select(appDatabase.goals).watch();
- final completedDaysStream =
- appDatabase.select(appDatabase.completedDays).watch();
- final tagRefferencesStream =
- appDatabase.select(appDatabase.tagRefferences).watch();
- return Rx.combineLatest3(
- goalsStream,
- completedDaysStream,
- tagRefferencesStream,
- (goalsData, completedDays, tagRefferences) {
- return goalsData.map((goal) {
- final completedDaysList = completedDays
- .where((day) => day.goalId == goal.id)
- .map((day) => day.date)
- .toList();
- final tagIds = tagRefferences
- .where((tag) => tag.goalId == goal.id)
- .map((tag) => tag.tagRefferenceId)
- .toList();
- return GoalObject(
- goalData: goal,
- completedDays: completedDaysList,
- tagIds: tagIds,
- );
- }).toList();
- },
- );
- }
- Future<void> createGoal({
- required GoalCategory category,
- required String title,
- String? details,
- required IList<int> tagIds,
- }) async {
- final goal = GoalsCompanion(
- category: Value(category),
- title: Value(title),
- details: Value(details),
- );
- await appDatabase.transaction(
- () async {
- final goalId = await appDatabase.into(appDatabase.goals).insert(goal);
- if (tagIds.isNotEmpty) {
- for (final tagId in tagIds) {
- await appDatabase.into(appDatabase.tagRefferences).insert(
- TagRefferencesCompanion(
- goalId: Value(goalId),
- tagRefferenceId: Value(tagId),
- ),
- );
- }
- }
- },
- );
- }
- Future<void> updateGoal({
- required GoalData goalToUpdate,
- required GoalCategory category,
- required String title,
- String? details,
- required IList<int> tagIds,
- }) async {
- await appDatabase.transaction(() async {
- final goal = goalToUpdate.copyWith(
- category: category,
- title: title,
- details: Value(details),
- );
- await appDatabase.update(appDatabase.goals).replace(goal);
- if (tagIds.isNotEmpty) {
- await (appDatabase.delete(appDatabase.tagRefferences)
- ..where((t) => t.goalId.equals(goal.id)))
- .go();
- for (final tagId in tagIds) {
- await appDatabase.into(appDatabase.tagRefferences).insert(
- TagRefferencesCompanion(
- goalId: Value(goal.id),
- tagRefferenceId: Value(tagId),
- ),
- );
- }
- }
- });
- }
- Future<void> deleteGoal(GoalData goal) async {
- await appDatabase.transaction(() async {
- // Delete all completed days for the goal
- await (appDatabase.delete(appDatabase.completedDays)
- ..where((t) => t.goalId.equals(goal.id)))
- .go();
- // Delete all tag refferences for the goal
- await (appDatabase.delete(appDatabase.tagRefferences)
- ..where((t) => t.goalId.equals(goal.id)))
- .go();
- // Delete the goal
- await (appDatabase.delete(appDatabase.goals)
- ..where((t) => t.id.equals(goal.id)))
- .go();
- });
- }
- Future<void> updateDay(int goalId, DateTime date) async {
- final day = date.withoutTime();
- final isCompleted = await (appDatabase.select(appDatabase.completedDays)
- ..where((t) => t.goalId.equals(goalId) & t.date.equals(day))
- ..limit(1))
- .getSingleOrNull();
- if (isCompleted != null) {
- await appDatabase.delete(appDatabase.completedDays).delete(isCompleted);
- return;
- }
- await appDatabase
- .into(appDatabase.completedDays)
- .insert(CompletedDaysCompanion(
- goalId: Value(goalId),
- date: Value(day),
- ));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement