git-touch-android-ios-app/lib/widgets/event_item.dart

270 lines
7.9 KiB
Dart
Raw Normal View History

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-02-07 07:35:19 +01:00
import '../screens/issue.dart';
import '../screens/pull_request.dart';
import '../screens/user.dart';
import 'avatar.dart';
import '../utils/utils.dart';
2018-07-05 16:08:19 +02:00
2019-02-10 05:54:48 +01:00
class EventPayload {
String actorLogin;
String actorAvatarUrl;
String type;
String repoFullName;
Map<String, dynamic> payload;
EventPayload.fromJson(input) {
actorLogin = input['actor']['login'];
actorAvatarUrl = input['actor']['avatar_url'];
type = input['type'];
payload = input['payload'];
repoFullName = input['repo']['name'];
}
}
2018-07-05 16:08:19 +02:00
class EventItem extends StatelessWidget {
2019-02-10 05:54:48 +01:00
final EventPayload event;
2018-07-05 16:08:19 +02:00
EventItem(this.event);
TextSpan _buildRepo(BuildContext context) {
2019-02-10 05:54:48 +01:00
String name = event.repoFullName;
var arr = name.split('/');
return createRepoLinkSpan(context, arr[0], arr[1]);
}
2018-07-05 16:08:19 +02:00
TextSpan _buildIssue(BuildContext context) {
int id = event.payload['issue']['number'];
2019-02-10 05:54:48 +01:00
return createLinkSpan(
context,
'#' + id.toString(),
() =>
IssueScreen.fromFullName(number: id, fullName: event.repoFullName));
2018-07-05 16:08:19 +02:00
}
TextSpan _buildPullRequest(BuildContext context, int number) {
return createLinkSpan(
context,
'#' + number.toString(),
() => PullRequestScreen.fromFullName(
2019-02-10 05:54:48 +01:00
number: number, fullName: event.repoFullName),
);
}
2019-02-07 10:19:08 +01:00
Widget _buildItem({
@required BuildContext context,
@required List<TextSpan> spans,
String detail,
IconData iconData = Octicons.octoface,
}) {
var _spans = [
createLinkSpan(
2019-02-10 05:54:48 +01:00
context, event.actorLogin, () => UserScreen(event.actorLogin))
2019-02-07 10:19:08 +01:00
];
_spans.addAll(spans);
2018-07-05 16:08:19 +02:00
2019-02-02 17:28:51 +01:00
return Container(
padding: EdgeInsets.all(8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
2019-02-10 05:54:48 +01:00
Avatar(login: event.actorLogin, url: event.actorAvatarUrl),
2019-02-02 17:28:51 +01:00
Padding(padding: EdgeInsets.only(left: 10)),
Expanded(
child: RichText(
text: TextSpan(
style: TextStyle(
color: Colors.black, height: 1.3, fontSize: 15),
2019-02-07 10:19:08 +01:00
children: _spans,
),
2019-01-31 07:37:25 +01:00
),
2019-02-02 17:28:51 +01:00
),
Padding(padding: EdgeInsets.only(left: 8)),
Icon(iconData, color: Colors.black45, size: 22),
2019-02-02 17:28:51 +01:00
],
),
2019-02-07 10:19:08 +01:00
detail == null
? Container()
: Container(
padding: EdgeInsets.only(left: 46, top: 6),
child: Text(
detail,
overflow: TextOverflow.ellipsis,
maxLines: 3,
style: TextStyle(
color: Colors.black87,
fontSize: 14,
height: 1.2,
fontWeight: FontWeight.w300,
),
),
)
2019-02-02 17:28:51 +01:00
],
2018-07-05 16:08:19 +02:00
),
);
}
2019-02-07 10:19:08 +01:00
@override
build(BuildContext context) {
var defaultItem = _buildItem(
context: context,
2019-02-10 05:54:48 +01:00
spans: [
TextSpan(
text: ' ' + event.type,
style: TextStyle(color: Colors.blueAccent),
)
],
iconData: Octicons.octoface,
detail: 'Woops, ${event.type} not implemented yet',
);
// all events types here:
// https://developer.github.com/v3/activity/events/types/#event-types--payloads
2019-02-07 10:19:08 +01:00
switch (event.type) {
case 'CheckRunEvent':
case 'CheckSuiteEvent':
case 'CommitCommentEvent':
case 'ContentReferenceEvent':
case 'CreateEvent':
case 'DeleteEvent':
case 'DeploymentEvent':
case 'DeploymentStatusEvent':
case 'DownloadEvent':
case 'FollowEvent':
// TODO:
return defaultItem;
case 'ForkEvent':
2019-02-07 10:19:08 +01:00
return _buildItem(
context: context,
spans: [
TextSpan(text: ' forked '),
createRepoLinkSpan(
context,
event.payload['forkee']['owner']['login'],
event.payload['forkee']['name']),
TextSpan(text: ' from '),
_buildRepo(context),
],
iconData: Octicons.repo_forked,
);
case 'ForkApplyEvent':
case 'GitHubAppAuthorizationEvent':
case 'GistEvent':
case 'GollumEvent':
case 'InstallationEvent':
case 'InstallationRepositoriesEvent':
// TODO:
return defaultItem;
case 'IssueCommentEvent':
bool isIssue = event.payload['issue']['pull_request'] == null;
String resource = isIssue ? 'issue' : 'pull request';
TextSpan link = isIssue
? _buildIssue(context)
: _buildPullRequest(context, event.payload['issue']['number']);
return _buildItem(
context: context,
spans: [
TextSpan(text: ' commented on $resource '),
link,
2019-02-07 10:19:08 +01:00
TextSpan(text: ' at '),
_buildRepo(context),
// TextSpan(text: event.payload['comment']['body'])
2019-02-07 10:19:08 +01:00
],
detail: event.payload['comment']['body'],
iconData: Octicons.comment_discussion,
2019-02-07 10:19:08 +01:00
);
case 'IssuesEvent':
2019-02-07 10:19:08 +01:00
return _buildItem(
context: context,
spans: [
TextSpan(text: ' ${event.payload['action']} issue '),
_buildIssue(context),
2019-02-07 10:19:08 +01:00
TextSpan(text: ' at '),
_buildRepo(context),
],
iconData: Octicons.issue_opened,
detail: event.payload['issue']['title'],
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:
return defaultItem;
2019-02-07 10:19:08 +01:00
case 'PullRequestEvent':
return _buildItem(
context: context,
spans: [
TextSpan(text: ' ${event.payload['action']} pull request '),
_buildPullRequest(context, event.payload['pull_request']['number']),
TextSpan(text: ' at '),
_buildRepo(context),
],
iconData: Octicons.git_pull_request,
detail: event.payload['pull_request']['title'],
);
case 'PullRequestReviewEvent':
// TODO:
return defaultItem;
2019-02-07 10:19:08 +01:00
case 'PullRequestReviewCommentEvent':
return _buildItem(
context: context,
spans: [
TextSpan(text: ' reviewed pull request '),
_buildPullRequest(context, event.payload['pull_request']['number']),
TextSpan(text: ' at '),
_buildRepo(context),
],
detail: event.payload['comment']['body'],
);
case 'PushEvent':
2019-02-07 10:19:08 +01:00
return _buildItem(
context: context,
spans: [
TextSpan(text: ' pushed to '),
TextSpan(
text: event.payload['ref'],
style: TextStyle(color: CupertinoColors.activeBlue),
),
2019-02-07 10:19:08 +01:00
TextSpan(text: ' at '),
_buildRepo(context),
TextSpan(text: '')
2019-02-07 10:19:08 +01:00
],
iconData: Octicons.repo_push,
2019-02-07 10:19:08 +01:00
);
case 'ReleaseEvent':
case 'RepositoryEvent':
case 'RepositoryImportEvent':
case 'RepositoryVulnerabilityAlertEvent':
case 'SecurityAdvisoryEvent':
case 'StatusEvent':
case 'TeamEvent':
case 'TeamAddEvent':
// TODO:
return defaultItem;
case 'WatchEvent':
2019-02-07 10:19:08 +01:00
return _buildItem(
context: context,
spans: [
TextSpan(text: ' ${event.payload['action']} '),
_buildRepo(context)
2019-02-07 10:19:08 +01:00
],
iconData: Octicons.star,
2019-02-07 10:19:08 +01:00
);
default:
return defaultItem;
2019-02-07 10:19:08 +01:00
}
}
2018-07-05 16:08:19 +02:00
}