From 3802faaf518ccd2e7ef4d186c092f518a03d8ce5 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Fri, 13 Dec 2019 13:13:45 +0800 Subject: [PATCH] refactor: define router in screen file --- lib/main.dart | 107 +++++++--------------------------- lib/screens/commits.dart | 5 ++ lib/screens/credits.dart | 3 + lib/screens/gitlab_blob.dart | 1 - lib/screens/gitlab_issue.dart | 5 ++ lib/screens/issue.dart | 5 ++ lib/screens/issue_form.dart | 5 ++ lib/screens/issues.dart | 7 ++- lib/screens/login.dart | 4 +- lib/screens/object.dart | 14 ++++- lib/screens/pull.dart | 21 +++++++ lib/screens/pulls.dart | 20 +++++++ lib/screens/repository.dart | 9 +++ lib/screens/settings.dart | 6 +- lib/screens/user.dart | 3 + lib/utils/utils.dart | 7 +++ 16 files changed, 130 insertions(+), 92 deletions(-) create mode 100644 lib/screens/pull.dart create mode 100644 lib/screens/pulls.dart diff --git a/lib/main.dart b/lib/main.dart index 3925455..a03e2c2 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -12,6 +12,8 @@ import 'package:git_touch/screens/issue_form.dart'; import 'package:git_touch/screens/issues.dart'; import 'package:git_touch/screens/notification.dart'; import 'package:git_touch/screens/object.dart'; +import 'package:git_touch/screens/pull.dart'; +import 'package:git_touch/screens/pulls.dart'; import 'package:git_touch/screens/repository.dart'; import 'package:git_touch/screens/repositories.dart'; import 'package:git_touch/screens/settings.dart'; @@ -230,93 +232,26 @@ void main() async { codeModel.init(), ]); - // github - themeModel.router.define('/login', handler: Handler( - handlerFunc: (context, params) { - return LoginScreen(); - }, - )); - themeModel.router.define('/settings', handler: Handler( - handlerFunc: (context, params) { - return SettingsScreen(); - }, - )); - themeModel.router.define('/help/credits', handler: Handler( - handlerFunc: (context, params) { - return CreditsScreen(); - }, - )); - themeModel.router.define('/:login', handler: Handler( - handlerFunc: (context, params) { - return UserScreen(params['login'].first); - }, - )); - themeModel.router.define('/:owner/:name', handler: Handler( - handlerFunc: (context, params) { - if (params['ref'] == null) { - return RepositoryScreen(params['owner'].first, params['name'].first); - } else { - return RepositoryScreen(params['owner'].first, params['name'].first, - branch: params['ref'].first); - } - }, - )); - themeModel.router.define('/:owner/:name/issues', handler: Handler( - handlerFunc: (context, params) { - return IssuesScreen(params['owner'].first, params['name'].first); - }, - )); - themeModel.router.define('/:owner/:name/pulls', handler: Handler( - handlerFunc: (context, params) { - return IssuesScreen(params['owner'].first, params['name'].first, - isPullRequest: true); - }, - )); - themeModel.router.define('/:owner/:name/issues/:number', handler: Handler( - handlerFunc: (context, params) { - return IssueScreen(params['owner'].first, params['name'].first, - int.parse(params['number'].first)); - }, - )); - themeModel.router.define('/:owner/:name/pulls/:number', handler: Handler( - handlerFunc: (context, params) { - return IssueScreen( - params['owner'].first, - params['name'].first, - int.parse(params['number'].first), - isPullRequest: true, - ); - }, - )); - themeModel.router.define('/:owner/:name/commits', handler: Handler( - handlerFunc: (context, params) { - return CommitsScreen(params['owner'].first, params['name'].first); - }, - )); - themeModel.router.define('/:owner/:name/blob/:ref', handler: Handler( - handlerFunc: (context, params) { - final pathParam = params['path']; - return ObjectScreen( - params['owner'].first, - params['name'].first, - params['ref'].first, - paths: pathParam?.first?.urldecode?.split('/') ?? [], - ); - }, - )); - themeModel.router.define('/:owner/:name/issues/new', handler: Handler( - handlerFunc: (context, params) { - return IssueFormScreen(params['owner'].first, params['name'].first); - }, - )); + final routers = [ + loginRouter, + settingsRouter, + creditsRouter, + userRouter, + repositoryRouter, + issuesRouter, + pullsRouter, + issueRouter, + pullRouter, + commitsRouter, + objectRouter, + issueAddRouter, + gitlabIssueRouter + ]; - // gitlab - themeModel.router.define('/project/:id/issue/:iid', handler: Handler( - handlerFunc: (context, params) { - return GitlabIssueScreen( - int.parse(['id'].first), int.parse(params['iid'].first)); - }, - )); + routers.forEach((screen) { + themeModel.router + .define(screen.path, handler: Handler(handlerFunc: screen.handler)); + }); runApp(MultiProvider( providers: [ diff --git a/lib/screens/commits.dart b/lib/screens/commits.dart index 7e962c5..27b9fed 100644 --- a/lib/screens/commits.dart +++ b/lib/screens/commits.dart @@ -9,6 +9,11 @@ import 'package:timeago/timeago.dart' as timeago; import 'package:git_touch/widgets/avatar.dart'; import 'package:primer/primer.dart'; +final commitsRouter = RouterScreen( + '/:owner/:name/commits', + (context, params) => + CommitsScreen(params['owner'].first, params['name'].first)); + class CommitsScreen extends StatelessWidget { final String owner; final String name; diff --git a/lib/screens/credits.dart b/lib/screens/credits.dart index 4478cf6..cf3a1a2 100644 --- a/lib/screens/credits.dart +++ b/lib/screens/credits.dart @@ -4,6 +4,9 @@ import 'package:git_touch/utils/utils.dart'; import 'package:git_touch/widgets/table_view.dart'; import 'package:tuple/tuple.dart'; +final creditsRouter = + RouterScreen('/help/credits', (context, parameters) => CreditsScreen()); + class CreditsScreen extends StatelessWidget { static const projects = [ Tuple2('flutter', 'flutter/flutter'), diff --git a/lib/screens/gitlab_blob.dart b/lib/screens/gitlab_blob.dart index aa8fcbe..af0c909 100644 --- a/lib/screens/gitlab_blob.dart +++ b/lib/screens/gitlab_blob.dart @@ -1,5 +1,4 @@ import 'dart:convert'; - import 'package:flutter/material.dart'; import 'package:git_touch/models/auth.dart'; import 'package:git_touch/models/gitlab.dart'; diff --git a/lib/screens/gitlab_issue.dart b/lib/screens/gitlab_issue.dart index 503b0b9..8a40231 100644 --- a/lib/screens/gitlab_issue.dart +++ b/lib/screens/gitlab_issue.dart @@ -9,6 +9,11 @@ import 'package:git_touch/models/auth.dart'; import 'package:timeago/timeago.dart' as timeago; import 'package:tuple/tuple.dart'; +final gitlabIssueRouter = RouterScreen( + '/project/:id/issue/:iid', + (context, params) => GitlabIssueScreen( + int.parse(['id'].first), int.parse(params['iid'].first))); + class GitlabIssueScreen extends StatelessWidget { final int projectId; final int iid; diff --git a/lib/screens/issue.dart b/lib/screens/issue.dart index a9906fd..e6d919c 100644 --- a/lib/screens/issue.dart +++ b/lib/screens/issue.dart @@ -9,6 +9,11 @@ import '../scaffolds/long_list.dart'; import '../widgets/timeline_item.dart'; import '../widgets/comment_item.dart'; +final issueRouter = RouterScreen( + '/:owner/:name/issues/:number', + (context, params) => IssueScreen(params['owner'].first, + params['name'].first, int.parse(params['number'].first))); + final reactionChunk = emojiMap.entries.map((entry) { var key = entry.key; return ''' diff --git a/lib/screens/issue_form.dart b/lib/screens/issue_form.dart index 21cc818..5c77adb 100644 --- a/lib/screens/issue_form.dart +++ b/lib/screens/issue_form.dart @@ -9,6 +9,11 @@ import 'package:git_touch/scaffolds/common.dart'; import 'package:git_touch/utils/utils.dart'; import 'package:provider/provider.dart'; +final issueAddRouter = RouterScreen( + '/:owner/:name/issues/new', + (context, params) => + IssueFormScreen(params['owner'].first, params['name'].first)); + class IssueFormScreen extends StatefulWidget { final String owner; final String name; diff --git a/lib/screens/issues.dart b/lib/screens/issues.dart index 828eca9..aba9fc3 100644 --- a/lib/screens/issues.dart +++ b/lib/screens/issues.dart @@ -2,11 +2,16 @@ import 'package:flutter/material.dart'; import 'package:git_touch/models/auth.dart'; import 'package:git_touch/models/theme.dart'; import 'package:git_touch/scaffolds/list_stateful.dart'; +import 'package:git_touch/utils/utils.dart'; import 'package:git_touch/widgets/action_entry.dart'; import 'package:git_touch/widgets/app_bar_title.dart'; import 'package:git_touch/widgets/issue_item.dart'; import 'package:provider/provider.dart'; -import '../utils/utils.dart'; + +final issuesRouter = RouterScreen( + '/:owner/:name/issues', + (context, params) => + IssuesScreen(params['owner'].first, params['name'].first)); class IssuesScreen extends StatelessWidget { final String owner; diff --git a/lib/screens/login.dart b/lib/screens/login.dart index 7ccdbc9..7a1d4a2 100644 --- a/lib/screens/login.dart +++ b/lib/screens/login.dart @@ -9,7 +9,9 @@ import 'package:provider/provider.dart'; import '../widgets/link.dart'; import '../widgets/loading.dart'; import '../widgets/avatar.dart'; -// import 'login_gitlab.dart'; + +final loginRouter = + RouterScreen('/login', (context, parameters) => LoginScreen()); class LoginScreen extends StatefulWidget { @override diff --git a/lib/screens/object.dart b/lib/screens/object.dart index 6f5fb34..72ea49f 100644 --- a/lib/screens/object.dart +++ b/lib/screens/object.dart @@ -9,7 +9,7 @@ import 'package:git_touch/widgets/action_entry.dart'; import 'package:git_touch/widgets/app_bar_title.dart'; import 'package:git_touch/widgets/markdown_view.dart'; import 'package:git_touch/widgets/table_view.dart'; -import 'package:path/path.dart' as path; +import 'package:path/path.dart' as p; import 'package:flutter/material.dart'; import 'package:flutter_highlight/flutter_highlight.dart'; import 'package:git_touch/models/auth.dart'; @@ -19,6 +19,16 @@ import 'package:git_touch/utils/utils.dart'; import 'package:primer/primer.dart'; import 'package:seti/seti.dart'; +final objectRouter = RouterScreen( + '/:owner/:name/blob/:ref', + (context, params) => ObjectScreen( + params['owner'].first, + params['name'].first, + params['ref'].first, + paths: params['path']?.first?.urldecode?.split('/') ?? [], + ), +); + class ObjectScreen extends StatelessWidget { final String owner; final String name; @@ -30,7 +40,7 @@ class ObjectScreen extends StatelessWidget { String get _expression => '$branch:$_path'; String get _extname { if (paths.isEmpty) return ''; - var dotext = path.extension(paths.last); + var dotext = p.extension(paths.last); if (dotext.isEmpty) return ''; return dotext.substring(1); } diff --git a/lib/screens/pull.dart b/lib/screens/pull.dart new file mode 100644 index 0000000..14dec6d --- /dev/null +++ b/lib/screens/pull.dart @@ -0,0 +1,21 @@ +import 'package:flutter/material.dart'; +import 'package:git_touch/screens/issue.dart'; +import 'package:git_touch/utils/utils.dart'; + +final pullRouter = RouterScreen( + '/:owner/:name/pulls/:number', + (context, params) => PullScreen(params['owner'].first, params['name'].first, + int.parse(params['number'].first))); + +class PullScreen extends StatelessWidget { + final String owner; + final String name; + final int number; + + PullScreen(this.owner, this.name, this.number); + + @override + Widget build(BuildContext context) { + return IssueScreen(owner, name, number, isPullRequest: true); + } +} diff --git a/lib/screens/pulls.dart b/lib/screens/pulls.dart new file mode 100644 index 0000000..0e5a039 --- /dev/null +++ b/lib/screens/pulls.dart @@ -0,0 +1,20 @@ +import 'package:flutter/material.dart'; +import 'package:git_touch/screens/issues.dart'; +import 'package:git_touch/utils/utils.dart'; + +final pullsRouter = RouterScreen( + '/:owner/:name/pulls', + (context, params) => + PullsScreen(params['owner'].first, params['name'].first)); + +class PullsScreen extends StatelessWidget { + final String owner; + final String name; + + PullsScreen(this.owner, this.name); + + @override + Widget build(BuildContext context) { + return IssuesScreen(owner, name, isPullRequest: true); + } +} diff --git a/lib/screens/repository.dart b/lib/screens/repository.dart index 72b1224..2fd698e 100644 --- a/lib/screens/repository.dart +++ b/lib/screens/repository.dart @@ -17,6 +17,15 @@ import 'package:tuple/tuple.dart'; import '../widgets/entry_item.dart'; import 'package:git_touch/widgets/action_button.dart'; +final repositoryRouter = RouterScreen('/:owner/:name', (context, params) { + if (params['ref'] == null) { + return RepositoryScreen(params['owner'].first, params['name'].first); + } else { + return RepositoryScreen(params['owner'].first, params['name'].first, + branch: params['ref'].first); + } +}); + class RepositoryScreen extends StatelessWidget { final String owner; final String name; diff --git a/lib/screens/settings.dart b/lib/screens/settings.dart index d11ffd1..20793f0 100644 --- a/lib/screens/settings.dart +++ b/lib/screens/settings.dart @@ -7,7 +7,11 @@ import 'package:git_touch/widgets/app_bar_title.dart'; import 'package:provider/provider.dart'; import 'package:launch_review/launch_review.dart'; import '../widgets/table_view.dart'; -import '../screens/login.dart'; + +final settingsRouter = RouterScreen( + '/settings', + (context, parameters) => SettingsScreen(), +); class SettingsScreen extends StatelessWidget { Widget _buildRightWidget(bool checked) { diff --git a/lib/screens/user.dart b/lib/screens/user.dart index c4c1679..5253d6d 100644 --- a/lib/screens/user.dart +++ b/lib/screens/user.dart @@ -20,6 +20,9 @@ import 'package:provider/provider.dart'; import 'package:git_touch/widgets/action_button.dart'; import 'package:tuple/tuple.dart'; +final userRouter = RouterScreen( + '/:login', (context, parameters) => UserScreen(parameters['login'].first)); + class UserScreen extends StatelessWidget { final String login; diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index f4bcfd3..a2fd8f6 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -1,4 +1,5 @@ import 'dart:io'; +import 'package:fluro/fluro.dart'; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/gestures.dart'; @@ -177,3 +178,9 @@ launchUrl(String url) async { // TODO: fallback } } + +class RouterScreen { + String path; + HandlerFunc handler; + RouterScreen(this.path, this.handler); +}