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

198 lines
5.7 KiB
Dart
Raw Normal View History

2019-02-06 06:06:11 +01:00
import 'package:flutter/cupertino.dart';
2022-09-17 14:35:45 +02:00
import 'package:flutter/widgets.dart';
import 'package:flutter_gen/gen_l10n/S.dart';
import 'package:git_touch/models/auth.dart';
import 'package:git_touch/models/github.dart';
import 'package:git_touch/models/notification.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-11 13:59:47 +02:00
import 'package:git_touch/widgets/app_bar_title.dart';
2020-02-07 15:05:07 +01:00
import 'package:github/github.dart';
2019-09-02 14:40:20 +02:00
import 'package:provider/provider.dart';
2022-09-17 14:35:45 +02:00
import 'package:git_touch/utils/utils.dart';
import 'package:git_touch/widgets/empty.dart';
import 'package:git_touch/widgets/list_group.dart';
import 'package:git_touch/widgets/notification_item.dart';
2020-02-07 07:17:05 +01:00
class GhNotificationScreen extends StatefulWidget {
@override
2020-02-07 07:17:05 +01:00
GhNotificationScreenState createState() => GhNotificationScreenState();
}
2020-02-07 07:17:05 +01:00
class GhNotificationScreenState extends State<GhNotificationScreen> {
2021-05-30 17:55:57 +02:00
Future<Map<String, NotificationGroup>> fetchNotifications(int index) async {
2021-06-14 08:56:42 +02:00
final ns = await context.read<AuthModel>().ghClient.getJSON(
2020-02-18 18:00:16 +01:00
'/notifications?all=${index == 2}&participating=${index == 1}',
2021-05-16 09:16:35 +02:00
convert: (dynamic vs) =>
2020-02-18 18:00:16 +01:00
[for (var v in vs) GithubNotificationItem.fromJson(v)],
);
2019-02-07 07:35:19 +01:00
if (index == 0) {
2020-10-04 14:37:23 +02:00
context.read<NotificationModel>().setCount(ns.length);
2019-02-07 07:35:19 +01:00
}
2022-09-06 18:28:12 +02:00
Map<String, NotificationGroup> groupMap = {};
2019-02-07 07:35:19 +01:00
2022-09-06 18:28:12 +02:00
for (var item in ns) {
2021-05-30 17:55:57 +02:00
final repo = item.repository!.fullName ?? ''; // TODO: nullable
2022-09-06 18:28:12 +02:00
if (groupMap[repo] == null) {
groupMap[repo] = NotificationGroup(repo);
2019-02-07 07:35:19 +01:00
}
2022-09-06 18:28:12 +02:00
groupMap[repo]!.items.add(item);
}
2019-02-07 07:35:19 +01:00
2022-09-06 18:28:12 +02:00
if (groupMap.isNotEmpty) {
// query state of issues and pull requests
var schema = '{';
2022-09-06 18:28:12 +02:00
groupMap.forEach((repo, group) {
2019-03-19 13:11:35 +01:00
// Check if issue and pull request exist
if (group.items.where((item) {
2021-05-16 09:16:35 +02:00
return item.subject!.type == 'Issue' ||
item.subject!.type == 'PullRequest';
2019-03-19 13:11:35 +01:00
}).isEmpty) {
return;
}
schema +=
2019-09-03 05:56:25 +02:00
'${group.key}: repository(owner: "${group.owner}", name: "${group.name}") {';
2022-09-06 18:28:12 +02:00
for (var item in group.items) {
2021-05-16 09:16:35 +02:00
switch (item.subject!.type) {
case 'Issue':
schema += '''
2021-05-16 09:16:35 +02:00
${item.key}: issue(number: ${item.subject!.number}) {
2019-02-07 07:35:19 +01:00
state
}
''';
break;
case 'PullRequest':
schema += '''
2021-05-16 09:16:35 +02:00
${item.key}: pullRequest(number: ${item.subject!.number}) {
2019-02-07 07:35:19 +01:00
state
}
''';
break;
}
2022-09-06 18:28:12 +02:00
}
2019-02-07 07:35:19 +01:00
schema += '}';
});
2019-02-07 07:35:19 +01:00
schema += '}';
2022-09-06 18:28:12 +02:00
if (schema == '{}') return groupMap;
2019-12-26 06:58:23 +01:00
// Fimber.d(schema);
2020-10-04 14:37:23 +02:00
var data = await context.read<AuthModel>().query(schema);
2022-09-06 18:28:12 +02:00
groupMap.forEach((repo, group) {
for (var item in group.items) {
2019-09-03 05:56:25 +02:00
var groupData = data[group.key];
2022-09-06 18:28:12 +02:00
if (groupData == null) continue;
2019-03-19 13:11:35 +01:00
2019-09-03 05:56:25 +02:00
var itemData = data[group.key][item.key];
if (itemData != null) {
item.state = itemData['state'];
}
2022-09-06 18:28:12 +02:00
}
2019-02-07 07:35:19 +01:00
});
// Fimber.d(data);
}
2019-02-07 07:35:19 +01:00
2022-09-06 18:28:12 +02:00
return groupMap;
2019-02-07 07:35:19 +01:00
}
Widget _buildGroupItem(
2020-02-07 15:05:07 +01:00
BuildContext context,
MapEntry<String, NotificationGroup> entry,
Map<String, NotificationGroup> groupMap,
) {
2019-12-27 08:29:13 +01:00
final theme = Provider.of<ThemeModel>(context);
2019-12-26 11:00:36 +01:00
final group = entry.value;
return ListGroup(
2019-03-10 14:26:05 +01:00
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
2021-05-16 09:16:35 +02:00
group.fullName!,
2019-12-27 08:29:13 +01:00
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
2020-01-27 08:11:51 +01:00
color: theme.palette.text,
2019-12-27 08:29:13 +01:00
),
2019-03-10 14:26:05 +01:00
),
2019-09-29 10:39:30 +02:00
GestureDetector(
2019-03-10 14:26:05 +01:00
onTap: () async {
2020-10-04 14:37:23 +02:00
await context
.read<AuthModel>()
2021-06-14 08:56:42 +02:00
.ghClient
2020-10-04 14:37:23 +02:00
.activity
.markRepositoryNotificationsRead(
2021-05-16 09:16:35 +02:00
RepositorySlug.full(group.fullName!));
// await _onSwitchTab(); // TODO:
2019-02-06 06:06:11 +01:00
},
2019-03-10 14:26:05 +01:00
child: Icon(
2021-02-14 15:17:22 +01:00
Ionicons.checkmark_done,
2020-01-27 08:11:51 +01:00
color: theme.palette.tertiaryText,
2019-03-10 14:26:05 +01:00
size: 24,
),
),
],
),
items: group.items,
2021-05-16 09:16:35 +02:00
itemBuilder: (dynamic item, index) {
2019-03-10 14:26:05 +01:00
return NotificationItem(
payload: item,
markAsRead: () {
if (mounted) {
setState(() {
2021-05-16 09:16:35 +02:00
groupMap[entry.key]!.items[index].unread = false;
2019-03-10 14:26:05 +01:00
});
}
},
);
},
);
}
@override
Widget build(context) {
2021-05-30 17:55:57 +02:00
return TabStatefulScaffold<Map<String, NotificationGroup>>(
2021-05-16 09:16:35 +02:00
title: AppBarTitle(AppLocalizations.of(context)!.notification),
tabs: [
2021-05-16 09:16:35 +02:00
AppLocalizations.of(context)!.unread,
AppLocalizations.of(context)!.participating,
AppLocalizations.of(context)!.all
],
2019-09-30 11:37:51 +02:00
fetchData: fetchNotifications,
2021-05-16 09:16:35 +02:00
bodyBuilder: (dynamic groupMap, activeTab) {
2019-09-24 14:45:55 +02:00
if (groupMap.isEmpty) return EmptyWidget();
return Column(
children: [
2022-09-06 18:28:12 +02:00
const Padding(padding: EdgeInsets.only(top: 10)),
2019-09-24 14:45:55 +02:00
...groupMap.entries
.map((entry) => _buildGroupItem(context, entry, groupMap))
.toList()
],
);
2019-02-06 06:06:11 +01:00
},
2021-02-14 15:17:22 +01:00
// actionBuilder: (_, refresh) => ActionEntry(
// iconData: Ionicons.checkmark_done,
// onTap: () async {
// final value = await context
// .read<ThemeModel>()
// .showConfirm(context, Text('Mark all as read?'));
// if (value) {
// await context
// .read<AuthModel>()
// .ghClient
// .activity
// .markNotificationsRead();
// refresh();
// }
// },
// ),
2019-02-06 06:06:11 +01:00
);
}
}