1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-19 11:43:30 +01:00

refactor: define router in screen file

This commit is contained in:
Rongjian Zhang 2019-12-13 13:13:45 +08:00
parent b816986c59
commit 3802faaf51
16 changed files with 130 additions and 92 deletions

View File

@ -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: [

View File

@ -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;

View File

@ -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'),

View File

@ -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';

View File

@ -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;

View File

@ -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 '''

View File

@ -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;

View File

@ -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;

View File

@ -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

View File

@ -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);
}

21
lib/screens/pull.dart Normal file
View File

@ -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);
}
}

20
lib/screens/pulls.dart Normal file
View File

@ -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);
}
}

View File

@ -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;

View File

@ -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) {

View File

@ -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;

View File

@ -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);
}