Skip to content

Commit

Permalink
refactor: migrate to riverpod code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesvedder committed May 27, 2024
1 parent d7498b2 commit cbabee2
Show file tree
Hide file tree
Showing 36 changed files with 3,145 additions and 177 deletions.
4 changes: 3 additions & 1 deletion designer_v2/lib/features/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import 'package:studyu_designer_v2/routing/router.dart';
import 'package:studyu_designer_v2/services/notification_dispatcher.dart';
import 'package:studyu_designer_v2/theme.dart';

import '../localization/locale_state.dart';

final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey = GlobalKey();

/// Entry widget of the app.
Expand Down Expand Up @@ -54,7 +56,7 @@ class _AppContentState extends ConsumerState<AppContent> {
// todo move this into appControllerProvider
WidgetsBinding.instance.addPostFrameCallback((_) async {
// Locale Startup Actions
ref.read(localeStateProvider.notifier).initLocale();
ref.read(localeStateNotifierProvider.notifier).initLocale();
});
}

Expand Down
39 changes: 16 additions & 23 deletions designer_v2/lib/features/app_controller.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:studyu_designer_v2/constants.dart';
import 'package:studyu_designer_v2/features/app_controller_state.dart';
import 'package:studyu_designer_v2/repositories/auth_repository.dart';

part 'app_controller.g.dart';

/// Interface for implementation by any resources that want to bind themselves
/// to the application lifecycle
abstract class IAppDelegate {
Expand All @@ -13,18 +15,24 @@ abstract class IAppDelegate {
typedef _DelegateCallback = Future<bool> Function(IAppDelegate delegate);

/// Main controller that's bound to the top-level application widget's state
class AppController extends StateNotifier<AppControllerState> {
@riverpod
class AppController extends _$AppController {
@override
AppControllerState build() {
_appDelegates = [
/// Register [IAppDelegate]s here for invocation of app lifecycle methods
ref.watch(authRepositoryProvider),
];
return const AppControllerState();
}

/// List of listeners for app lifecycle events registered via Riverpod
final List<IAppDelegate> appDelegates;
late final List<IAppDelegate> _appDelegates;

/// A dummy [Future] used for setting a lower bound on app initialization
/// (so that the splash screen is shown during this time)
late final _delayedFuture = Future.delayed(const Duration(milliseconds: Config.minSplashTime), () => true);

AppController({required this.appDelegates}) : super(const AppControllerState());

get isInitialized => state.status == AppStatus.initialized;

Future<bool> onAppStart() async {
// Forward onAppStart to all registered delegates so that they can
// e.g. read some data from local storage for initialization
Expand All @@ -37,7 +45,7 @@ class AppController extends StateNotifier<AppControllerState> {
Future<bool> _callDelegates(_DelegateCallback function, {withMinDelay = false}) async {
final List<Future<bool>> delegateFutures = [];
// Collect all delegated futures
for (final delegate in appDelegates) {
for (final delegate in _appDelegates) {
final future = function(delegate);
delegateFutures.add(future);
}
Expand All @@ -50,18 +58,3 @@ class AppController extends StateNotifier<AppControllerState> {
return !results.contains(false);
}
}

final appControllerProvider = StateNotifierProvider<AppController, AppControllerState>((ref) {
final appController = AppController(appDelegates: [
/// Register [IAppDelegate]s here for invocation of app lifecycle methods
ref.watch(authRepositoryProvider),
]);
appController.addListener((state) {
print("appController.state updated");
});
ref.onDispose(() {
print("appControllerProvider.DISPOSE");
});
print("appControllerProvider");
return appController;
});
28 changes: 28 additions & 0 deletions designer_v2/lib/features/app_controller.g.dart

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

2 changes: 2 additions & 0 deletions designer_v2/lib/features/app_controller_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class AppControllerState extends Equatable {

final AppStatus status;

get isInitialized => status == AppStatus.initialized;

@override
List<Object?> get props => [status];
}
20 changes: 8 additions & 12 deletions designer_v2/lib/features/design/study_form_controller.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:async';

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:reactive_forms/reactive_forms.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:studyu_core/core.dart';
import 'package:studyu_designer_v2/domain/study.dart';
import 'package:studyu_designer_v2/features/design/enrollment/enrollment_form_controller.dart';
Expand All @@ -24,6 +24,8 @@ import 'package:studyu_designer_v2/repositories/auth_repository.dart';
import 'package:studyu_designer_v2/repositories/study_repository.dart';
import 'package:studyu_designer_v2/routing/router.dart';

part 'study_form_controller.g.dart';

class StudyFormViewModel extends FormViewModel<Study> implements IFormViewModelDelegate<FormViewModel> {
StudyFormViewModel({
required this.router,
Expand Down Expand Up @@ -162,20 +164,14 @@ class StudyFormViewModel extends FormViewModel<Study> implements IFormViewModelD
///
/// Note: This is not safe to use in widgets (or other providers) that are built
/// before the [StudyController]'s [Study] is available (see also: [AsyncValue])
final studyFormViewModelProvider = Provider.autoDispose.family<StudyFormViewModel, StudyID>((ref, studyId) {
print("studyFormViewModelProvider");
@riverpod
StudyFormViewModel studyFormViewModel(StudyFormViewModelRef ref, StudyID studyId) {
print("studyFormViewModel");
final state = ref.watch(studyControllerProvider(studyId));
final formViewModel = StudyFormViewModel(
return StudyFormViewModel(
router: ref.watch(routerProvider),
studyRepository: ref.watch(studyRepositoryProvider),
authRepository: ref.watch(authRepositoryProvider),
formData: state.study.value,
);

ref.onDispose(() {
formViewModel.dispose();
print("studyFormViewModelProvider.DISPOSE");
});

return formViewModel;
});
}
197 changes: 197 additions & 0 deletions designer_v2/lib/features/design/study_form_controller.g.dart

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

Loading

0 comments on commit cbabee2

Please sign in to comment.