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

141 lines
4.0 KiB
Dart
Raw Normal View History

import 'package:fimber/fimber.dart';
2019-02-06 06:06:11 +01:00
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
2019-12-20 15:41:38 +01:00
import 'package:git_touch/widgets/issue_icon.dart';
2019-12-26 11:00:36 +01:00
import 'package:git_touch/models/github.dart';
import 'package:git_touch/utils/utils.dart';
2019-09-27 14:52:38 +02:00
import 'package:git_touch/models/auth.dart';
2019-09-08 14:07:35 +02:00
import 'package:provider/provider.dart';
2019-12-26 11:00:36 +01:00
import 'package:git_touch/widgets/link.dart';
2019-01-30 07:46:18 +01:00
2019-02-06 06:06:11 +01:00
class NotificationItem extends StatefulWidget {
2019-12-26 11:00:36 +01:00
final GithubNotificationItem payload;
2019-02-06 06:06:11 +01:00
final Function markAsRead;
NotificationItem({
2019-01-30 07:46:18 +01:00
Key key,
2019-02-05 13:57:05 +01:00
@required this.payload,
2019-02-06 06:06:11 +01:00
@required this.markAsRead,
2019-01-30 07:46:18 +01:00
}) : super(key: key);
2019-02-06 06:06:11 +01:00
@override
_NotificationItemState createState() => _NotificationItemState();
}
class _NotificationItemState extends State<NotificationItem> {
2019-12-26 11:00:36 +01:00
GithubNotificationItem get payload => widget.payload;
2019-02-06 06:06:11 +01:00
bool loading = false;
2019-01-30 07:46:18 +01:00
2019-02-06 12:14:11 +01:00
Widget _buildIcon(IconData data, [Color color = Colors.black54]) {
return Icon(data, color: color, size: 20);
}
Widget _buildIconData() {
2019-12-26 11:00:36 +01:00
switch (payload.subject.type) {
2019-01-30 07:46:18 +01:00
case 'Issue':
2019-02-06 12:14:11 +01:00
switch (payload.state) {
case 'OPEN':
2019-12-20 15:41:38 +01:00
return IssueIcon(IssueIconState.open, size: 20);
2019-02-06 12:14:11 +01:00
case 'CLOSED':
2019-12-20 15:41:38 +01:00
return IssueIcon(IssueIconState.closed, size: 20);
2019-02-06 12:14:11 +01:00
default:
return _buildIcon(Octicons.person);
}
break;
2019-01-30 07:46:18 +01:00
case 'PullRequest':
2019-02-06 12:14:11 +01:00
switch (payload.state) {
case 'OPEN':
2019-12-20 15:41:38 +01:00
return IssueIcon(IssueIconState.prOpen, size: 20);
2019-02-06 12:14:11 +01:00
case 'CLOSED':
2019-12-20 15:41:38 +01:00
return IssueIcon(IssueIconState.prClosed, size: 20);
2019-02-06 12:14:11 +01:00
case 'MERGED':
2019-12-20 15:41:38 +01:00
return IssueIcon(IssueIconState.prMerged, size: 20);
2019-02-06 12:14:11 +01:00
default:
return _buildIcon(Octicons.person);
}
break;
2019-01-30 07:46:18 +01:00
// color: Color.fromRGBO(0x6f, 0x42, 0xc1, 1),
2019-02-06 12:14:11 +01:00
case 'Release':
return _buildIcon(Octicons.tag);
case 'Commit':
return _buildIcon(Octicons.git_commit);
2019-01-30 07:46:18 +01:00
default:
2019-12-26 11:00:36 +01:00
Fimber.d('Unhandled notification type: ${payload.subject.type}');
return _buildIcon(Octicons.octoface);
2019-01-30 07:46:18 +01:00
}
}
2019-02-06 06:06:11 +01:00
Widget _buildCheckIcon() {
return Icon(
payload.unread ? Octicons.check : Octicons.primitive_dot,
color: loading ? Colors.black12 : Colors.black45,
2019-02-10 12:45:08 +01:00
size: 24,
2019-02-06 06:06:11 +01:00
);
}
2019-02-06 14:35:52 +01:00
void _markAsRead() async {
if (payload.unread && !loading) {
setState(() {
loading = true;
});
try {
2019-09-27 14:52:38 +02:00
await Provider.of<AuthModel>(context)
2019-12-26 11:00:36 +01:00
.patchWithCredentials('/notifications/threads/${payload.id}');
2019-02-06 14:35:52 +01:00
widget.markAsRead();
} finally {
if (mounted) {
setState(() {
loading = false;
});
}
}
}
}
2019-12-26 07:10:52 +01:00
String get _url {
2019-12-26 11:00:36 +01:00
switch (payload.subject.type) {
2019-12-26 07:10:52 +01:00
case 'Issue':
2019-12-26 11:00:36 +01:00
return '/${payload.repository.owner}/${payload.repository.name}/issues/${payload.subject.number}';
2019-12-26 07:10:52 +01:00
case 'PullRequest':
2019-12-26 11:00:36 +01:00
return '/${payload.repository.owner}/${payload.repository.name}/pulls/${payload.subject.number}';
2019-12-26 07:10:52 +01:00
case 'Release':
2019-12-26 11:00:36 +01:00
// TODO: title
// return 'https://github.com/${payload.repository.owner}/${payload.repository.name}/releases/tag/${payload.title}';
2019-12-26 07:10:52 +01:00
case 'Commit':
return '';
default:
return null;
}
}
2019-01-30 07:46:18 +01:00
@override
Widget build(BuildContext context) {
2019-01-31 07:37:25 +01:00
return Link(
2019-12-26 07:10:52 +01:00
url: _url,
onTap: _markAsRead,
2019-02-06 06:06:11 +01:00
child: Opacity(
opacity: payload.unread ? 1 : 0.5,
child: Container(
padding: EdgeInsets.all(8),
child: Row(
children: <Widget>[
Container(
padding: EdgeInsets.only(right: 8),
2019-02-06 12:14:11 +01:00
child: _buildIconData(),
2019-02-06 06:06:11 +01:00
),
Expanded(
child: Text(
2019-12-26 11:00:36 +01:00
payload.subject.title,
2019-02-06 06:06:11 +01:00
overflow: TextOverflow.ellipsis,
2019-02-10 12:45:08 +01:00
style: TextStyle(fontSize: 15),
),
2019-02-06 06:06:11 +01:00
),
Link(child: _buildCheckIcon(), onTap: _markAsRead),
2019-02-06 06:06:11 +01:00
],
),
2019-02-05 13:57:05 +01:00
),
2019-01-30 07:46:18 +01:00
),
);
}
}