Skip to content

Commit

Permalink
refactor: migrated dashboard_controller.dart to riverpod v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimozkn committed Jun 12, 2024
1 parent 6d1e731 commit 4d8001e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 35 deletions.
66 changes: 31 additions & 35 deletions designer_v2/lib/features/dashboard/dashboard_controller.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:studyu_core/core.dart';
import 'package:studyu_designer_v2/common_views/search.dart';
import 'package:studyu_designer_v2/domain/study.dart';
Expand All @@ -17,29 +18,44 @@ import 'package:studyu_designer_v2/utils/model_action.dart';

import 'dashboard_state.dart';

class DashboardController extends StateNotifier<DashboardState> implements IModelActionProvider<Study> {
part 'dashboard_controller.g.dart';

@riverpod
class DashboardController extends _$DashboardController implements IModelActionProvider<Study> {

@override
DashboardState build(){
studyRepository = ref.watch(studyRepositoryProvider);
authRepository = ref.watch(authRepositoryProvider);
userRepository = ref.watch(userRepositoryProvider);
router = ref.watch(routerProvider);

ref.onDispose(() {
print("dashboardControllerProvider.DISPOSE");
_studiesSubscription?.cancel();
});

ref.listenSelf((previous, next) {
print("dashboardController.state updated");
});

_subscribeStudies();
return DashboardState(currentUser: authRepository.currentUser!);
}

/// References to the data repositories injected by Riverpod
final IStudyRepository studyRepository;
final IAuthRepository authRepository;
final IUserRepository userRepository;
late final IStudyRepository studyRepository;
late final IAuthRepository authRepository;
late final IUserRepository userRepository;

/// Reference to services injected via Riverpod
final GoRouter router;
late final GoRouter router;

/// A subscription for synchronizing state between the repository and the controller
StreamSubscription<List<WrappedModel<Study>>>? _studiesSubscription;

final SearchController searchController = SearchController();

DashboardController({
required this.studyRepository,
required this.authRepository,
required this.userRepository,
required this.router,
}) : super(DashboardState(currentUser: authRepository.currentUser!)) {
_subscribeStudies();
}

_subscribeStudies() {
_studiesSubscription = studyRepository.watchAll().listen((wrappedModels) {
print("studyRepository.update");
Expand Down Expand Up @@ -135,25 +151,5 @@ class DashboardController extends StateNotifier<DashboardState> implements IMode
);
}

@override
dispose() {
_studiesSubscription?.cancel();
super.dispose();
}
}

final dashboardControllerProvider = StateNotifierProvider.autoDispose<DashboardController, DashboardState>((ref) {
final dashboardController = DashboardController(
studyRepository: ref.watch(studyRepositoryProvider),
authRepository: ref.watch(authRepositoryProvider),
userRepository: ref.watch(userRepositoryProvider),
router: ref.watch(routerProvider),
);
dashboardController.addListener((state) {
print("dashboardController.state updated");
});
ref.onDispose(() {
print("dashboardControllerProvider.DISPOSE");
});
return dashboardController;
});
}
27 changes: 27 additions & 0 deletions designer_v2/lib/features/dashboard/dashboard_controller.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4d8001e

Please sign in to comment.