From eac2cc547089064f825d72af96a2f69197a10fb0 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Tue, 19 Feb 2019 21:20:00 +0800 Subject: [PATCH] refactor: extract show actions method --- lib/screens/issue.dart | 52 +++++++++----------------- lib/screens/repo.dart | 83 +++++++++++++++--------------------------- lib/screens/user.dart | 80 ++++++++++++++++------------------------ lib/utils/utils.dart | 48 ++++++++++++++++++++++++ 4 files changed, 126 insertions(+), 137 deletions(-) diff --git a/lib/screens/issue.dart b/lib/screens/issue.dart index b12c4df..9060125 100644 --- a/lib/screens/issue.dart +++ b/lib/screens/issue.dart @@ -336,42 +336,24 @@ __typename Future _openActions(payload) async { if (payload == null) return; - var value = await showCupertinoModalPopup( - context: context, - builder: (BuildContext context) { - return CupertinoActionSheet( - title: Text((isPullRequest ? 'Pull Request' : 'Issue') + ' Actions'), - actions: { - 2: 'Share', - 3: 'Open in Browser', - }.entries.map((entry) { - return CupertinoActionSheetAction( - child: Text(entry.value), - onPressed: () { - Navigator.pop(context, entry.key); - }, - ); - }).toList(), - cancelButton: CupertinoActionSheetAction( - child: const Text('Cancel'), - isDefaultAction: true, - onPressed: () { - Navigator.pop(context); - }, - ), - ); - }, + showActions( + context, + title: (isPullRequest ? 'Pull Request' : 'Issue') + ' Actions', + actions: [ + Action( + text: 'Share', + onPress: () { + Share.share(payload['url']); + }, + ), + Action( + text: 'Open in Browser', + onPress: () { + launch(payload['url']); + }, + ), + ], ); - - switch (value) { - case 2: - Share.share(payload['url']); - break; - case 3: - launch(payload['url']); - break; - default: - } } @override diff --git a/lib/screens/repo.dart b/lib/screens/repo.dart index 2c5b6c5..d07a3c0 100644 --- a/lib/screens/repo.dart +++ b/lib/screens/repo.dart @@ -10,6 +10,7 @@ import '../widgets/repo_item.dart'; import '../widgets/entry_item.dart'; import '../screens/issues.dart'; import '../widgets/link.dart'; +import '../utils/utils.dart'; class RepoScreen extends StatefulWidget { final String owner; @@ -85,59 +86,35 @@ class _RepoScreenState extends State { if (data == null) return; var payload = data[0]; - var _actionMap = { - 0: payload['viewerHasStarred'] ? 'Unstar' : 'Star', - // 1: 'Watch', TODO: - 2: 'Share', - 3: 'Open in Browser', - }; - - var value = await showCupertinoModalPopup( - context: context, - builder: (BuildContext context) { - return CupertinoActionSheet( - title: Text('Repository Actions'), - actions: _actionMap.entries.map((entry) { - return CupertinoActionSheetAction( - child: Text(entry.value), - onPressed: () { - Navigator.pop(context, entry.key); - }, - ); - }).toList(), - cancelButton: CupertinoActionSheetAction( - child: const Text('Cancel'), - isDefaultAction: true, - onPressed: () { - Navigator.pop(context); - }, - ), - ); - }, - ); - - switch (value) { - case 0: - if (payload['viewerHasStarred']) { - await SettingsProvider.of(context) - .deleteWithCredentials('/user/starred/$owner/$name'); - payload['viewerHasStarred'] = false; - } else { - SettingsProvider.of(context) - .putWithCredentials('/user/starred/$owner/$name'); - payload['viewerHasStarred'] = true; - } - break; - // case 1: - // break; - case 2: - Share.share(payload['url']); - break; - case 3: - launch(payload['url']); - break; - default: - } + showActions(context, title: 'Repository Actions', actions: [ + Action( + text: payload['viewerHasStarred'] ? 'Unstar' : 'Star', + onPress: () async { + if (payload['viewerHasStarred']) { + await SettingsProvider.of(context) + .deleteWithCredentials('/user/starred/$owner/$name'); + payload['viewerHasStarred'] = false; + } else { + SettingsProvider.of(context) + .putWithCredentials('/user/starred/$owner/$name'); + payload['viewerHasStarred'] = true; + } + }, + ), + // TODO: watch + Action( + text: 'Share', + onPress: () { + Share.share(payload['url']); + }, + ), + Action( + text: 'Open in Browser', + onPress: () { + launch(payload['url']); + }, + ), + ]); } @override diff --git a/lib/screens/user.dart b/lib/screens/user.dart index 17a3816..177c699 100644 --- a/lib/screens/user.dart +++ b/lib/screens/user.dart @@ -138,59 +138,41 @@ class _UserScreenState extends State { Future _openActions(payload) async { if (payload == null) return; - var _actionMap = {}; + List actions = []; + if (payload['viewerCanFollow']) { - _actionMap[0] = payload['viewerIsFollowing'] ? 'Unfollow' : 'Follow'; + actions.add(Action( + text: payload['viewerIsFollowing'] ? 'Unfollow' : 'Follow', + onPress: () async { + if (payload['viewerIsFollowing']) { + await SettingsProvider.of(context) + .deleteWithCredentials('/user/following/${widget.login}'); + payload['viewerIsFollowing'] = false; + } else { + SettingsProvider.of(context) + .putWithCredentials('/user/following/${widget.login}'); + payload['viewerIsFollowing'] = true; + } + }, + )); } - _actionMap[2] = 'Share'; - _actionMap[3] = 'Open in Browser'; - var value = await showCupertinoModalPopup( - context: context, - builder: (BuildContext context) { - return CupertinoActionSheet( - title: Text('User Actions'), - actions: _actionMap.entries.map((entry) { - return CupertinoActionSheetAction( - child: Text(entry.value), - onPressed: () { - Navigator.pop(context, entry.key); - }, - ); - }).toList(), - cancelButton: CupertinoActionSheetAction( - child: const Text('Cancel'), - isDefaultAction: true, - onPressed: () { - Navigator.pop(context); - }, - ), - ); - }, - ); + actions.addAll([ + Action( + text: 'Share', + onPress: () { + Share.share(payload['url']); + }, + ), + Action( + text: 'Open in Browser', + onPress: () { + launch(payload['url']); + }, + ), + ]); - switch (value) { - case 0: - if (payload['viewerIsFollowing']) { - await SettingsProvider.of(context) - .deleteWithCredentials('/user/following/${widget.login}'); - payload['viewerIsFollowing'] = false; - } else { - SettingsProvider.of(context) - .putWithCredentials('/user/following/${widget.login}'); - payload['viewerIsFollowing'] = true; - } - break; - // case 1: - // break; - case 2: - Share.share(payload['url']); - break; - case 3: - launch(payload['url']); - break; - default: - } + showActions(context, title: 'User Actions', actions: actions); } @override diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index f3acdc0..bbb14ca 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -118,6 +118,54 @@ Future showOptions(BuildContext context, List> options) { } } +class Action { + String text; + Function onPress; + + Action({@required this.text, @required this.onPress}); +} + +Future showActions( + BuildContext context, { + @required String title, + @required List actions, +}) async { + int result; + + switch (SettingsProvider.of(context).theme) { + case ThemeMap.cupertino: + result = await showCupertinoModalPopup( + context: context, + builder: (BuildContext context) { + return CupertinoActionSheet( + title: Text(title), + actions: actions.asMap().entries.map((entry) { + return CupertinoActionSheetAction( + child: Text(entry.value.text), + onPressed: () { + Navigator.pop(context, entry.key); + }, + ); + }).toList(), + cancelButton: CupertinoActionSheetAction( + child: const Text('Cancel'), + isDefaultAction: true, + onPressed: () { + Navigator.pop(context); + }, + ), + ); + }, + ); + break; + default: + } + + if (result != null) { + actions[result].onPress(); + } +} + TextSpan createLinkSpan(BuildContext context, String text, Function handle) { return TextSpan( text: text,