Skip to content

Commit

Permalink
refactor(#636): routing modules return functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Dec 20, 2023
1 parent 87fc076 commit 136fce8
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 60 deletions.
64 changes: 48 additions & 16 deletions app/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,68 @@ alias flutter-clean='find . -maxdepth 20 -type f \( -name "*.inject.summary" -o

## Architecture

The app consists of multiple so-called modules. Our main modules correspond to
the direct subfolders of `lib/`.
The app consists of multiple so-called modules. Our main modules (usually app
screens) correspond to the direct subfolders of `lib/`.

### Example Module
Common functions used by modules such as `models`, `widgets`, and `utilities`
are living in `common`. All such functions are exported from
`common/module.dart`.

Structure of `lib/my_module`:
The structure of an example module `lib/my_module` should look as follows:

- `my_module`
- `module.dart`:
- exports everything that is required by other modules
- declares all routes as a const variable (`myModuleRoutes`)
- exports everything that is required by other modules, i.e., page(s) and
possibly the cubit
- declares all routes as functions `myModuleRoute` (see example below)
- may contain initialization code (`initMyModule()`)
- `cubit.dart`: contains `MyModuleCubit` and `MyModuleState`s
- `widgets`:
- `my_widget.dart`: contains `MyWidget` and helpers
- `pages`:
- `my_first.dart`: contains `MyFirstPage` and helpers
- `my_complex`: create a folder for complex pages (e.g., tabbed ones)
- `my_module.dart`: contains `MyModulePage` and helpers
- `my_child_page.dart`: contains
- `my_complex_page`: create a folder for complex pages (e.g., tabbed ones)
- `page.dart`: contains `MyComplexPage`
- `tab_first.dart`: contains `FirstTab` and helpers
- `tab_second.dart`: contains `SecondTab` and helpers
- `utils.dart`: contains utilities used by multiple files in this page
- `utils.dart`: contains utilities used throughout this module
- `submodule_one`
- `submodule_two`

If a single file gets too complex for routes, the `Cubit`, a widget, a page,
etc., you can create a folder with the same name and split the original file
into different files. An example of that is `MyComplexPage` in the file tree
above.
- `cubit.dart`: contains `MyModuleCubit` and `MyModuleState`s (if needed)

Example for `my_module/module.dart`; the page is used as a root page in the tab
router, which is why the empty router `MyModuleRootPage` and adding
`AutoRoute(path: '', page: MyModuleRoute.page)` to children is needed.

```dart
import '../common/module.dart';
// For generated routes
export 'cubit.dart';
export 'pages/my_module.dart';
export 'pages/my_child_page.dart';
export 'pages/my_complex_page/page.dart';
@RoutePage()
class MyModuleRootPage extends AutoRouter {}
AutoRoute myChildRoute() => AutoRoute(
path: 'my_child',
page: MyChildRoute.page,
);
AutoRoute myComplexRoute() => AutoRoute(
path: 'my_complex',
page: MyComplexRoute.page,
);
AutoRoute myModuleRoute({ required List<AutoRoute> children }) => AutoRoute(
path: 'my_module',
page: MyModuleRootRoute.page,
children: [
AutoRoute(path: '', page: MyModuleRoute.page),
...children, // includes myChildRoute() and priva
],
);
```

## Making app icons

Expand Down
24 changes: 13 additions & 11 deletions app/lib/common/routing/router.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// Imports for generated routes
import '../../drug/module.dart';
import '../../drug_selection/module.dart';
import '../../faq/module.dart';
import '../../login/module.dart';
import '../../main/module.dart';
import '../../onboarding/module.dart';

import '../../report/module.dart';
import '../../search/module.dart';
import '../../settings/module.dart';
Expand All @@ -19,14 +17,18 @@ class AppRouter extends _$AppRouter {
RouteType get defaultRouteType => RouteType.adaptive();
@override
List<AutoRoute> get routes => [
drugSelectionRoute,
loginRoute,
onboardingRoute,
mainRoute(children: [
reportRoute,
searchRoute,
settingsRoute,
faqRoute,
]),
drugSelectionRoute(),
loginRoute(),
onboardingRoute(),
mainRoute(
children: [
reportRoute(children: [ geneRoute(), drugRoute() ]),
searchRoute(children: [ drugRoute() ]),
faqRoute(),
moreRoute(
children: [ aboutRoute(), termsRoute(), privacyRoute() ],
),
],
),
];
}
2 changes: 1 addition & 1 deletion app/lib/drug_selection/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import '../common/module.dart';
export 'cubit.dart';
export 'pages/drug_selection.dart';

final drugSelectionRoute = AutoRoute(
AutoRoute drugSelectionRoute() => AutoRoute(
path: '/drugselection',
page: DrugSelectionRoute.page,
);
2 changes: 1 addition & 1 deletion app/lib/faq/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import '../common/module.dart';
// For generated routes
export 'pages/faq.dart';

final faqRoute = AutoRoute(page: FaqRoute.page);
AutoRoute faqRoute() => AutoRoute(page: FaqRoute.page);
2 changes: 1 addition & 1 deletion app/lib/login/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import '../common/module.dart';
export 'cubit.dart';
export 'pages/login.dart';

final loginRoute = AutoRoute(path: '/login', page: LoginRoute.page);
AutoRoute loginRoute() => AutoRoute(path: '/login', page: LoginRoute.page);
2 changes: 1 addition & 1 deletion app/lib/main/pages/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ List<TabRouteDefinition> getTabRoutesDefinition(BuildContext context) {
icon: Icon(Icons.lightbulb_rounded),
),
TabRouteDefinition(
pageRouteInfo: SettingsRoute(),
pageRouteInfo: MoreRoute(),
label: context.l10n.nav_more,
icon: Icon(Icons.more_horiz_rounded),
),
Expand Down
2 changes: 1 addition & 1 deletion app/lib/onboarding/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import '../common/module.dart';
// For generated route
export 'pages/onboarding.dart';

final onboardingRoute = AutoRoute(
AutoRoute onboardingRoute() => AutoRoute(
path: '/onboarding',
page: OnboardingRoute.page,
);
8 changes: 4 additions & 4 deletions app/lib/report/module.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import '../common/module.dart';
import '../drug/module.dart';

// For generated routes
export 'pages/gene.dart';
Expand All @@ -8,12 +7,13 @@ export 'pages/report.dart';
@RoutePage()
class ReportRootPage extends AutoRouter {}

final reportRoute = AutoRoute(
AutoRoute geneRoute() => AutoRoute(page: GeneRoute.page);

AutoRoute reportRoute({ required List<AutoRoute> children }) => AutoRoute(
path: 'report',
page: ReportRootRoute.page,
children: [
AutoRoute(path: '', page: ReportRoute.page),
AutoRoute(page: GeneRoute.page),
drugRoute(),
...children,
],
);
5 changes: 2 additions & 3 deletions app/lib/search/module.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import '../common/module.dart';
import '../drug/module.dart';

// For generated routes
export 'pages/search.dart';

@RoutePage()
class SearchRootPage extends AutoRouter {}

final searchRoute = AutoRoute(
AutoRoute searchRoute({ required List<AutoRoute> children }) => AutoRoute(
path: 'search',
page: SearchRootRoute.page,
children: [
AutoRoute(path: '', page: SearchRoute.page),
drugRoute()
...children,
],
);
26 changes: 16 additions & 10 deletions app/lib/settings/module.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import '../common/module.dart';

// For generated routes
export 'pages/about_us.dart';
export 'pages/privacy_policy.dart';
export 'pages/settings.dart';
export 'pages/terms_and_conditions.dart';
export 'pages/about.dart';
export 'pages/more.dart';
export 'pages/privacy.dart';
export 'pages/terms.dart';

final settingsRoute = AutoRoute(
path: 'settings',
page: SettingsRoute.page,
@RoutePage()
class MoreRootPage extends AutoRouter {}

AutoRoute aboutRoute() => AutoRoute(path: 'about', page: AboutRoute.page);
AutoRoute privacyRoute() => AutoRoute(path: 'privacy', page: PrivacyRoute.page);
AutoRoute termsRoute() => AutoRoute(path: 'terms', page: TermsRoute.page);

AutoRoute moreRoute({ required List<AutoRoute> children }) => AutoRoute(
path: 'more',
page: MoreRootRoute.page,
children: [
AutoRoute(path: 'about', page: AboutUsRoute.page),
AutoRoute(path: 'privacy', page: PrivacyPolicyRoute.page),
AutoRoute(path: 'terms', page: TermsAndConditionsRoute.page),
AutoRoute(path: '', page: MoreRoute.page),
...children,
],
);
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import '../../common/module.dart';

@RoutePage()
class AboutUsPage extends StatelessWidget {
const AboutUsPage({super.key});
class AboutPage extends StatelessWidget {
const AboutPage({super.key});

@override
Widget build(BuildContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import '../../common/module.dart';
import '../utils.dart';

@RoutePage()
class SettingsPage extends StatelessWidget {
const SettingsPage({super.key});
class MorePage extends StatelessWidget {
const MorePage({super.key});

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -48,17 +48,17 @@ class SettingsPage extends StatelessWidget {
ListTile(
title: Text(context.l10n.settings_page_about_us),
trailing: Icon(Icons.chevron_right_rounded),
onTap: () => context.router.push(AboutUsRoute()),
onTap: () => context.router.push(AboutRoute()),
),
ListTile(
title: Text(context.l10n.settings_page_privacy_policy),
trailing: Icon(Icons.chevron_right_rounded),
onTap: () => context.router.push(PrivacyPolicyRoute()),
onTap: () => context.router.push(PrivacyRoute()),
),
ListTile(
title: Text(context.l10n.settings_page_terms_and_conditions),
trailing: Icon(Icons.chevron_right_rounded),
onTap: () => context.router.push(TermsAndConditionsRoute()),
onTap: () => context.router.push(TermsRoute()),
),
Divider(),
ListTile(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import '../../common/module.dart';

@RoutePage()
class PrivacyPolicyPage extends StatelessWidget {
const PrivacyPolicyPage({super.key});
class PrivacyPage extends StatelessWidget {
const PrivacyPage({super.key});

@override
Widget build(BuildContext context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import '../../common/module.dart';

@RoutePage()
class TermsAndConditionsPage extends StatelessWidget {
const TermsAndConditionsPage({super.key});
class TermsPage extends StatelessWidget {
const TermsPage({super.key});

@override
Widget build(BuildContext context) {
Expand Down

0 comments on commit 136fce8

Please sign in to comment.