2019-02-06 06:06:11 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import '../widgets/refresh_scaffold.dart';
|
2019-01-31 15:07:52 +01:00
|
|
|
import '../providers/notification.dart';
|
2019-01-31 07:20:48 +01:00
|
|
|
import '../widgets/notification_item.dart';
|
2019-02-03 16:10:10 +01:00
|
|
|
import '../widgets/list_group.dart';
|
2019-02-06 06:06:11 +01:00
|
|
|
import '../widgets/link.dart';
|
2019-01-31 07:20:48 +01:00
|
|
|
import '../utils/utils.dart';
|
|
|
|
|
2019-02-06 06:06:11 +01:00
|
|
|
Future<List<NotificationPayload>> fetchNotifications([int page = 1]) async {
|
|
|
|
List items = await getWithCredentials('/notifications?page=$page&all=true');
|
|
|
|
return items.map((item) => NotificationPayload.fromJson(item)).toList();
|
|
|
|
}
|
|
|
|
|
2019-01-31 07:20:48 +01:00
|
|
|
class NotificationGroup {
|
2019-02-06 06:06:11 +01:00
|
|
|
String repo;
|
|
|
|
List<NotificationPayload> items = [];
|
2019-01-31 07:20:48 +01:00
|
|
|
|
2019-02-06 06:06:11 +01:00
|
|
|
NotificationGroup(this.repo);
|
2019-01-31 07:20:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class NotificationScreen extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
NotificationScreenState createState() => NotificationScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class NotificationScreenState extends State<NotificationScreen> {
|
|
|
|
int active = 0;
|
|
|
|
bool loading = false;
|
2019-02-06 06:06:11 +01:00
|
|
|
Map<String, NotificationGroup> groupMap = {};
|
2019-01-31 07:20:48 +01:00
|
|
|
|
2019-02-06 06:06:11 +01:00
|
|
|
Widget _buildGroupItem(String key, NotificationGroup group) {
|
|
|
|
var repo = group.repo;
|
2019-02-03 16:10:10 +01:00
|
|
|
return ListGroup(
|
2019-02-06 06:06:11 +01:00
|
|
|
title: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: <Widget>[
|
|
|
|
Text(
|
|
|
|
repo,
|
|
|
|
style: TextStyle(color: Colors.black, fontSize: 15),
|
|
|
|
),
|
|
|
|
Link(
|
|
|
|
onTap: () async {
|
|
|
|
await putWithCredentials('/repos/$repo/notifications');
|
|
|
|
await _refresh();
|
|
|
|
},
|
|
|
|
child: Icon(
|
|
|
|
Octicons.check,
|
|
|
|
color: Colors.black45,
|
|
|
|
size: 20,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
items: group.items,
|
|
|
|
itemBuilder: (item, index) {
|
|
|
|
return NotificationItem(
|
|
|
|
payload: item,
|
|
|
|
markAsRead: () {
|
|
|
|
setState(() {
|
|
|
|
groupMap[key].items[index].unread = false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2019-01-31 07:20:48 +01:00
|
|
|
}
|
|
|
|
|
2019-02-06 06:06:11 +01:00
|
|
|
Future<void> _onSwitchTab(BuildContext context, int index) async {
|
|
|
|
// setState(() {
|
|
|
|
// active = index;
|
|
|
|
// loading = true;
|
|
|
|
// });
|
2019-01-31 07:20:48 +01:00
|
|
|
|
2019-02-06 06:06:11 +01:00
|
|
|
var ns = await fetchNotifications();
|
2019-01-31 07:20:48 +01:00
|
|
|
|
2019-02-06 06:06:11 +01:00
|
|
|
// NotificationProvider.of(context).setCount(ns.length);
|
2019-01-31 07:20:48 +01:00
|
|
|
|
2019-02-06 06:06:11 +01:00
|
|
|
Map<String, NotificationGroup> _groupMap = {};
|
2019-01-31 07:20:48 +01:00
|
|
|
ns.forEach((item) {
|
2019-02-06 06:06:11 +01:00
|
|
|
String repo = item.owner + '/' + item.name;
|
|
|
|
if (_groupMap[repo] == null) {
|
|
|
|
_groupMap[repo] = NotificationGroup(repo);
|
2019-01-31 07:20:48 +01:00
|
|
|
}
|
|
|
|
|
2019-02-06 06:06:11 +01:00
|
|
|
_groupMap[repo].items.add(item);
|
2019-01-31 07:20:48 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
setState(() {
|
2019-02-06 06:06:11 +01:00
|
|
|
groupMap = _groupMap;
|
|
|
|
// loading = false;
|
2019-01-31 07:20:48 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-06 06:06:11 +01:00
|
|
|
// TODO: filter
|
|
|
|
// CupertinoSegmentedControl(
|
|
|
|
// groupValue: active,
|
|
|
|
// onValueChanged: (index) => _onSwitchTab(context, index),
|
|
|
|
// children: {
|
|
|
|
// 0: Text('Unread'),
|
|
|
|
// 1: Text('Paticipating'),
|
|
|
|
// 2: Text('All')
|
|
|
|
// },
|
|
|
|
// )
|
|
|
|
|
|
|
|
Future<void> _refresh() async {
|
|
|
|
print('onrefresh');
|
|
|
|
await _onSwitchTab(context, active);
|
|
|
|
}
|
|
|
|
|
2019-01-31 07:20:48 +01:00
|
|
|
@override
|
|
|
|
Widget build(context) {
|
2019-02-06 06:06:11 +01:00
|
|
|
return RefreshScaffold(
|
|
|
|
title: Text('Notifications'),
|
|
|
|
onRefresh: _refresh,
|
|
|
|
bodyBuilder: () {
|
|
|
|
var children = groupMap.entries
|
|
|
|
.map((entry) => _buildGroupItem(entry.key, entry.value))
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
return Column(children: children);
|
|
|
|
},
|
|
|
|
);
|
2019-01-31 07:20:48 +01:00
|
|
|
}
|
|
|
|
}
|