refactor: extract show actions method

This commit is contained in:
Rongjian Zhang 2019-02-19 21:20:00 +08:00
parent 2bdd9216c3
commit eac2cc5470
4 changed files with 126 additions and 137 deletions

View File

@ -336,42 +336,24 @@ __typename
Future<void> _openActions(payload) async {
if (payload == null) return;
var value = await showCupertinoModalPopup<int>(
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

View File

@ -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<RepoScreen> {
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<int>(
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

View File

@ -138,59 +138,41 @@ class _UserScreenState extends State<UserScreen> {
Future<void> _openActions(payload) async {
if (payload == null) return;
var _actionMap = {};
List<Action> 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<int>(
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

View File

@ -118,6 +118,54 @@ Future<T> showOptions<T>(BuildContext context, List<DialogOption<T>> options) {
}
}
class Action {
String text;
Function onPress;
Action({@required this.text, @required this.onPress});
}
Future showActions(
BuildContext context, {
@required String title,
@required List<Action> actions,
}) async {
int result;
switch (SettingsProvider.of(context).theme) {
case ThemeMap.cupertino:
result = await showCupertinoModalPopup<int>(
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,