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

58 lines
1.8 KiB
Dart
Raw Normal View History

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';
2019-12-12 07:02:48 +01:00
import 'package:git_touch/models/github.dart';
2019-09-03 10:51:25 +02:00
import 'package:git_touch/models/notification.dart';
2019-09-25 11:06:36 +02:00
import 'package:git_touch/scaffolds/list_stateful.dart';
2019-09-11 13:59:47 +02:00
import 'package:git_touch/widgets/app_bar_title.dart';
2020-02-08 08:06:36 +01:00
import 'package:git_touch/widgets/event_item.dart';
2022-09-17 14:35:45 +02:00
import 'package:provider/provider.dart';
2019-03-10 09:09:26 +01:00
2020-02-07 07:17:05 +01:00
class GhNewsScreen extends StatefulWidget {
@override
2020-02-07 07:17:05 +01:00
GhNewsScreenState createState() => GhNewsScreenState();
}
2020-02-07 07:17:05 +01:00
class GhNewsScreenState extends State<GhNewsScreen> {
2019-09-03 10:51:25 +02:00
@override
initState() {
super.initState();
2020-01-14 05:33:18 +01:00
Future.microtask(() async {
2019-09-03 10:51:25 +02:00
// Check if there are unread notification items.
// 1 item is enough since count is not displayed for now.
2020-10-04 14:37:23 +02:00
var items = await context
.read<AuthModel>()
2021-06-14 08:56:42 +02:00
.ghClient
2020-02-18 18:00:16 +01:00
.getJSON('/notifications?per_page=1');
2019-09-03 10:51:25 +02:00
if (items is List && items.isNotEmpty) {
2020-10-04 14:37:23 +02:00
context.read<NotificationModel>().setCount(1);
2019-09-03 10:51:25 +02:00
}
});
}
@override
Widget build(context) {
2019-12-08 07:21:14 +01:00
return ListStatefulScaffold<GithubEvent, int>(
2021-05-16 09:16:35 +02:00
title: AppBarTitle(AppLocalizations.of(context)!.news),
itemBuilder: (payload) => EventItem(payload),
fetch: (page) async {
2020-10-04 16:10:05 +02:00
page = page ?? 1;
final auth = context.read<AuthModel>();
2021-05-16 09:16:35 +02:00
final login = auth.activeAccount!.login;
2020-10-04 16:10:05 +02:00
2021-06-14 08:56:42 +02:00
final events = await auth.ghClient.getJSON(
'/users/$login/received_events?page=$page&per_page=$PAGE_SIZE',
2021-05-16 09:16:35 +02:00
convert: (dynamic vs) => [for (var v in vs) GithubEvent.fromJson(v)],
2020-10-04 16:10:05 +02:00
);
return ListPayload(
cursor: page + 1,
hasMore: events.length == PAGE_SIZE,
2020-10-04 16:10:05 +02:00
items: events,
);
},
2019-02-03 07:42:50 +01:00
);
}
}