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

294 lines
8.6 KiB
Dart
Raw Normal View History

2019-01-29 06:34:52 +01:00
import 'dart:core';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
2019-01-31 07:37:25 +01:00
import '../utils/utils.dart';
2019-02-07 07:35:19 +01:00
import 'comment_item.dart';
import 'user_name.dart';
class TimelineItem extends StatelessWidget {
final Map<String, dynamic> item;
TimelineItem(this.item);
TextSpan _buildReviewText(BuildContext context, item) {
switch (item['state']) {
case 'APPROVED':
return TextSpan(text: ' approved these changes');
case 'COMMENTED':
return TextSpan(text: ' commented ');
default:
return warningSpan;
}
}
Widget _buildItem({
String actor,
IconData iconData = Octicons.octoface,
2019-02-06 12:14:11 +01:00
Color iconColor = Palette.gray,
TextSpan textSpan,
item,
}) {
return Row(
children: <Widget>[
2019-02-06 12:14:11 +01:00
Icon(iconData, color: iconColor, size: 16),
Padding(padding: EdgeInsets.only(left: 4)),
Expanded(
child: RichText(
text: TextSpan(style: TextStyle(color: Colors.black), children: [
createUserSpan(actor),
textSpan,
// TextSpan(text: ' ' + TimeAgo.formatFromString(item['createdAt']))
]),
),
),
],
);
}
2019-01-29 06:34:52 +01:00
TextSpan _buildLabel(item) {
var textColor = item['label']['color'] == 'fbca04' ? null : Colors.white;
return TextSpan(
text: item['label']['name'],
style: TextStyle(
2019-02-04 11:32:39 +01:00
color: textColor,
// https://github.com/flutter/flutter/issues/20430
background: Paint()..color = convertColor(item['label']['color']),
// https://stackoverflow.com/a/52592679
// ..strokeWidth = 16.5
// ..style = PaintingStyle.stroke
),
2019-01-29 06:34:52 +01:00
);
}
Widget _buildByType(BuildContext context) {
2019-02-08 07:30:22 +01:00
String type = item['__typename'];
2019-02-08 07:30:22 +01:00
var defaultItem = _buildItem(
actor: '',
iconData: Octicons.octoface,
textSpan: TextSpan(children: [
TextSpan(text: 'Woops, $type type not implemented yet'),
]),
item: item,
);
switch (type) {
// common types
case 'Commit':
return _buildItem(
2019-02-08 07:30:22 +01:00
actor: item['author']['user']['login'],
iconData: Octicons.git_commit,
textSpan: TextSpan(children: [
2019-02-08 07:30:22 +01:00
TextSpan(text: ' added commit '),
TextSpan(text: item['oid'].substring(0, 8))
]),
item: item,
);
2019-02-08 07:30:22 +01:00
case 'IssueComment':
return CommentItem(item);
case 'CrossReferencedEvent':
return _buildItem(
actor: item['actor']['login'],
2019-02-08 07:30:22 +01:00
iconData: Octicons.primitive_dot,
iconColor: Palette.green,
textSpan: TextSpan(
text: ' referenced this on #' +
item['source']['number'].toString()),
item: item,
);
case 'ClosedEvent':
return _buildItem(
actor: item['actor']['login'],
iconData: Octicons.circle_slash,
iconColor: Palette.red,
textSpan: TextSpan(text: ' closed this '),
item: item,
);
2019-02-08 07:30:22 +01:00
case 'ReopenedEvent':
return _buildItem(
actor: item['actor']['login'],
iconData: Octicons.primitive_dot,
iconColor: Palette.green,
textSpan: TextSpan(text: ' reopened this '),
item: item,
);
2019-02-08 07:30:22 +01:00
case 'SubscribedEvent':
case 'UnsubscribedEvent':
return defaultItem; // TODO:
case 'ReferencedEvent':
// TODO: isCrossRepository
if (item['commit'] == null) {
return Container();
}
return _buildItem(
actor: item['actor']['login'],
2019-02-08 07:30:22 +01:00
iconData: Octicons.bookmark,
textSpan: TextSpan(children: [
TextSpan(text: ' referenced this pull request from commit '),
TextSpan(text: item['commit']['oid'].substring(0, 8)),
]),
item: item,
);
case 'AssignedEvent':
return _buildItem(
actor: item['actor']['login'],
iconData: Octicons.key,
textSpan: TextSpan(children: [
TextSpan(text: ' assigned this to '),
TextSpan(text: item['user']['login'])
]),
item: item,
);
2019-02-08 07:30:22 +01:00
case 'UnassignedEvent':
return defaultItem; // TODO:
2019-01-29 06:34:52 +01:00
case 'LabeledEvent':
return _buildItem(
actor: item['actor']['login'],
iconData: Octicons.tag,
textSpan: TextSpan(children: [
TextSpan(text: ' added '),
_buildLabel(item),
TextSpan(text: ' label'),
]),
item: item,
);
case 'UnlabeledEvent':
return _buildItem(
actor: item['actor']['login'],
iconData: Octicons.tag,
textSpan: TextSpan(children: [
TextSpan(text: ' removed '),
_buildLabel(item),
TextSpan(text: ' label'),
]),
item: item,
);
2019-02-08 07:30:22 +01:00
2019-01-29 06:34:52 +01:00
case 'MilestonedEvent':
return _buildItem(
actor: item['actor']['login'],
iconData: Octicons.milestone,
textSpan: TextSpan(children: [
TextSpan(text: ' added this to '),
TextSpan(text: item['milestoneTitle']),
TextSpan(text: ' milestone'),
]),
item: item,
);
2019-02-08 07:30:22 +01:00
case 'DemilestonedEvent':
return defaultItem; // TODO:
case 'RenamedTitleEvent':
2019-01-29 06:34:52 +01:00
return _buildItem(
actor: item['actor']['login'],
2019-02-08 07:30:22 +01:00
iconData: Octicons.pencil,
2019-01-29 06:34:52 +01:00
textSpan: TextSpan(children: [
2019-02-08 07:30:22 +01:00
TextSpan(text: ' changed the title '),
TextSpan(
text: item['previousTitle'],
style: TextStyle(decoration: TextDecoration.lineThrough),
),
TextSpan(text: ' to '),
TextSpan(text: item['currentTitle'])
2019-01-29 06:34:52 +01:00
]),
item: item,
);
2019-02-08 07:30:22 +01:00
case 'LockedEvent':
2019-01-29 06:34:52 +01:00
return _buildItem(
actor: item['actor']['login'],
2019-02-08 07:30:22 +01:00
iconData: Octicons.lock,
2019-01-29 06:34:52 +01:00
textSpan: TextSpan(children: [
2019-02-08 07:30:22 +01:00
TextSpan(text: ' locked this conversation '),
2019-01-29 06:34:52 +01:00
]),
item: item,
);
2019-02-08 07:30:22 +01:00
case 'UnlockedEvent':
2019-01-29 06:34:52 +01:00
return _buildItem(
actor: item['actor']['login'],
iconData: Octicons.key,
textSpan: TextSpan(children: [
2019-02-08 07:30:22 +01:00
TextSpan(text: ' unlocked this conversation '),
2019-01-29 06:34:52 +01:00
]),
item: item,
);
2019-02-08 07:30:22 +01:00
// issue only types
case 'TransferredEvent':
return defaultItem; // TODO:
// pull request only types
case 'CommitCommentThread':
return defaultItem; // TODO:
case 'PullRequestReview':
return _buildItem(
actor: item['author']['login'],
2019-02-06 12:14:11 +01:00
iconColor: Color(0xff28a745),
iconData: Octicons.check,
textSpan: _buildReviewText(context, item),
item: item,
);
2019-02-08 07:30:22 +01:00
case 'PullRequestReviewThread':
case 'PullRequestReviewComment':
return defaultItem; // TODO:
case 'MergedEvent':
return _buildItem(
actor: item['actor']['login'],
iconData: Octicons.git_merge,
2019-02-06 12:14:11 +01:00
iconColor: Color(0xff6f42c1),
textSpan: TextSpan(children: [
TextSpan(text: ' merged commit '),
TextSpan(text: item['commit']['oid'].substring(0, 8)),
TextSpan(text: ' into '),
TextSpan(text: item['mergeRefName']),
]),
item: item,
);
2019-02-08 07:30:22 +01:00
case 'DeployedEvent':
case 'DeploymentEnvironmentChangedEvent':
return defaultItem; // TODO:
case 'HeadRefDeletedEvent':
return _buildItem(
actor: item['actor']['login'],
iconData: Octicons.git_branch,
textSpan: TextSpan(children: [
TextSpan(text: ' deleted the '),
TextSpan(text: item['headRefName']),
TextSpan(text: ' branch'),
]),
item: item,
);
2019-02-08 07:30:22 +01:00
case 'HeadRefRestoredEvent':
case 'HeadRefForcePushedEvent':
case 'BaseRefForcePushedEvent':
return defaultItem; // TODO:
case 'ReviewRequestedEvent':
return _buildItem(
2019-02-08 07:30:22 +01:00
iconData: Octicons.eye,
// actor: payload['author']['login'],
// TODO:
actor: 'test',
textSpan: TextSpan(children: [
2019-02-08 07:30:22 +01:00
TextSpan(text: ' requested a review from '),
createUserSpan(item['requestedReviewer']['login']),
]),
item: item,
);
2019-02-08 07:30:22 +01:00
case 'ReviewRequestRemovedEvent':
case 'ReviewDismissedEvent':
return defaultItem; // TODO:
default:
2019-02-08 07:30:22 +01:00
return defaultItem;
}
}
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(10),
child: _buildByType(context),
);
}
}