Skip to content

Commit

Permalink
feat: use initialization page and providers (#532)
Browse files Browse the repository at this point in the history
* feat: use initialization page and providers

* chore: build diff

* chore: build diff
  • Loading branch information
riscait authored Dec 27, 2024
1 parent 394afb4 commit d21e28f
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 56 deletions.
3 changes: 3 additions & 0 deletions packages/flutter_app/lib/initialization_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import 'features/user_device/user_device_provider.dart';
import 'package_adaptor/configurator_provider.dart';
import 'package_adaptor/package_info_provider.dart';
import 'util/providers/providers.dart';

Expand All @@ -21,4 +22,6 @@ Future<void> initialization(Ref ref) async {
ref.watch(packageInfoInitializingProvider.future),
ref.watch(userDeviceInitializingProvider.future),
]);
final configurator = ref.watch(configuratorProvider);
await configurator.fetchAndActivate();
}
2 changes: 1 addition & 1 deletion packages/flutter_app/lib/initialization_provider.g.dart

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

4 changes: 4 additions & 0 deletions packages/flutter_app/lib/main_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:themes/themes.dart';

import 'features/theme_selector/theme_selector.dart';
import 'gen/strings.g.dart';
import 'pages/initialization_page.dart';
import 'router/router.dart';
import 'util/providers/scaffold_messenger_key_provider.dart';

Expand All @@ -29,6 +30,9 @@ class MainApp extends ConsumerWidget {
],
supportedLocales: AppLocaleUtils.supportedLocales,
scaffoldMessengerKey: ref.watch(scaffoldMessengerKeyProvider),
builder: (context, child) {
return InitializationPage(onInitialized: (_) => child!);
},
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:altfire_configurator/altfire_configurator.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'configurator_provider.g.dart';

/// Provides a [Configurator] to retrieve parameters using "RemoteConfig".
///
/// It is recommended to call [Configurator.fetchAndActivate] before using
/// the parameters (e.g., at app startup) in order to fetch and activate them
/// for use in the app.
@Riverpod(keepAlive: true)
Configurator configurator(Ref ref) {
return Configurator();
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ part 'package_info_provider.g.dart';

/// Providers that need to initialize asynchronously only once at startup.
@Riverpod(keepAlive: true)
Future<PackageInfo> packageInfoInitializing(Ref ref) async =>
PackageInfo.fromPlatform();
Future<PackageInfo> packageInfoInitializing(Ref ref) async {
return PackageInfo.fromPlatform();
}

/// Provide metadata for the application.
///
Expand Down

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

7 changes: 5 additions & 2 deletions packages/flutter_app/lib/pages/initialization_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../initialization_provider.dart';

/// The first page.
class InitializationPage extends ConsumerWidget {
const InitializationPage({super.key});
const InitializationPage({super.key, required this.onInitialized});

final WidgetBuilder onInitialized;

@override
Widget build(BuildContext context, WidgetRef ref) {
return switch (ref.watch(initializationProvider)) {
AsyncData(isLoading: false) => throw AssertionError(),
AsyncData(isLoading: false) => onInitialized(context),
AsyncError(:final error) => _ErrorPage(
error,
onRetry: () => ref.invalidate(initializationProvider),
Expand Down
17 changes: 0 additions & 17 deletions packages/flutter_app/lib/router/app_routes.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import '../pages/initialization_page.dart';
import '../pages/main/main_page.dart';
import 'branches/branches.dart';

part 'app_routes.g.dart';

@TypedGoRoute<InitializationRoute>(
name: InitializationRoute.name,
path: InitializationRoute.path,
)
class InitializationRoute extends GoRouteData {
const InitializationRoute();

static const String name = '/init';
static const String path = '/init';

@override
Page<void> buildPage(BuildContext context, GoRouterState state) {
return const NoTransitionPage(child: InitializationPage());
}
}

@TypedStatefulShellRoute<MainShellRouteData>(
branches: [
TypedStatefulShellBranch<HomeShellBranchData>(
Expand Down
25 changes: 0 additions & 25 deletions packages/flutter_app/lib/router/app_routes.g.dart

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

5 changes: 0 additions & 5 deletions packages/flutter_app/lib/router/router_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../initialization_provider.dart';
import '../package_adaptor/tracker_provider.dart';
import '../pages/not_found_page/error_page.dart';
import '../util/logger.dart';
Expand All @@ -16,16 +15,12 @@ part 'router_provider.g.dart';

@Riverpod(keepAlive: true)
Raw<GoRouter> router(Ref ref) {
final initialization = ref.watch(initializationProvider);
final tracker = ref.watch(trackerProvider);

late final GoRouter router;
router = GoRouter(
routes: $appRoutes,
redirect: (context, state) {
if (initialization.isLoading || initialization.hasError) {
return InitializationRoute.path;
}
if (state.matchedLocation == '/') {
return HomeRouteData.path;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_app/lib/router/router_provider.g.dart

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

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Mocks generated by Mockito 5.4.4 from annotations
// Mocks generated by Mockito 5.4.5 from annotations
// in flutter_app/test/pages/riverpod_example_page/async_notifier_provider_page/async_notifier_provider_page_test.dart.
// Do not manually edit this file.

Expand All @@ -17,6 +17,7 @@ import 'package:mockito/mockito.dart' as _i1;
// ignore_for_file: deprecated_member_use_from_same_package
// ignore_for_file: implementation_imports
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: must_be_immutable
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Mocks generated by Mockito 5.4.4 from annotations
// Mocks generated by Mockito 5.4.5 from annotations
// in flutter_app/test/util/network_connectivity/connectivity_provider_test.dart.
// Do not manually edit this file.

Expand All @@ -18,6 +18,7 @@ import 'package:mockito/mockito.dart' as _i1;
// ignore_for_file: deprecated_member_use_from_same_package
// ignore_for_file: implementation_imports
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: must_be_immutable
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types
Expand Down

0 comments on commit d21e28f

Please sign in to comment.