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

211 lines
6.3 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 '../utils/utils.dart';
2019-02-07 07:35:19 +01:00
import 'avatar.dart';
2018-07-05 16:08:19 +02:00
/// Events types:
///
/// https://developer.github.com/v3/activity/events/types/#event-types--payloads
2018-07-05 16:08:19 +02:00
class EventItem extends StatelessWidget {
final Event event;
EventItem(this.event);
TextSpan _buildRepo(BuildContext context) {
String name = event.repo.name;
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'];
String name = event.repo.name;
var arr = name.split('/');
2019-02-02 17:28:51 +01:00
return createLinkSpan(
context, '#' + id.toString(), () => IssueScreen(id, arr[0], arr[1]));
2018-07-05 16:08:19 +02:00
}
TextSpan _buildPullRequest(BuildContext context, int id) {
String name = event.repo.name;
var arr = name.split('/');
2019-02-02 17:28:51 +01:00
return createLinkSpan(context, '#' + id.toString(),
() => PullRequestScreen(id, arr[0], arr[1]));
}
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(
context,
event.actor.login,
() => UserScreen(event.actor.login),
)
];
_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-03 08:50:17 +01:00
Avatar(login: event.actor.login, url: event.actor.avatarUrl),
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(
2019-02-07 10:19:08 +01:00
iconData,
2019-02-02 17:28:51 +01:00
color: CupertinoColors.inactiveGray,
size: 22,
),
],
),
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) {
switch (event.type) {
case 'IssuesEvent':
return _buildItem(
context: context,
spans: [
TextSpan(text: ' ${event.payload['action']} issue '),
_buildIssue(context),
TextSpan(text: ' at '),
_buildRepo(context),
],
iconData: Octicons.issue_opened,
detail: event.payload['issue']['title'],
);
case 'PushEvent':
return _buildItem(
context: context,
spans: [
TextSpan(text: ' pushed to '),
TextSpan(
text: event.payload['ref'],
style: TextStyle(color: CupertinoColors.activeBlue),
),
TextSpan(text: ' at '),
_buildRepo(context),
TextSpan(text: '')
],
iconData: Octicons.repo_push,
);
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 '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 'WatchEvent':
return _buildItem(
context: context,
spans: [
TextSpan(text: ' ${event.payload['action']} '),
_buildRepo(context)
],
iconData: Octicons.star,
);
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,
TextSpan(text: ' at '),
_buildRepo(context),
// TextSpan(text: event.payload['comment']['body'])
],
detail: event.payload['comment']['body'],
iconData: Octicons.comment_discussion,
);
case 'ForkEvent':
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,
);
default:
return _buildItem(
context: context,
spans: [
TextSpan(
text: ' Type ${event.type} Not implement yet',
style: TextStyle(color: CupertinoColors.destructiveRed),
),
],
iconData: Octicons.octoface,
);
}
}
2018-07-05 16:08:19 +02:00
}