1
0
mirror of https://github.com/git-touch/git-touch synced 2025-02-21 22:07:51 +01:00

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 { Future<void> _openActions(payload) async {
if (payload == null) return; if (payload == null) return;
var value = await showCupertinoModalPopup<int>( showActions(
context: context, context,
builder: (BuildContext context) { title: (isPullRequest ? 'Pull Request' : 'Issue') + ' Actions',
return CupertinoActionSheet( actions: [
title: Text((isPullRequest ? 'Pull Request' : 'Issue') + ' Actions'), Action(
actions: { text: 'Share',
2: 'Share', onPress: () {
3: 'Open in Browser', Share.share(payload['url']);
}.entries.map((entry) { },
return CupertinoActionSheetAction( ),
child: Text(entry.value), Action(
onPressed: () { text: 'Open in Browser',
Navigator.pop(context, entry.key); onPress: () {
}, launch(payload['url']);
); },
}).toList(), ),
cancelButton: CupertinoActionSheetAction( ],
child: const Text('Cancel'),
isDefaultAction: true,
onPressed: () {
Navigator.pop(context);
},
),
);
},
); );
switch (value) {
case 2:
Share.share(payload['url']);
break;
case 3:
launch(payload['url']);
break;
default:
}
} }
@override @override

View File

@ -10,6 +10,7 @@ import '../widgets/repo_item.dart';
import '../widgets/entry_item.dart'; import '../widgets/entry_item.dart';
import '../screens/issues.dart'; import '../screens/issues.dart';
import '../widgets/link.dart'; import '../widgets/link.dart';
import '../utils/utils.dart';
class RepoScreen extends StatefulWidget { class RepoScreen extends StatefulWidget {
final String owner; final String owner;
@ -85,59 +86,35 @@ class _RepoScreenState extends State<RepoScreen> {
if (data == null) return; if (data == null) return;
var payload = data[0]; var payload = data[0];
var _actionMap = { showActions(context, title: 'Repository Actions', actions: [
0: payload['viewerHasStarred'] ? 'Unstar' : 'Star', Action(
// 1: 'Watch', TODO: text: payload['viewerHasStarred'] ? 'Unstar' : 'Star',
2: 'Share', onPress: () async {
3: 'Open in Browser', if (payload['viewerHasStarred']) {
}; await SettingsProvider.of(context)
.deleteWithCredentials('/user/starred/$owner/$name');
var value = await showCupertinoModalPopup<int>( payload['viewerHasStarred'] = false;
context: context, } else {
builder: (BuildContext context) { SettingsProvider.of(context)
return CupertinoActionSheet( .putWithCredentials('/user/starred/$owner/$name');
title: Text('Repository Actions'), payload['viewerHasStarred'] = true;
actions: _actionMap.entries.map((entry) { }
return CupertinoActionSheetAction( },
child: Text(entry.value), ),
onPressed: () { // TODO: watch
Navigator.pop(context, entry.key); Action(
}, text: 'Share',
); onPress: () {
}).toList(), Share.share(payload['url']);
cancelButton: CupertinoActionSheetAction( },
child: const Text('Cancel'), ),
isDefaultAction: true, Action(
onPressed: () { text: 'Open in Browser',
Navigator.pop(context); onPress: () {
}, launch(payload['url']);
), },
); ),
}, ]);
);
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:
}
} }
@override @override

View File

@ -138,59 +138,41 @@ class _UserScreenState extends State<UserScreen> {
Future<void> _openActions(payload) async { Future<void> _openActions(payload) async {
if (payload == null) return; if (payload == null) return;
var _actionMap = {}; List<Action> actions = [];
if (payload['viewerCanFollow']) { 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>( actions.addAll([
context: context, Action(
builder: (BuildContext context) { text: 'Share',
return CupertinoActionSheet( onPress: () {
title: Text('User Actions'), Share.share(payload['url']);
actions: _actionMap.entries.map((entry) { },
return CupertinoActionSheetAction( ),
child: Text(entry.value), Action(
onPressed: () { text: 'Open in Browser',
Navigator.pop(context, entry.key); onPress: () {
}, launch(payload['url']);
); },
}).toList(), ),
cancelButton: CupertinoActionSheetAction( ]);
child: const Text('Cancel'),
isDefaultAction: true,
onPressed: () {
Navigator.pop(context);
},
),
);
},
);
switch (value) { showActions(context, title: 'User Actions', actions: actions);
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:
}
} }
@override @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) { TextSpan createLinkSpan(BuildContext context, String text, Function handle) {
return TextSpan( return TextSpan(
text: text, text: text,