2019-02-06 06:06:11 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
2019-09-30 11:37:51 +02:00
|
|
|
import 'package:git_touch/models/theme.dart';
|
2019-09-25 11:06:36 +02:00
|
|
|
import 'package:git_touch/scaffolds/tab_stateful.dart';
|
2019-09-30 11:37:51 +02:00
|
|
|
import 'package:git_touch/widgets/action_entry.dart';
|
2019-09-11 13:59:47 +02:00
|
|
|
import 'package:git_touch/widgets/app_bar_title.dart';
|
2019-09-02 14:40:20 +02:00
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:git_touch/models/notification.dart';
|
2019-09-27 14:52:38 +02:00
|
|
|
import 'package:git_touch/models/auth.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-10 05:16:52 +01:00
|
|
|
import '../widgets/empty.dart';
|
2019-01-31 07:20:48 +01:00
|
|
|
import '../utils/utils.dart';
|
|
|
|
|
|
|
|
class NotificationScreen extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
NotificationScreenState createState() => NotificationScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class NotificationScreenState extends State<NotificationScreen> {
|
2019-02-07 07:35:19 +01:00
|
|
|
Future<Map<String, NotificationGroup>> fetchNotifications(int index) async {
|
2019-09-27 14:52:38 +02:00
|
|
|
List items = await Provider.of<AuthModel>(context).getWithCredentials(
|
2019-02-07 07:35:19 +01:00
|
|
|
'/notifications?all=${index == 2}&participating=${index == 1}');
|
|
|
|
var ns = items.map((item) => NotificationPayload.fromJson(item)).toList();
|
|
|
|
|
|
|
|
if (index == 0) {
|
2019-09-02 14:40:20 +02:00
|
|
|
Provider.of<NotificationModel>(context).setCount(ns.length);
|
2019-02-07 07:35:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, NotificationGroup> _groupMap = {};
|
|
|
|
|
|
|
|
ns.forEach((item) {
|
|
|
|
String repo = item.owner + '/' + item.name;
|
|
|
|
if (_groupMap[repo] == null) {
|
|
|
|
_groupMap[repo] = NotificationGroup(item.owner, item.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
_groupMap[repo].items.add(item);
|
|
|
|
});
|
|
|
|
|
2019-02-07 12:17:29 +01:00
|
|
|
if (_groupMap.isNotEmpty) {
|
|
|
|
// query state of issues and pull requests
|
|
|
|
var schema = '{';
|
|
|
|
_groupMap.forEach((repo, group) {
|
2019-03-19 13:11:35 +01:00
|
|
|
// Check if issue and pull request exist
|
|
|
|
if (group.items.where((item) {
|
|
|
|
return item.type == 'Issue' || item.type == 'PullRequest';
|
|
|
|
}).isEmpty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-07 12:17:29 +01:00
|
|
|
schema +=
|
2019-09-03 05:56:25 +02:00
|
|
|
'${group.key}: repository(owner: "${group.owner}", name: "${group.name}") {';
|
2019-02-07 12:17:29 +01:00
|
|
|
|
|
|
|
group.items.forEach((item) {
|
2019-09-03 05:56:25 +02:00
|
|
|
var key = item.key;
|
2019-02-07 12:17:29 +01:00
|
|
|
|
|
|
|
switch (item.type) {
|
|
|
|
case 'Issue':
|
|
|
|
schema += '''
|
2019-02-07 07:35:19 +01:00
|
|
|
$key: issue(number: ${item.number}) {
|
|
|
|
state
|
|
|
|
}
|
|
|
|
''';
|
2019-02-07 12:17:29 +01:00
|
|
|
break;
|
|
|
|
case 'PullRequest':
|
|
|
|
schema += '''
|
2019-02-07 07:35:19 +01:00
|
|
|
$key: pullRequest(number: ${item.number}) {
|
|
|
|
state
|
|
|
|
}
|
|
|
|
''';
|
2019-02-07 12:17:29 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2019-02-07 07:35:19 +01:00
|
|
|
|
2019-02-07 12:17:29 +01:00
|
|
|
schema += '}';
|
|
|
|
});
|
2019-02-07 07:35:19 +01:00
|
|
|
schema += '}';
|
2019-02-07 12:17:29 +01:00
|
|
|
|
2019-10-13 05:01:29 +02:00
|
|
|
// Fimber.d(schema);
|
2019-09-27 14:52:38 +02:00
|
|
|
var data = await Provider.of<AuthModel>(context).query(schema);
|
2019-02-07 12:17:29 +01:00
|
|
|
_groupMap.forEach((repo, group) {
|
|
|
|
group.items.forEach((item) {
|
2019-09-03 05:56:25 +02:00
|
|
|
var groupData = data[group.key];
|
2019-03-19 13:11:35 +01:00
|
|
|
if (groupData == null) return;
|
|
|
|
|
2019-09-03 05:56:25 +02:00
|
|
|
var itemData = data[group.key][item.key];
|
2019-02-07 12:17:29 +01:00
|
|
|
if (itemData != null) {
|
|
|
|
item.state = itemData['state'];
|
|
|
|
}
|
|
|
|
});
|
2019-02-07 07:35:19 +01:00
|
|
|
});
|
2019-10-13 05:01:29 +02:00
|
|
|
// Fimber.d(data);
|
2019-02-07 12:17:29 +01:00
|
|
|
}
|
2019-02-07 07:35:19 +01:00
|
|
|
|
|
|
|
return _groupMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildGroupItem(
|
2019-09-23 19:46:50 +02:00
|
|
|
BuildContext context,
|
|
|
|
MapEntry<String, NotificationGroup> entry,
|
|
|
|
Map<String, NotificationGroup> groupMap) {
|
2019-02-06 14:35:52 +01:00
|
|
|
var group = entry.value;
|
2019-02-06 06:06:11 +01:00
|
|
|
var repo = group.repo;
|
2019-02-03 16:10:10 +01:00
|
|
|
return ListGroup(
|
2019-03-10 14:26:05 +01:00
|
|
|
title: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: <Widget>[
|
|
|
|
Text(
|
|
|
|
repo,
|
|
|
|
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
|
|
|
|
),
|
2019-09-29 10:39:30 +02:00
|
|
|
GestureDetector(
|
2019-03-10 14:26:05 +01:00
|
|
|
onTap: () async {
|
2019-09-27 14:52:38 +02:00
|
|
|
await Provider.of<AuthModel>(context)
|
2019-03-10 14:26:05 +01:00
|
|
|
.putWithCredentials('/repos/$repo/notifications');
|
2019-09-23 19:46:50 +02:00
|
|
|
// await _onSwitchTab(); // TODO:
|
2019-02-06 06:06:11 +01:00
|
|
|
},
|
2019-03-10 14:26:05 +01:00
|
|
|
child: Icon(
|
|
|
|
Octicons.check,
|
|
|
|
color: Colors.black45,
|
|
|
|
size: 24,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
items: group.items,
|
|
|
|
itemBuilder: (item, index) {
|
|
|
|
return NotificationItem(
|
|
|
|
payload: item,
|
|
|
|
markAsRead: () {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
groupMap[entry.key].items[index].unread = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2019-01-31 07:20:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(context) {
|
2019-09-25 11:06:36 +02:00
|
|
|
return TabStatefulScaffold(
|
2019-09-23 19:46:50 +02:00
|
|
|
title: AppBarTitle('Notifications'),
|
|
|
|
tabs: ['Unread', 'Paticipating', 'All'],
|
2019-09-30 11:37:51 +02:00
|
|
|
fetchData: fetchNotifications,
|
2019-09-24 14:45:55 +02:00
|
|
|
bodyBuilder: (groupMap, activeTab) {
|
|
|
|
if (groupMap.isEmpty) return EmptyWidget();
|
|
|
|
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
Padding(padding: EdgeInsets.only(top: 10)),
|
|
|
|
...groupMap.entries
|
|
|
|
.map((entry) => _buildGroupItem(context, entry, groupMap))
|
|
|
|
.toList()
|
|
|
|
],
|
|
|
|
);
|
2019-02-06 06:06:11 +01:00
|
|
|
},
|
2019-09-30 11:37:51 +02:00
|
|
|
actionBuilder: (_, refresh) => ActionEntry(
|
|
|
|
iconData: Icons.done_all,
|
|
|
|
onTap: () async {
|
|
|
|
var value = await Provider.of<ThemeModel>(context)
|
2019-10-03 06:55:17 +02:00
|
|
|
.showConfirm(context, Text('Mark all as read?'));
|
2019-09-30 11:37:51 +02:00
|
|
|
if (value) {
|
|
|
|
await Provider.of<AuthModel>(context)
|
|
|
|
.putWithCredentials('/notifications');
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2019-02-06 06:06:11 +01:00
|
|
|
);
|
2019-01-31 07:20:48 +01:00
|
|
|
}
|
|
|
|
}
|