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

135 lines
3.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/pull_request.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;
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-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-05 13:57:05 +01:00
IconData _buildIconData() {
switch (payload.type) {
2019-01-30 07:46:18 +01:00
case 'Issue':
return Octicons.issue_opened;
// color: Color.fromRGBO(0x28, 0xa7, 0x45, 1),
case 'PullRequest':
return Octicons.git_pull_request;
// color: Color.fromRGBO(0x6f, 0x42, 0xc1, 1),
default:
return 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-01-30 07:46:18 +01:00
@override
Widget build(BuildContext context) {
2019-01-31 07:37:25 +01:00
return Link(
onTap: () {
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),
child: Icon(_buildIconData(), color: Colors.black45, size: 20),
),
Expanded(
child: Text(
payload.title,
overflow: TextOverflow.ellipsis,
),
2019-02-06 06:06:11 +01:00
),
Link(
child: _buildCheckIcon(),
onTap: () async {
if (payload.unread && !loading) {
setState(() {
loading = true;
});
try {
await patchWithCredentials(
'/notifications/threads/' + payload.id);
widget.markAsRead();
} finally {
setState(() {
loading = false;
});
}
}
},
),
],
),
2019-02-05 13:57:05 +01:00
),
2019-01-30 07:46:18 +01:00
),
);
}
}