1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-23 05:44:03 +01:00
git-touch-android-ios-app/lib/widgets/event_item.dart

407 lines
13 KiB
Dart
Raw Normal View History

2019-12-22 06:00:58 +01:00
import 'package:flutter/gestures.dart';
2018-07-05 16:08:19 +02:00
import 'package:flutter/material.dart';
2019-02-02 17:28:51 +01:00
import 'package:flutter/cupertino.dart';
2019-12-12 07:02:48 +01:00
import 'package:git_touch/models/github.dart';
2019-11-03 16:33:24 +01:00
import 'package:git_touch/models/theme.dart';
2019-12-20 15:41:38 +01:00
import 'package:git_touch/widgets/issue_icon.dart';
2019-11-03 16:33:24 +01:00
import 'package:provider/provider.dart';
2019-08-31 15:37:29 +02:00
import 'package:timeago/timeago.dart' as timeago;
2019-02-07 07:35:19 +01:00
import 'avatar.dart';
import '../widgets/link.dart';
import '../utils/utils.dart';
2018-07-05 16:08:19 +02:00
class EventItem extends StatelessWidget {
2019-12-20 15:41:38 +01:00
final GithubEvent e;
2019-12-20 15:41:38 +01:00
EventItem(this.e);
2018-07-05 16:08:19 +02:00
2019-12-22 06:00:58 +01:00
InlineSpan _buildLinkSpan(BuildContext context, String text, String url) {
final theme = Provider.of<ThemeModel>(context);
2019-11-05 14:22:41 +01:00
return TextSpan(
text: text,
2019-12-21 09:16:17 +01:00
style: TextStyle(color: theme.palette.primary),
2019-12-22 06:00:58 +01:00
recognizer: TapGestureRecognizer()
..onTap = () {
theme.push(context, url);
},
2019-11-05 14:22:41 +01:00
);
}
2018-07-05 16:08:19 +02:00
2019-12-22 06:00:58 +01:00
InlineSpan _buildRepo(BuildContext context, [String fullName]) {
final name = fullName ?? e.repo.name;
return _buildLinkSpan(context, name, '/$name');
}
InlineSpan _buildIssue(BuildContext context, int number,
{bool isPullRequest = false}) {
return _buildLinkSpan(context, '#$number',
'/${e.repoOwner}/${e.repoName}/${isPullRequest ? 'pulls' : 'issues'}/$number');
}
2019-02-07 10:19:08 +01:00
Widget _buildItem({
@required BuildContext context,
2019-09-25 07:34:13 +02:00
@required List<InlineSpan> spans,
2019-12-20 15:41:38 +01:00
Widget card,
2019-02-07 10:19:08 +01:00
}) {
2019-11-05 14:22:41 +01:00
final theme = Provider.of<ThemeModel>(context);
2019-12-20 13:52:49 +01:00
return Container(
padding: CommonStyle.padding,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
2019-12-27 08:06:45 +01:00
Avatar(url: e.actor.avatarUrl, linkUrl: '/' + e.actor.login),
2019-12-20 13:52:49 +01:00
SizedBox(width: 10),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
2019-12-25 04:05:25 +01:00
children: join(SizedBox(height: 6), [
2019-12-20 13:52:49 +01:00
RichText(
text: TextSpan(
style: TextStyle(
fontSize: 17,
2019-12-20 13:52:49 +01:00
color: theme.palette.text,
2019-08-31 15:37:29 +02:00
),
2019-12-20 13:52:49 +01:00
children: [
2019-12-22 06:00:58 +01:00
_buildLinkSpan(
context, e.actor.login, '/${e.actor.login}'),
2019-12-20 13:52:49 +01:00
...spans,
],
2019-09-07 11:08:24 +02:00
),
2019-12-20 13:52:49 +01:00
),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
2019-12-20 15:41:38 +01:00
Text(timeago.format(e.createdAt),
2019-12-20 13:52:49 +01:00
style: TextStyle(
fontSize: 14,
2019-12-20 13:52:49 +01:00
color: theme.palette.tertiaryText,
)),
],
),
2019-12-20 15:41:38 +01:00
if (card != null) card
2019-12-20 13:52:49 +01:00
]),
2019-01-31 07:37:25 +01:00
),
2019-12-20 13:52:49 +01:00
),
],
),
],
2018-07-05 16:08:19 +02:00
),
);
}
2019-02-07 10:19:08 +01:00
2019-12-12 07:02:48 +01:00
Widget _buildDefaultItem(BuildContext context) {
2019-11-05 14:22:41 +01:00
final theme = Provider.of<ThemeModel>(context);
2019-12-12 07:02:48 +01:00
return _buildItem(
context: context,
2019-02-10 05:54:48 +01:00
spans: [
TextSpan(
2019-12-20 15:41:38 +01:00
text: ' ' + e.type,
2019-11-05 14:22:41 +01:00
style: TextStyle(color: theme.palette.primary),
2019-02-10 05:54:48 +01:00
)
],
2019-12-22 06:00:58 +01:00
card: Text('Woops, ${e.type} not implemented yet'),
2019-12-20 15:41:38 +01:00
);
}
Widget _buildCommitsCard(BuildContext context) {
final theme = Provider.of<ThemeModel>(context);
return Link(
url:
'https://github.com/${e.repoOwner}/${e.repoName}/compare/${e.payload.before}...${e.payload.head}',
child: Container(
padding: EdgeInsets.all(12),
decoration: BoxDecoration(
color: theme.palette.grayBackground,
borderRadius: BorderRadius.all(Radius.circular(4))),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
RichText(
text: TextSpan(
style: TextStyle(color: theme.palette.text, fontSize: 15),
2019-12-20 15:41:38 +01:00
children: [
TextSpan(
text:
e.payload.commits.length.toString() + ' commits to '),
WidgetSpan(
child: PrimerBranchName(
e.payload.ref.replaceFirst('refs/heads/', '')),
),
],
),
),
SizedBox(height: 8),
2019-12-20 15:41:38 +01:00
...e.payload.commits.map((commit) {
return Row(
children: <Widget>[
Text(
commit.sha.substring(0, 7),
style: TextStyle(
color: theme.palette.primary,
fontSize: 15,
2019-12-20 15:41:38 +01:00
fontFamily: CommonStyle.monospace,
),
),
SizedBox(width: 6),
Expanded(
child: Text(
commit.message,
overflow: TextOverflow.ellipsis,
maxLines: 1,
style: TextStyle(color: theme.palette.text, fontSize: 15),
2019-12-20 15:41:38 +01:00
),
)
],
);
}).toList()
],
),
),
);
2019-12-12 07:02:48 +01:00
}
2019-12-20 15:41:38 +01:00
Widget _buildIssueCard(
BuildContext context, GithubEventIssue issue, String body,
{isPullRequest = false}) {
final theme = Provider.of<ThemeModel>(context);
IssueIconState state;
if (isPullRequest) {
if (issue.merged == true) {
state = IssueIconState.prMerged;
} else if (issue.state == 'open') {
state = IssueIconState.prOpen;
} else {
state = IssueIconState.prClosed;
}
} else {
if (issue.state == 'open') {
state = IssueIconState.open;
} else {
state = IssueIconState.closed;
}
}
return Link(
url:
'/${e.repoOwner}/${e.repoName}/${isPullRequest ? 'pulls' : 'issues'}/${issue.number}',
child: Container(
padding: EdgeInsets.all(12),
decoration: BoxDecoration(
color: theme.palette.grayBackground,
borderRadius: BorderRadius.all(Radius.circular(4))),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
2019-12-25 04:05:25 +01:00
children: join(SizedBox(height: 6), [
2019-12-20 15:41:38 +01:00
Row(
children: <Widget>[
IssueIcon(state, size: 20),
SizedBox(width: 4),
Expanded(
child: Text(
'#' + issue.number.toString() + ' ' + issue.title,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 17,
2019-12-20 15:41:38 +01:00
color: theme.palette.text,
),
overflow: TextOverflow.ellipsis,
),
),
],
),
if (body != null && body.isNotEmpty)
2019-12-20 15:41:38 +01:00
Text(
body,
overflow: TextOverflow.ellipsis,
maxLines: 3,
style:
TextStyle(color: theme.palette.secondaryText, fontSize: 15),
2019-12-20 15:41:38 +01:00
),
Row(
children: <Widget>[
2019-12-22 04:11:13 +01:00
Avatar(url: issue.user.avatarUrl, size: AvatarSize.extraSmall),
SizedBox(width: 8),
Text(issue.user.login,
style: TextStyle(
fontSize: 14,
color: theme.palette.tertiaryText,
)),
Expanded(child: Container()),
2019-12-25 04:05:25 +01:00
if (issue.comments != null) ...[
Icon(
Octicons.comment,
size: 14,
color: theme.palette.tertiaryText,
),
SizedBox(width: 4),
Text(issue.comments.toString(),
style: TextStyle(
fontSize: 14,
color: theme.palette.tertiaryText,
)),
]
],
)
]),
2019-12-20 13:52:49 +01:00
),
2019-12-20 15:41:38 +01:00
),
2019-12-20 13:52:49 +01:00
);
}
2019-12-12 07:02:48 +01:00
@override
build(BuildContext context) {
// all events types here:
// https://developer.github.com/v3/activity/events/types/#event-types--payloads
2019-12-20 15:41:38 +01:00
switch (e.type) {
case 'CheckRunEvent':
case 'CheckSuiteEvent':
case 'CommitCommentEvent':
case 'ContentReferenceEvent':
case 'CreateEvent':
2019-12-25 03:56:30 +01:00
return _buildItem(
context: context,
spans: <InlineSpan>[
TextSpan(
text: ' created ${e.payload.refType} ${e.payload.ref} at '),
_buildRepo(context),
],
);
case 'DeleteEvent':
case 'DeploymentEvent':
case 'DeploymentStatusEvent':
case 'DownloadEvent':
case 'FollowEvent':
// TODO:
2019-12-12 07:02:48 +01:00
return _buildDefaultItem(context);
case 'ForkEvent':
2019-12-20 15:41:38 +01:00
final forkeeOwner = e.payload.forkee['owner']['login'] as String;
final forkeeName = e.payload.forkee['name'] as String;
2019-02-07 10:19:08 +01:00
return _buildItem(
context: context,
spans: [
TextSpan(text: ' forked '),
2019-12-22 06:00:58 +01:00
_buildRepo(context, '$forkeeOwner/$forkeeName'),
TextSpan(text: ' from '),
2019-12-22 06:00:58 +01:00
_buildRepo(context),
],
);
case 'ForkApplyEvent':
case 'GitHubAppAuthorizationEvent':
case 'GistEvent':
case 'GollumEvent':
case 'InstallationEvent':
case 'InstallationRepositoriesEvent':
// TODO:
2019-12-12 07:02:48 +01:00
return _buildDefaultItem(context);
case 'IssueCommentEvent':
return _buildItem(
context: context,
spans: [
2019-12-20 13:52:49 +01:00
TextSpan(
text:
2019-12-20 15:41:38 +01:00
' commented on ${e.payload.issue.isPullRequestComment ? 'pull request' : 'issue'} '),
2019-12-22 06:00:58 +01:00
_buildIssue(context, e.payload.issue.number),
2019-02-07 10:19:08 +01:00
TextSpan(text: ' at '),
2019-12-22 06:00:58 +01:00
_buildRepo(context),
2019-02-07 10:19:08 +01:00
],
2019-12-20 15:41:38 +01:00
card: _buildIssueCard(
context,
e.payload.issue,
e.payload.comment.body,
isPullRequest: e.payload.issue.isPullRequestComment,
),
2019-02-07 10:19:08 +01:00
);
case 'IssuesEvent':
2019-12-20 15:41:38 +01:00
final issue = e.payload.issue;
2019-02-07 10:19:08 +01:00
return _buildItem(
context: context,
spans: [
2019-12-20 15:41:38 +01:00
TextSpan(text: ' ${e.payload.action} issue '),
2019-12-22 06:00:58 +01:00
_buildIssue(context, issue.number),
2019-02-07 10:19:08 +01:00
TextSpan(text: ' at '),
2019-12-22 06:00:58 +01:00
_buildRepo(context),
2019-02-07 10:19:08 +01:00
],
2019-12-20 15:41:38 +01:00
card: _buildIssueCard(context, issue, issue.body),
2019-02-07 10:19:08 +01:00
);
case 'LabelEvent':
case 'MarketplacePurchaseEvent':
case 'MemberEvent':
case 'MembershipEvent':
case 'MilestoneEvent':
case 'OrganizationEvent':
case 'OrgBlockEvent':
case 'PageBuildEvent':
case 'ProjectCardEvent':
case 'ProjectColumnEvent':
case 'ProjectEvent':
case 'PublicEvent':
// TODO:
2019-12-12 07:02:48 +01:00
return _buildDefaultItem(context);
2019-02-07 10:19:08 +01:00
case 'PullRequestEvent':
2019-12-20 15:41:38 +01:00
final pr = e.payload.pullRequest;
2019-02-07 10:19:08 +01:00
return _buildItem(
context: context,
spans: [
2019-12-20 15:41:38 +01:00
TextSpan(text: ' ${e.payload.action} pull request '),
2019-12-22 06:00:58 +01:00
_buildIssue(context, pr.number, isPullRequest: true),
2019-02-07 10:19:08 +01:00
TextSpan(text: ' at '),
2019-12-22 06:00:58 +01:00
_buildRepo(context),
2019-02-07 10:19:08 +01:00
],
2019-12-20 15:41:38 +01:00
card: _buildIssueCard(context, pr, pr.body, isPullRequest: true),
2019-02-07 10:19:08 +01:00
);
case 'PullRequestReviewEvent':
// TODO:
2019-12-12 07:02:48 +01:00
return _buildDefaultItem(context);
2019-02-07 10:19:08 +01:00
case 'PullRequestReviewCommentEvent':
2019-12-20 15:41:38 +01:00
final pr = e.payload.pullRequest;
2019-02-07 10:19:08 +01:00
return _buildItem(
context: context,
spans: [
TextSpan(text: ' reviewed pull request '),
2019-12-22 06:00:58 +01:00
_buildIssue(context, pr.number, isPullRequest: true),
2019-02-07 10:19:08 +01:00
TextSpan(text: ' at '),
2019-12-22 06:00:58 +01:00
_buildRepo(context),
2019-02-07 10:19:08 +01:00
],
2019-12-25 04:05:25 +01:00
card: _buildIssueCard(context, pr, e.payload.comment.body,
isPullRequest: true),
2019-02-07 10:19:08 +01:00
);
case 'PushEvent':
2019-02-07 10:19:08 +01:00
return _buildItem(
context: context,
2019-12-22 06:00:58 +01:00
spans: [TextSpan(text: ' pushed to '), _buildRepo(context)],
2019-12-20 15:41:38 +01:00
card: _buildCommitsCard(context),
2019-02-07 10:19:08 +01:00
);
case 'ReleaseEvent':
2019-12-25 03:46:56 +01:00
return _buildItem(
context: context,
spans: [
TextSpan(text: ' released '),
_buildLinkSpan(
context, e.payload.release.tagName, e.payload.release.htmlUrl),
TextSpan(text: ' at '),
_buildRepo(context)
],
);
case 'RepositoryEvent':
case 'RepositoryImportEvent':
case 'RepositoryVulnerabilityAlertEvent':
case 'SecurityAdvisoryEvent':
case 'StatusEvent':
case 'TeamEvent':
case 'TeamAddEvent':
// TODO:
2019-12-12 07:02:48 +01:00
return _buildDefaultItem(context);
case 'WatchEvent':
2019-02-07 10:19:08 +01:00
return _buildItem(
context: context,
2019-12-22 06:00:58 +01:00
spans: [TextSpan(text: ' starred '), _buildRepo(context)],
2019-02-07 10:19:08 +01:00
);
default:
2019-12-12 07:02:48 +01:00
return _buildDefaultItem(context);
2019-02-07 10:19:08 +01:00
}
}
2018-07-05 16:08:19 +02:00
}