1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-15 09:56:15 +01:00
git-touch-android-ios-app/lib/widgets/notification_item.dart

179 lines
4.6 KiB
Dart
Raw Normal View History

2019-02-06 06:06:11 +01:00
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
2019-01-31 07:37:25 +01:00
import '../utils/utils.dart';
2019-02-05 13:57:05 +01:00
import '../screens/issue.dart';
import '../screens/not_found.dart';
2019-02-07 07:35:19 +01:00
import '../providers/settings.dart';
2019-01-31 07:37:25 +01:00
import 'link.dart';
2019-01-30 07:46:18 +01:00
2019-02-05 13:57:05 +01:00
class NotificationPayload {
2019-02-06 06:06:11 +01:00
String id;
2019-02-05 13:57:05 +01:00
String type;
String owner;
String name;
int number;
String title;
String updateAt;
bool unread;
2019-02-06 12:14:11 +01:00
String state;
2019-02-05 13:57:05 +01:00
NotificationPayload.fromJson(input) {
2019-02-06 06:06:11 +01:00
id = input['id'];
2019-02-05 13:57:05 +01:00
type = input['subject']['type'];
name = input['repository']['name'];
owner = input['repository']['owner']['login'];
String url = input['subject']['url'];
if (type == 'Issue' || type == 'PullRequest') {
String numberStr = url.split('/').lastWhere((_) => true);
number = int.parse(numberStr);
}
2019-02-05 13:57:05 +01:00
title = input['subject']['title'];
updateAt = TimeAgo.formatFromString(input['updated_at']);
unread = input['unread'];
}
}
2019-02-06 06:06:11 +01:00
class NotificationItem extends StatefulWidget {
final NotificationPayload payload;
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> {
NotificationPayload get payload => widget.payload;
bool loading = false;
2019-01-30 07:46:18 +01:00
Widget _buildRoute(BuildContext context) {
2019-02-05 13:57:05 +01:00
switch (payload.type) {
2019-01-30 07:46:18 +01:00
case 'Issue':
return IssueScreen(
number: payload.number,
owner: payload.owner,
name: payload.name,
);
2019-01-30 07:46:18 +01:00
case 'PullRequest':
return IssueScreen(
number: payload.number,
owner: payload.owner,
name: payload.name,
isPullRequest: true,
);
2019-02-06 12:14:11 +01:00
case 'Release':
// return
case 'Commit':
// return
2019-01-30 07:46:18 +01:00
default:
print('Unhandled notification type: ${payload.type}');
return NotFoundScreen();
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-02-05 13:57:05 +01:00
switch (payload.type) {
2019-01-30 07:46:18 +01:00
case 'Issue':
2019-02-06 12:14:11 +01:00
switch (payload.state) {
case 'OPEN':
return _buildIcon(Octicons.issue_opened, Palette.green);
case 'CLOSED':
return _buildIcon(Octicons.issue_closed, Palette.red);
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':
return _buildIcon(Octicons.git_pull_request, Palette.green);
case 'CLOSED':
return _buildIcon(Octicons.git_pull_request, Palette.red);
case 'MERGED':
return _buildIcon(Octicons.git_merge, Palette.purple);
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:
print('Unhandled notification type: ${payload.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-02-07 07:35:19 +01:00
await SettingsProvider.of(context)
.patchWithCredentials('/notifications/threads/' + payload.id);
2019-02-06 14:35:52 +01:00
widget.markAsRead();
} finally {
if (mounted) {
setState(() {
loading = false;
});
}
}
}
}
2019-01-30 07:46:18 +01:00
@override
Widget build(BuildContext context) {
2019-01-31 07:37:25 +01:00
return Link(
screenBuilder: _buildRoute,
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(
payload.title,
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
),
);
}
}