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

310 lines
11 KiB
Dart
Raw Normal View History

2021-05-16 09:16:35 +02:00
import 'package:ferry/ferry.dart';
import 'package:flutter/material.dart';
2019-01-25 18:43:09 +01:00
import 'package:flutter/cupertino.dart';
2021-01-17 15:08:32 +01:00
import 'package:git_touch/graphql/github.data.gql.dart';
import 'package:git_touch/graphql/github.req.gql.dart';
2021-05-16 09:16:35 +02:00
import 'package:git_touch/graphql/github.var.gql.dart';
2019-09-27 14:52:38 +02:00
import 'package:git_touch/models/auth.dart';
2020-01-12 08:44:07 +01:00
import 'package:git_touch/models/theme.dart';
2019-12-25 03:41:51 +01:00
import 'package:git_touch/utils/utils.dart';
2019-09-30 10:31:07 +02:00
import 'package:git_touch/widgets/action_button.dart';
2020-01-12 08:44:07 +01:00
import 'package:git_touch/widgets/avatar.dart';
import 'package:git_touch/widgets/link.dart';
2021-01-17 15:08:32 +01:00
import 'package:git_touch/widgets/timeline_item.dart';
2019-05-12 09:14:28 +02:00
import 'package:primer/primer.dart';
2019-09-08 14:07:35 +02:00
import 'package:provider/provider.dart';
2021-01-17 15:08:32 +01:00
import 'package:github/github.dart' as github;
import '../scaffolds/long_list.dart';
2019-02-06 06:06:11 +01:00
import '../widgets/comment_item.dart';
2021-01-17 15:08:32 +01:00
class GhIssueScreen extends StatelessWidget {
final String owner;
final String name;
2019-11-02 17:02:41 +01:00
final int number;
2021-01-17 15:08:32 +01:00
GhIssueScreen(this.owner, this.name, this.number);
Widget _buildHeader(
BuildContext context, {
2021-05-16 09:16:35 +02:00
required String? avatarUrl,
required String title,
required StateLabelStatus status,
required Widget body,
2021-01-17 15:08:32 +01:00
Iterable<Widget> extraWidgets = const [],
}) {
final theme = Provider.of<ThemeModel>(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
padding: CommonStyle.padding,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
2021-05-16 09:16:35 +02:00
LinkWidget(
2021-01-17 15:08:32 +01:00
url: '/github/$owner/$name',
child: Row(
children: <Widget>[
Avatar(url: avatarUrl, size: AvatarSize.extraSmall),
SizedBox(width: 4),
Text(
'$owner / $name',
style: TextStyle(
fontSize: 17,
color: theme.palette.secondaryText,
),
),
SizedBox(width: 4),
Text(
'#$number',
style: TextStyle(
fontSize: 17,
color: theme.palette.tertiaryText,
),
),
],
),
),
SizedBox(height: 8),
Text(
title,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w600,
),
),
SizedBox(height: 8),
StateLabel(status, small: true),
SizedBox(height: 8),
CommonStyle.border,
...extraWidgets,
SizedBox(height: 8),
2021-01-31 12:27:45 +01:00
body,
2021-01-17 15:08:32 +01:00
],
),
),
CommonStyle.border,
],
);
}
2021-05-16 09:16:35 +02:00
Future<GIssueData_repository?> _queryIssue(BuildContext context,
{String? cursor}) async {
2021-01-17 15:08:32 +01:00
final req = GIssueReq((b) {
b.vars.owner = owner;
b.vars.name = name;
b.vars.number = number;
b.vars.cursor = cursor;
2021-01-17 15:08:32 +01:00
});
2021-05-16 09:16:35 +02:00
OperationResponse<GIssueData, GIssueVars?> res =
await context.read<AuthModel>().gqlClient!.request(req).first;
return res.data!.repository;
}
2019-01-25 18:43:09 +01:00
@override
Widget build(BuildContext context) {
2021-01-17 15:08:32 +01:00
return LongListStatefulScaffold<GIssueData_repository, dynamic>(
title: Text('$owner/$name #$number'),
trailingBuilder: (p) {
2021-05-16 09:16:35 +02:00
if (p.issueOrPullRequest!.G__typename == 'Issue') {
2021-01-17 15:08:32 +01:00
final d = p.issueOrPullRequest
as GIssueData_repository_issueOrPullRequest__asIssue;
return ActionButton(
title: 'Actions',
items: [
if (!d.viewerCanUpdate)
2020-01-07 13:48:50 +01:00
ActionItem(
2021-01-17 15:08:32 +01:00
text: d.closed ? 'Reopen issue' : 'Close issue',
2020-01-07 13:48:50 +01:00
onTap: (_) async {
2021-05-16 09:16:35 +02:00
await context.read<AuthModel>().ghClient!.issues.edit(
2021-01-17 15:08:32 +01:00
github.RepositorySlug(owner, name),
number,
github.IssueRequest(
state: d.closed ? 'open' : 'closed'));
2020-01-07 13:48:50 +01:00
},
),
2021-01-17 15:08:32 +01:00
...ActionItem.getUrlActions(d.url),
2019-09-30 09:46:06 +02:00
],
2021-01-17 15:08:32 +01:00
);
} else {
final d = p.issueOrPullRequest
as GIssueData_repository_issueOrPullRequest__asPullRequest;
return ActionButton(
title: 'Actions',
items: [
...ActionItem.getUrlActions(d.url),
],
);
}
},
2020-01-12 08:44:07 +01:00
headerBuilder: (p) {
final theme = Provider.of<ThemeModel>(context);
2021-05-16 09:16:35 +02:00
if (p.issueOrPullRequest!.G__typename == 'Issue') {
2021-01-17 15:08:32 +01:00
final issue = p.issueOrPullRequest
as GIssueData_repository_issueOrPullRequest__asIssue;
2021-01-31 12:27:45 +01:00
2021-01-17 15:08:32 +01:00
return _buildHeader(
context,
2021-05-16 09:16:35 +02:00
avatarUrl: issue.author!.avatarUrl,
2021-01-17 15:08:32 +01:00
title: issue.title,
status: issue.closed
? StateLabelStatus.issueClosed
: StateLabelStatus.issueOpened,
2021-01-31 12:27:45 +01:00
body: CommentItem.gql(issue, issue, (key) {
// TODO: reduce boilerplate
// switch (key) {
// case GReactionContent.THUMBS_UP:
// issue.THUMBS_UP.rebuild((b) {
// b.viewerHasReacted = !b.viewerHasReacted;
// b.totalCount += (b.viewerHasReacted ? 1 : -1);
// });
// break;
// case GReactionContent.THUMBS_DOWN:
// issue.THUMBS_DOWN.rebuild((b) {
// b.viewerHasReacted = !b.viewerHasReacted;
// b.totalCount += (b.viewerHasReacted ? 1 : -1);
// });
// break;
// case GReactionContent.LAUGH:
// issue.LAUGH.rebuild((b) {
// b.viewerHasReacted = !b.viewerHasReacted;
// b.totalCount += (b.viewerHasReacted ? 1 : -1);
// });
// break;
// case GReactionContent.HOORAY:
// issue.HOORAY.rebuild((b) {
// b.viewerHasReacted = !b.viewerHasReacted;
// b.totalCount += (b.viewerHasReacted ? 1 : -1);
// });
// break;
// case GReactionContent.CONFUSED:
// issue.CONFUSED.rebuild((b) {
// b.viewerHasReacted = !b.viewerHasReacted;
// b.totalCount += (b.viewerHasReacted ? 1 : -1);
// });
// break;
// case GReactionContent.HEART:
// issue.HEART.rebuild((b) {
// b.viewerHasReacted = !b.viewerHasReacted;
// b.totalCount += (b.viewerHasReacted ? 1 : -1);
// });
// break;
// case GReactionContent.ROCKET:
// issue.ROCKET.rebuild((b) {
// b.viewerHasReacted = !b.viewerHasReacted;
// b.totalCount += (b.viewerHasReacted ? 1 : -1);
// });
// break;
// case GReactionContent.EYES:
// issue.EYES.rebuild((b) {
// b.viewerHasReacted = !b.viewerHasReacted;
// b.totalCount += (b.viewerHasReacted ? 1 : -1);
// });
// break;
// }
}),
2021-01-17 15:08:32 +01:00
);
} else {
final pr = p.issueOrPullRequest
as GIssueData_repository_issueOrPullRequest__asPullRequest;
return _buildHeader(
context,
2021-05-16 09:16:35 +02:00
avatarUrl: pr.author!.avatarUrl,
2021-01-17 15:08:32 +01:00
title: pr.title,
status: pr.merged
? StateLabelStatus.pullMerged
: pr.closed
? StateLabelStatus.pullClosed
: StateLabelStatus.pullOpened,
2021-01-31 12:27:45 +01:00
body: CommentItem.gql(pr, pr, (key) {}),
2021-01-17 15:08:32 +01:00
extraWidgets: [
2021-05-16 09:16:35 +02:00
LinkWidget(
2021-01-17 15:08:32 +01:00
child: Container(
padding: EdgeInsets.symmetric(vertical: 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('${pr.changedFiles} files changed',
style: TextStyle(
color: theme.palette.secondaryText,
fontSize: 17,
2021-01-17 15:08:32 +01:00
)),
Row(
children: <Widget>[
Text('+${pr.additions}',
2020-01-12 08:44:07 +01:00
style: TextStyle(
2021-01-17 15:08:32 +01:00
color: Colors.green,
fontSize: 15,
)),
SizedBox(width: 2),
Text('-${pr.deletions}',
style: TextStyle(
color: Colors.red,
fontSize: 15,
)),
2021-02-14 15:17:22 +01:00
Icon(Ionicons.chevron_forward,
2021-01-17 15:08:32 +01:00
color: theme.palette.border),
],
)
],
),
),
url: '/github/$owner/$name/pull/$number/files',
2019-09-07 11:48:59 +02:00
),
2021-01-17 15:08:32 +01:00
CommonStyle.border,
],
);
}
},
2021-01-17 15:08:32 +01:00
itemBuilder: (p) => TimelineItem(p),
2019-02-03 07:42:50 +01:00
onRefresh: () async {
2021-05-16 09:16:35 +02:00
final res =
await (_queryIssue(context) as Future<GIssueData_repository>);
if (res.issueOrPullRequest!.G__typename == 'Issue') {
2021-01-17 15:08:32 +01:00
final issue = res.issueOrPullRequest
as GIssueData_repository_issueOrPullRequest__asIssue;
return LongListPayload(
header: res,
totalCount: issue.timelineItems.totalCount,
cursor: issue.timelineItems.pageInfo.endCursor,
2021-05-16 09:16:35 +02:00
leadingItems: issue.timelineItems.nodes!.toList(),
2021-01-17 15:08:32 +01:00
trailingItems: [],
);
} else {
final pr = res.issueOrPullRequest
as GIssueData_repository_issueOrPullRequest__asPullRequest;
return LongListPayload(
header: res,
totalCount: pr.timelineItems.totalCount,
cursor: pr.timelineItems.pageInfo.endCursor,
2021-05-16 09:16:35 +02:00
leadingItems: pr.timelineItems.nodes!.toList(),
2021-01-17 15:08:32 +01:00
trailingItems: [],
);
2019-02-06 14:35:52 +01:00
}
},
2021-01-17 15:08:32 +01:00
onLoadMore: (_cursor) async {
2021-05-16 09:16:35 +02:00
final res = await (_queryIssue(context, cursor: _cursor)
as Future<GIssueData_repository>);
if (res.issueOrPullRequest!.G__typename == 'Issue') {
2021-01-17 15:08:32 +01:00
final issue = res.issueOrPullRequest
as GIssueData_repository_issueOrPullRequest__asIssue;
return LongListPayload(
header: res,
totalCount: issue.timelineItems.totalCount,
cursor: issue.timelineItems.pageInfo.endCursor,
2021-05-16 09:16:35 +02:00
leadingItems: issue.timelineItems.nodes!.toList(),
2021-01-17 15:08:32 +01:00
);
} else {
final pr = res.issueOrPullRequest
as GIssueData_repository_issueOrPullRequest__asPullRequest;
return LongListPayload(
header: res,
totalCount: pr.timelineItems.totalCount,
cursor: pr.timelineItems.pageInfo.endCursor,
2021-05-16 09:16:35 +02:00
leadingItems: pr.timelineItems.nodes!.toList(),
2021-01-17 15:08:32 +01:00
);
}
2019-02-03 07:42:50 +01:00
},
2019-01-25 18:43:09 +01:00
);
}
2020-04-16 05:36:48 +02:00
}