git-touch-android-ios-app/lib/screens/notifications.dart

125 lines
3.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';
import '../widgets/refresh_scaffold.dart';
2019-01-31 15:07:52 +01:00
import '../providers/notification.dart';
import '../widgets/notification_item.dart';
import '../widgets/list_group.dart';
2019-02-06 06:06:11 +01:00
import '../widgets/link.dart';
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();
}
class NotificationGroup {
2019-02-06 06:06:11 +01:00
String repo;
List<NotificationPayload> items = [];
2019-02-06 06:06:11 +01:00
NotificationGroup(this.repo);
}
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-02-06 06:06:11 +01:00
Widget _buildGroupItem(String key, NotificationGroup group) {
var repo = group.repo;
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-02-06 06:06:11 +01:00
Future<void> _onSwitchTab(BuildContext context, int index) async {
// setState(() {
// active = index;
// loading = true;
// });
2019-02-06 06:06:11 +01:00
var ns = await fetchNotifications();
2019-02-06 06:06:11 +01:00
// NotificationProvider.of(context).setCount(ns.length);
2019-02-06 06:06:11 +01:00
Map<String, NotificationGroup> _groupMap = {};
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-02-06 06:06:11 +01:00
_groupMap[repo].items.add(item);
});
setState(() {
2019-02-06 06:06:11 +01:00
groupMap = _groupMap;
// loading = false;
});
}
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);
}
@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);
},
);
}
}