2019-02-06 06:06:11 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
2019-09-02 14:40:20 +02:00
|
|
|
import 'package:provider/provider.dart';
|
2019-02-08 13:52:10 +01:00
|
|
|
import '../scaffolds/refresh_stateless.dart';
|
2019-09-02 14:40:20 +02:00
|
|
|
import 'package:git_touch/models/notification.dart';
|
2019-09-02 15:52:32 +02:00
|
|
|
import 'package:git_touch/models/theme.dart';
|
2019-02-07 07:35:19 +01:00
|
|
|
import '../providers/settings.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-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-08 13:52:10 +01:00
|
|
|
String error = '';
|
2019-01-31 07:20:48 +01:00
|
|
|
int active = 0;
|
2019-02-10 12:15:50 +01:00
|
|
|
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 14:35:52 +01:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2019-02-08 03:48:25 +01:00
|
|
|
nextTick(_onSwitchTab);
|
2019-02-06 14:35:52 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 07:35:19 +01:00
|
|
|
Future<Map<String, NotificationGroup>> fetchNotifications(int index) async {
|
|
|
|
List items = await SettingsProvider.of(context).getWithCredentials(
|
|
|
|
'/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
|
|
|
|
|
|
|
// print(schema);
|
|
|
|
var data = await SettingsProvider.of(context).query(schema);
|
|
|
|
_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-02-07 12:17:29 +01:00
|
|
|
// print(data);
|
|
|
|
}
|
2019-02-07 07:35:19 +01:00
|
|
|
|
|
|
|
return _groupMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildGroupItem(
|
2019-02-07 13:28:48 +01:00
|
|
|
BuildContext context,
|
|
|
|
MapEntry<String, NotificationGroup> entry,
|
|
|
|
) {
|
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),
|
|
|
|
),
|
|
|
|
Link(
|
|
|
|
material: false,
|
|
|
|
onTap: () async {
|
|
|
|
await SettingsProvider.of(context)
|
|
|
|
.putWithCredentials('/repos/$repo/notifications');
|
|
|
|
await _onSwitchTab();
|
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
|
|
|
}
|
|
|
|
|
2019-02-07 13:28:48 +01:00
|
|
|
Future<void> _onSwitchTab([int index]) async {
|
2019-02-10 05:42:21 +01:00
|
|
|
if (loading) return;
|
|
|
|
|
2019-01-31 07:20:48 +01:00
|
|
|
setState(() {
|
2019-02-08 13:52:10 +01:00
|
|
|
error = '';
|
|
|
|
if (index != null) {
|
|
|
|
active = index;
|
|
|
|
}
|
2019-02-06 14:35:52 +01:00
|
|
|
loading = true;
|
2019-01-31 07:20:48 +01:00
|
|
|
});
|
2019-02-08 13:52:10 +01:00
|
|
|
try {
|
|
|
|
groupMap = await fetchNotifications(active);
|
|
|
|
} catch (err) {
|
|
|
|
error = err.toString();
|
2019-03-13 16:08:38 +01:00
|
|
|
throw err;
|
2019-02-08 13:52:10 +01:00
|
|
|
} finally {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
loading = false;
|
|
|
|
});
|
|
|
|
}
|
2019-02-06 14:35:52 +01:00
|
|
|
}
|
2019-01-31 07:20:48 +01:00
|
|
|
}
|
|
|
|
|
2019-02-06 14:35:52 +01:00
|
|
|
var textMap = {
|
|
|
|
0: 'Unread',
|
|
|
|
1: 'Paticipating',
|
|
|
|
2: 'All',
|
|
|
|
};
|
|
|
|
|
2019-02-07 13:28:48 +01:00
|
|
|
Widget _buildTitle() {
|
2019-09-02 15:52:32 +02:00
|
|
|
switch (Provider.of<ThemeModel>(context).theme) {
|
2019-02-07 13:28:48 +01:00
|
|
|
case ThemeMap.cupertino:
|
|
|
|
// var textStyle = DefaultTextStyle.of(context).style;
|
|
|
|
return DefaultTextStyle(
|
|
|
|
style: TextStyle(fontSize: 16),
|
|
|
|
child: SizedBox.expand(
|
|
|
|
child: CupertinoSegmentedControl(
|
|
|
|
groupValue: active,
|
|
|
|
onValueChanged: _onSwitchTab,
|
|
|
|
children: textMap.map((key, text) => MapEntry(key, Text(text))),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
default:
|
|
|
|
return Text('Notifications');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _confirm() async {
|
2019-09-02 15:52:32 +02:00
|
|
|
var value = await Provider.of<ThemeModel>(context)
|
|
|
|
.showConfirm(context, 'Mark all as read?');
|
2019-02-07 13:28:48 +01:00
|
|
|
if (value) {
|
|
|
|
await SettingsProvider.of(context).putWithCredentials('/notifications');
|
2019-02-08 03:48:25 +01:00
|
|
|
await _onSwitchTab();
|
2019-02-07 13:28:48 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-06 14:35:52 +01:00
|
|
|
|
2019-01-31 07:20:48 +01:00
|
|
|
@override
|
|
|
|
Widget build(context) {
|
2019-02-08 13:52:10 +01:00
|
|
|
return RefreshStatelessScaffold(
|
2019-02-07 13:28:48 +01:00
|
|
|
title: _buildTitle(),
|
|
|
|
bottom: TabBar(
|
|
|
|
onTap: _onSwitchTab,
|
2019-03-10 09:09:26 +01:00
|
|
|
tabs: textMap.entries
|
|
|
|
.map((entry) => Tab(text: entry.value.toUpperCase()))
|
|
|
|
.toList(),
|
2019-02-06 14:35:52 +01:00
|
|
|
),
|
2019-02-07 13:28:48 +01:00
|
|
|
// trailing: GestureDetector(
|
|
|
|
// child: Icon(Icons.more_vert, size: 20),
|
|
|
|
// onTap: () async {
|
|
|
|
// int value = await showCupertinoDialog(
|
|
|
|
// context: context,
|
|
|
|
// builder: (context) {
|
|
|
|
// return CupertinoAlertDialog(
|
|
|
|
// title: Text('Select filter'),
|
|
|
|
// actions: textMap.entries.map((entry) {
|
|
|
|
// return CupertinoDialogAction(
|
|
|
|
// child: Text(entry.value),
|
|
|
|
// onPressed: () {
|
|
|
|
// Navigator.pop(context, entry.key);
|
|
|
|
// },
|
|
|
|
// );
|
|
|
|
// }).toList(),
|
|
|
|
// );
|
|
|
|
// },
|
|
|
|
// );
|
|
|
|
// _onSwitchTab(value);
|
|
|
|
// },
|
|
|
|
// ),
|
2019-02-06 14:35:52 +01:00
|
|
|
actions: <Widget>[
|
2019-02-07 13:28:48 +01:00
|
|
|
IconButton(
|
2019-02-10 12:15:50 +01:00
|
|
|
icon: Icon(Icons.done_all),
|
2019-02-07 13:28:48 +01:00
|
|
|
onPressed: _confirm,
|
2019-02-06 14:35:52 +01:00
|
|
|
)
|
|
|
|
],
|
2019-02-08 03:48:25 +01:00
|
|
|
onRefresh: _onSwitchTab,
|
2019-02-06 14:35:52 +01:00
|
|
|
loading: loading,
|
2019-02-08 13:52:10 +01:00
|
|
|
error: error,
|
2019-02-06 06:06:11 +01:00
|
|
|
bodyBuilder: () {
|
2019-02-10 05:16:52 +01:00
|
|
|
return groupMap.isEmpty
|
|
|
|
? EmptyWidget()
|
2019-05-12 08:01:12 +02:00
|
|
|
: Column(children: [
|
|
|
|
Padding(padding: EdgeInsets.only(top: 10)),
|
|
|
|
...groupMap.entries
|
|
|
|
.map((entry) => _buildGroupItem(context, entry))
|
|
|
|
.toList()
|
|
|
|
]);
|
2019-02-06 06:06:11 +01:00
|
|
|
},
|
|
|
|
);
|
2019-01-31 07:20:48 +01:00
|
|
|
}
|
|
|
|
}
|