From 9c4cd738149ab5120f870538eb2284154e0dfd91 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Mon, 30 Sep 2019 15:46:06 +0800 Subject: [PATCH] refactor: action items --- lib/screens/issue.dart | 19 +++++------------- lib/screens/organization.dart | 23 +++++----------------- lib/screens/repository.dart | 29 ++++++++-------------------- lib/screens/user.dart | 25 ++++++------------------ lib/widgets/action.dart | 36 +++++++++++++++++++++-------------- 5 files changed, 46 insertions(+), 86 deletions(-) diff --git a/lib/screens/issue.dart b/lib/screens/issue.dart index 422c7d0..e051391 100644 --- a/lib/screens/issue.dart +++ b/lib/screens/issue.dart @@ -3,7 +3,6 @@ import 'package:flutter/cupertino.dart'; import 'package:git_touch/models/auth.dart'; import 'package:primer/primer.dart'; import 'package:provider/provider.dart'; -import 'package:share/share.dart'; import '../utils/utils.dart'; import '../scaffolds/long_list.dart'; import '../widgets/timeline_item.dart'; @@ -343,19 +342,11 @@ mutation { trailingBuilder: (payload) { return ActionButton( title: (isPullRequest ? 'Pull Request' : 'Issue') + ' Actions', - actions: [ - MyAction( - text: 'Share', - onPress: () { - Share.share(payload['url']); - }, - ), - MyAction( - text: 'Open in Browser', - onPress: () { - launchUrl(payload['url']); - }, - ), + items: [ + if (payload != null) ...[ + ActionItem.share(payload['url']), + ActionItem.launch(payload['url']), + ], ], ); }, diff --git a/lib/screens/organization.dart b/lib/screens/organization.dart index 07816d7..3e4b043 100644 --- a/lib/screens/organization.dart +++ b/lib/screens/organization.dart @@ -8,7 +8,6 @@ import 'package:git_touch/widgets/entry_item.dart'; import 'package:git_touch/widgets/repository_item.dart'; import 'package:git_touch/widgets/table_view.dart'; import 'package:git_touch/widgets/user_item.dart'; -import 'package:share/share.dart'; import 'package:git_touch/models/auth.dart'; import 'package:provider/provider.dart'; import '../widgets/action.dart'; @@ -93,23 +92,11 @@ class OrganizationScreen extends StatelessWidget { trailingBuilder: (payload) { return ActionButton( title: 'Organization Actions', - actions: [ - MyAction( - text: 'Share', - onPress: () { - if (payload != null) { - Share.share(payload['url']); - } - }, - ), - MyAction( - text: 'Open in Browser', - onPress: () { - if (payload != null) { - launchUrl(payload['url']); - } - }, - ), + items: [ + if (payload != null) ...[ + ActionItem.share(payload['url']), + ActionItem.launch(payload['url']), + ], ], ); }, diff --git a/lib/screens/repository.dart b/lib/screens/repository.dart index 50c1aca..e9e22cf 100644 --- a/lib/screens/repository.dart +++ b/lib/screens/repository.dart @@ -13,7 +13,6 @@ import 'package:provider/provider.dart'; import 'package:git_touch/models/theme.dart'; import 'package:git_touch/screens/commits.dart'; import 'package:git_touch/screens/object.dart'; -import 'package:share/share.dart'; import 'package:git_touch/widgets/repository_item.dart'; import '../widgets/entry_item.dart'; import '../screens/issues.dart'; @@ -149,8 +148,8 @@ class RepositoryScreen extends StatelessWidget { trailingBuilder: (data) { return ActionButton( title: 'Repository Actions', - actions: [ - MyAction( + items: [ + ActionItem( text: '@$owner', onPress: () { if (data == null) return; @@ -169,7 +168,7 @@ class RepositoryScreen extends StatelessWidget { }, ), if (data != null) ...[ - MyAction( + ActionItem( text: data[0]['viewerHasStarred'] ? 'Unstar' : 'Star', onPress: () async { if (data[0]['viewerHasStarred']) { @@ -183,7 +182,7 @@ class RepositoryScreen extends StatelessWidget { } }, ), - MyAction( + ActionItem( text: data[0]['viewerSubscription'] == 'SUBSCRIBED' ? 'Unwatch' : 'Watch', @@ -200,22 +199,10 @@ class RepositoryScreen extends StatelessWidget { }, ), ], - MyAction( - text: 'Share', - onPress: () { - if (data != null) { - Share.share(data[0]['url']); - } - }, - ), - MyAction( - text: 'Open in Browser', - onPress: () { - if (data != null) { - launchUrl(data[0]['url']); - } - }, - ), + if (data != null) ...[ + ActionItem.share(data[0]['url']), + ActionItem.launch(data[0]['url']), + ], ], ); }, diff --git a/lib/screens/user.dart b/lib/screens/user.dart index 5edeeb0..cba406e 100644 --- a/lib/screens/user.dart +++ b/lib/screens/user.dart @@ -9,7 +9,6 @@ import 'package:git_touch/widgets/table_view.dart'; import 'package:git_touch/widgets/text_contains_organization.dart'; import 'package:git_touch/widgets/user_item.dart'; import 'package:primer/primer.dart'; -import 'package:share/share.dart'; import 'package:github_contributions/github_contributions.dart'; import 'package:git_touch/models/auth.dart'; import 'package:provider/provider.dart'; @@ -161,9 +160,9 @@ class UserScreen extends StatelessWidget { } else { return ActionButton( title: 'User Actions', - actions: [ + items: [ if (data != null && data[0]['viewerCanFollow']) - MyAction( + ActionItem( text: data[0]['viewerIsFollowing'] ? 'Unfollow' : 'Follow', onPress: () async { if (data[0]['viewerIsFollowing']) { @@ -177,22 +176,10 @@ class UserScreen extends StatelessWidget { } }, ), - MyAction( - text: 'Share', - onPress: () { - if (data[0] != null) { - Share.share(data[0]['url']); - } - }, - ), - MyAction( - text: 'Open in Browser', - onPress: () { - if (data[0] != null) { - launchUrl(data[0]['url']); - } - }, - ), + if (data != null) ...[ + ActionItem.share(data[0]['url']), + ActionItem.launch(data[0]['url']), + ], ], ); } diff --git a/lib/widgets/action.dart b/lib/widgets/action.dart index 789db7f..f69d120 100644 --- a/lib/widgets/action.dart +++ b/lib/widgets/action.dart @@ -1,33 +1,39 @@ import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; +import 'package:git_touch/utils/utils.dart'; import 'package:git_touch/widgets/action_entry.dart'; import 'package:provider/provider.dart'; import 'package:git_touch/models/theme.dart'; +import 'package:share/share.dart'; -class MyAction { +class ActionItem { String text; - Function onPress; + void Function() onPress; - MyAction({@required this.text, @required this.onPress}); + ActionItem({@required this.text, @required this.onPress}); + ActionItem.share(String url) + : text = 'Share', + onPress = (() { + Share.share(url); + }); + ActionItem.launch(String url) + : text = 'Open in Browser', + onPress = (() { + launchUrl(url); + }); } class ActionButton extends StatelessWidget { final String title; - final List actions; + final List items; final IconData iconData; ActionButton({ @required this.title, - @required this.actions, + @required this.items, this.iconData = Icons.more_vert, }); - void _onSelected(int value) { - if (value != null) { - actions[value].onPress(); - } - } - @override Widget build(BuildContext context) { switch (Provider.of(context).theme) { @@ -36,12 +42,12 @@ class ActionButton extends StatelessWidget { return ActionEntry( iconData: iconData, onTap: () async { - int value = await showCupertinoModalPopup( + var value = await showCupertinoModalPopup( context: context, builder: (BuildContext context) { return CupertinoActionSheet( title: Text(title), - actions: actions.asMap().entries.map((entry) { + actions: items.asMap().entries.map((entry) { return CupertinoActionSheetAction( child: Text(entry.value.text), onPressed: () { @@ -60,7 +66,9 @@ class ActionButton extends StatelessWidget { }, ); - _onSelected(value); + if (value != null) { + items[value].onPress(); + } }, ); }