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

168 lines
4.3 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/pull_request.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'];
String numberStr = url.split('/').lastWhere((_) => true);
number = int.parse(numberStr);
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
2019-02-05 13:57:05 +01:00
Widget _buildRoute() {
switch (payload.type) {
2019-01-30 07:46:18 +01:00
case 'Issue':
2019-02-05 13:57:05 +01:00
return IssueScreen(payload.number, payload.owner, payload.name);
2019-01-30 07:46:18 +01:00
case 'PullRequest':
2019-02-05 13:57:05 +01:00
return PullRequestScreen(payload.number, payload.owner, payload.name);
2019-02-06 12:14:11 +01:00
case 'Release':
// return
2019-01-30 07:46:18 +01:00
default:
// throw new Exception('Unhandled notification type: $type');
return Text('test');
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);
2019-01-30 07:46:18 +01:00
default:
2019-02-06 12:14:11 +01:00
return _buildIcon(Octicons.person);
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,
size: 20,
);
}
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(
onTap: () {
2019-02-06 14:35:52 +01:00
_markAsRead();
2019-01-31 07:37:25 +01:00
Navigator.of(context).push(
2019-02-05 13:57:05 +01:00
CupertinoPageRoute(builder: (context) => _buildRoute()),
2019-01-31 07:37:25 +01:00
);
},
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-06 06:06:11 +01:00
),
Link(
child: _buildCheckIcon(),
2019-02-06 14:35:52 +01:00
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
),
);
}
}