git-touch-android-ios-app/lib/screens/issue.dart

438 lines
8.3 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2019-01-25 18:43:09 +01:00
import 'package:flutter/cupertino.dart';
2019-05-12 09:14:28 +02:00
import 'package:primer/primer.dart';
import 'package:share/share.dart';
import 'package:url_launcher/url_launcher.dart';
2019-01-31 07:37:25 +01:00
import '../utils/utils.dart';
import '../scaffolds/long_list.dart';
2019-02-03 07:42:50 +01:00
import '../widgets/timeline_item.dart';
2019-02-06 06:06:11 +01:00
import '../widgets/comment_item.dart';
2019-02-07 07:35:19 +01:00
import '../providers/settings.dart';
2019-02-20 09:31:22 +01:00
import '../widgets/action.dart';
var reactionChunk = emojiMap.entries.map((entry) {
var key = entry.key;
return '''
$key: reactions(content: $key) {
totalCount
viewerHasReacted
}''';
}).join('\n');
/// Screen for issue and pull request
2019-02-03 07:42:50 +01:00
class IssueScreen extends StatefulWidget {
final int number;
final String owner;
final String name;
final bool isPullRequest;
IssueScreen({
@required this.number,
@required this.owner,
@required this.name,
this.isPullRequest = false,
});
IssueScreen.fromFullName({
@required this.number,
@required String fullName,
this.isPullRequest = false,
}) : owner = fullName.split('/')[0],
name = fullName.split('/')[1];
2019-02-03 07:42:50 +01:00
@override
_IssueScreenState createState() => _IssueScreenState();
}
class _IssueScreenState extends State<IssueScreen> {
String get owner => widget.owner;
String get name => widget.name;
int get number => widget.number;
bool get isPullRequest => widget.isPullRequest;
2019-02-03 07:42:50 +01:00
String get resource => isPullRequest ? 'pullRequest' : 'issue';
String get issueChunk {
var base = '''
2019-04-05 15:19:00 +02:00
id
title
createdAt
body
author {
login
avatarUrl
}
closed
url
$reactionChunk
''';
if (isPullRequest) {
base += '''
merged
additions
deletions
commits {
totalCount
}
''';
}
return base;
}
String get timelineChunk {
var base = '''
__typename
... on IssueComment {
2019-04-05 15:19:00 +02:00
id
createdAt
body
author {
login
avatarUrl
}
$reactionChunk
}
... on Commit {
committedDate
oid
author {
user {
login
}
}
}
... on ReferencedEvent {
createdAt
isCrossRepository
actor {
login
}
commit {
oid
url
}
commitRepository {
owner {
login
}
name
}
}
... on RenamedTitleEvent {
createdAt
previousTitle
currentTitle
actor {
login
}
}
... on ClosedEvent {
createdAt
actor {
login
}
}
... on ReopenedEvent {
createdAt
actor {
login
}
}
... on CrossReferencedEvent {
createdAt
actor {
login
}
source {
__typename
... on Issue {
number
repository {
owner {
login
2019-02-07 07:35:19 +01:00
}
name
}
}
... on PullRequest {
number
repository {
owner {
login
2019-02-07 07:35:19 +01:00
}
name
2019-02-07 07:35:19 +01:00
}
}
}
}
... on LabeledEvent {
createdAt
actor {
login
}
label {
name
color
}
}
... on UnlabeledEvent {
createdAt
actor {
login
}
label {
name
color
}
}
... on MilestonedEvent {
createdAt
actor {
login
2019-02-07 07:35:19 +01:00
}
milestoneTitle
}
... on LockedEvent {
createdAt
actor {
login
}
lockReason
}
... on UnlockedEvent {
createdAt
actor {
login
}
}
... on AssignedEvent {
createdAt
actor {
login
}
user {
login
}
}
''';
2019-02-07 07:35:19 +01:00
if (isPullRequest) {
base += '''
... on ReviewRequestedEvent {
createdAt
actor {
login
}
requestedReviewer {
... on User {
login
}
}
}
... on PullRequestReview {
createdAt
state
author {
login
}
}
... on MergedEvent {
createdAt
mergeRefName
actor {
login
}
commit {
oid
url
}
}
... on HeadRefDeletedEvent {
createdAt
actor {
login
}
headRefName
}
''';
}
return base;
}
Future _queryIssue({String cursor, bool trailing = false}) async {
String timelineParams;
if (trailing) {
timelineParams = 'last: $pageSize';
} else {
timelineParams = 'first: $pageSize';
if (cursor != null) {
timelineParams += ', after: $cursor';
}
}
var data = await SettingsProvider.of(context).query('''
{
repository(owner: "$owner", name: "$name") {
$resource(number: $number) {
$issueChunk
timeline($timelineParams) {
totalCount
pageInfo {
hasNextPage
endCursor
}
nodes {
$timelineChunk
}
}
}
}
}
''');
return data['repository'][resource];
}
2019-05-12 09:47:36 +02:00
StateLabelStatus _getLabelStatus(payload) {
StateLabelStatus status;
if (isPullRequest) {
if (payload['merged']) {
2019-05-12 09:47:36 +02:00
status = StateLabelStatus.pullMerged;
} else if (payload['closed']) {
2019-05-12 09:47:36 +02:00
status = StateLabelStatus.pullClosed;
} else {
2019-05-12 09:47:36 +02:00
status = StateLabelStatus.pullOpened;
}
} else {
if (payload['closed']) {
2019-05-12 09:47:36 +02:00
status = StateLabelStatus.issueClosed;
} else {
2019-05-12 09:47:36 +02:00
status = StateLabelStatus.issueOpened;
}
}
2019-05-12 09:47:36 +02:00
return status;
}
2019-04-05 15:19:00 +02:00
_handleReaction(payload) {
return (String emojiKey, bool isRemove) async {
if (emojiKey == null) return;
var id = payload['id'] as String;
var operation = isRemove ? 'remove' : 'add';
await SettingsProvider.of(context).query('''
mutation {
${operation}Reaction(input: {subjectId: "$id", content: $emojiKey}) {
clientMutationId
}
}
''');
setState(() {
payload[emojiKey]['totalCount'] += isRemove ? -1 : 1;
payload[emojiKey]['viewerHasReacted'] = !isRemove;
});
};
}
2019-01-25 18:43:09 +01:00
@override
Widget build(BuildContext context) {
return LongListScaffold(
title: Text('$owner/$name #$number'),
trailingBuilder: (payload) {
2019-02-20 09:31:22 +01:00
return ActionButton(
title: (isPullRequest ? 'Pull Request' : 'Issue') + ' Actions',
actions: [
Action(
text: 'Share',
onPress: () {
Share.share(payload['url']);
},
),
Action(
text: 'Open in Browser',
onPress: () {
launch(payload['url']);
},
),
],
);
},
headerBuilder: (payload) {
return Column(children: <Widget>[
Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
border: Border(bottom: BorderSide(color: Colors.black12)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Expanded(
child: Text(
payload['title'],
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
Padding(padding: EdgeInsets.only(right: 8)),
2019-05-12 09:47:36 +02:00
StateLabel(_getLabelStatus(payload))
],
),
Padding(padding: EdgeInsets.only(bottom: 16)),
2019-04-05 15:19:00 +02:00
CommentItem(
payload,
onReaction: _handleReaction(payload),
),
],
),
)
]);
},
2019-04-05 15:19:00 +02:00
itemBuilder: (itemPayload) =>
TimelineItem(itemPayload, onReaction: _handleReaction(itemPayload)),
2019-02-03 07:42:50 +01:00
onRefresh: () async {
var res = await _queryIssue();
int totalCount = res['timeline']['totalCount'];
String cursor = res['timeline']['pageInfo']['endCursor'];
List leadingItems = res['timeline']['nodes'];
var payload = LongListPayload(
header: res,
totalCount: totalCount,
cursor: cursor,
leadingItems: leadingItems,
trailingItems: [],
);
if (totalCount > 2 * pageSize) {
var res = await _queryIssue(trailing: true);
payload.trailingItems = res['timeline']['nodes'];
2019-02-06 14:35:52 +01:00
}
return payload;
},
onLoadMore: (String _cursor) async {
var res = await _queryIssue(cursor: _cursor);
int totalCount = res['timeline']['totalCount'];
String cursor = res['timeline']['pageInfo']['endCursor'];
List leadingItems = res['timeline']['nodes'];
var payload = LongListPayload(
totalCount: totalCount,
cursor: cursor,
leadingItems: leadingItems,
);
return payload;
2019-02-03 07:42:50 +01:00
},
2019-01-25 18:43:09 +01:00
);
}
}