Skip to content

Commit 1b6d2de

Browse files
feat: Introduce DashboardRefreshService to manage dashboard refresh logic and update Nutrition and Workout dashboard cubits to utilize the new service
1 parent 9cc05c1 commit 1b6d2de

4 files changed

Lines changed: 60 additions & 5 deletions

File tree

fitbodyrd_flutter/lib/features/nutrition/presentation/cubits/nutrition_dashboard_cubit.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import 'package:fitbodyrd_client/fitbodyrd_client.dart';
22
import 'package:fitbodyrd_flutter/features/nutrition/domain/repositories/nutrition_repository.dart';
33
import 'package:fitbodyrd_flutter/features/nutrition/presentation/cubits/nutrition_dashboard_state.dart';
4+
import 'package:fitbodyrd_flutter/src/core/services/dashboard_refresh_service.dart';
45
import 'package:flutter_bloc/flutter_bloc.dart';
56
import 'package:record_result/record_result.dart';
67

78
class NutritionDashboardCubit extends Cubit<NutritionDashboardState> {
8-
NutritionDashboardCubit(this.repository) : super(NutritionDashboardInitial());
9+
NutritionDashboardCubit(
10+
this.repository,
11+
this.refreshService,
12+
) : super(NutritionDashboardInitial());
913
final NutritionRepository repository;
14+
final DashboardRefreshService refreshService;
1015

1116
Future<void> loadDashboard() async {
1217
emit(NutritionDashboardLoading());
@@ -201,6 +206,7 @@ class NutritionDashboardCubit extends Cubit<NutritionDashboardState> {
201206
consumedFat: consumed.fat,
202207
),
203208
);
209+
refreshService.refreshDashboard();
204210
}
205211
} else {
206212
final log = currentState.intakeLogs.firstWhere(
@@ -223,6 +229,7 @@ class NutritionDashboardCubit extends Cubit<NutritionDashboardState> {
223229
consumedFat: consumed.fat,
224230
),
225231
);
232+
refreshService.refreshDashboard();
226233
}
227234
}
228235
}

fitbodyrd_flutter/lib/features/workouts/presentation/cubits/workout_dashboard_cubit.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import 'package:equatable/equatable.dart';
22
import 'package:fitbodyrd_client/fitbodyrd_client.dart';
33
import 'package:fitbodyrd_flutter/features/workouts/domain/repositories/workout_repository.dart';
4+
import 'package:fitbodyrd_flutter/src/core/services/dashboard_refresh_service.dart';
45
import 'package:flutter_bloc/flutter_bloc.dart';
56
import 'package:record_result/record_result.dart';
67

78
part 'workout_dashboard_state.dart';
89

910
class WorkoutDashboardCubit extends Cubit<WorkoutDashboardState> {
10-
WorkoutDashboardCubit(this._repository)
11-
: super(const WorkoutDashboardInitial());
11+
WorkoutDashboardCubit(
12+
this._repository,
13+
this._refreshService,
14+
) : super(const WorkoutDashboardInitial());
1215

1316
final WorkoutRepository _repository;
17+
final DashboardRefreshService _refreshService;
1418

1519
Future<void> loadDashboard() async {
1620
emit(const WorkoutDashboardLoading());
@@ -160,6 +164,7 @@ class WorkoutDashboardCubit extends Cubit<WorkoutDashboardState> {
160164
sessionJustCompleted: sessionJustCompleted,
161165
),
162166
);
167+
_refreshService.refreshDashboard();
163168
}
164169

165170
Future<void> deleteLogForExercise(ExerciseLog log) async {
@@ -192,6 +197,7 @@ class WorkoutDashboardCubit extends Cubit<WorkoutDashboardState> {
192197
// In case of failure, the server will surface an AppException that
193198
// can be handled globally (e.g., via a snackbar interceptor).
194199
await _repository.deleteExerciseLog(log.id!);
200+
_refreshService.refreshDashboard();
195201
}
196202

197203
bool _isSessionFullyCompleted(

fitbodyrd_flutter/lib/src/core/injections/injection_container.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import 'package:fitbodyrd_flutter/features/workouts/presentation/cubits/workout_
2424
import 'package:fitbodyrd_flutter/features/workouts/presentation/cubits/workout_generation_cubit.dart';
2525
import 'package:fitbodyrd_flutter/src/core/env/app_env.dart';
2626
import 'package:fitbodyrd_flutter/src/core/routing/app_router.dart';
27+
import 'package:fitbodyrd_flutter/src/core/services/dashboard_refresh_service.dart';
2728
import 'package:fitbodyrd_flutter/src/core/services/device_id_service.dart';
2829
import 'package:fitbodyrd_flutter/src/core/services/preferences_helper.dart';
2930
import 'package:fitbodyrd_flutter/src/core/services/refresh_helper.dart';
@@ -62,6 +63,10 @@ Future<void> initCore() async {
6263
RefreshHelper.new,
6364
dispose: (param) => param.dispose(),
6465
)
66+
..registerLazySingleton(
67+
DashboardRefreshService.new,
68+
dispose: (param) => param.dispose(),
69+
)
6570
..registerLazySingleton(() => getRouter(sl()))
6671
..registerLazySingleton<Client>(
6772
() => Client(
@@ -134,7 +139,10 @@ Future<void> initNutrition() async {
134139
() => NutritionGenerationCubit(nutritionRepository: sl()),
135140
)
136141
..registerLazySingleton(
137-
() => NutritionDashboardCubit(sl<NutritionRepository>()),
142+
() => NutritionDashboardCubit(
143+
sl<NutritionRepository>(),
144+
sl<DashboardRefreshService>(),
145+
),
138146
);
139147
}
140148

@@ -153,7 +161,10 @@ Future<void> initWorkouts() async {
153161
() => ExerciseDetailsCubit(sl<WorkoutRepository>()),
154162
)
155163
..registerLazySingleton(
156-
() => WorkoutDashboardCubit(sl<WorkoutRepository>()),
164+
() => WorkoutDashboardCubit(
165+
sl<WorkoutRepository>(),
166+
sl<DashboardRefreshService>(),
167+
),
157168
);
158169
}
159170

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'dart:async';
2+
3+
import 'package:fitbodyrd_flutter/features/home/presentation/cubits/home_dashboard_cubit.dart';
4+
import 'package:fitbodyrd_flutter/src/core/injections/injection_container.dart';
5+
6+
class DashboardRefreshService {
7+
Timer? _debounceTimer;
8+
static const Duration _debounceDuration = Duration(milliseconds: 1000);
9+
10+
/// Triggers a debounced refresh of the home dashboard.
11+
/// If a refresh is already pending,
12+
/// it cancels the previous one and starts a new timer.
13+
void refreshDashboard() {
14+
// Cancel any existing timer
15+
_debounceTimer?.cancel();
16+
17+
// Start a new debounce timer
18+
_debounceTimer = Timer(_debounceDuration, () {
19+
// Get the HomeDashboardCubit from service locator
20+
// and refresh without showing loading
21+
22+
sl<HomeDashboardCubit>().loadDashboard(showLoading: false).ignore();
23+
});
24+
}
25+
26+
/// Disposes the service and cancels any pending timers.
27+
void dispose() {
28+
_debounceTimer?.cancel();
29+
_debounceTimer = null;
30+
}
31+
}

0 commit comments

Comments
 (0)