1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-24 06:42:07 +01:00
git-touch-android-ios-app/lib/screens/news.dart

72 lines
2.3 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.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-11-08 10:24:00 +01:00
import 'package:git_touch/models/theme.dart';
2019-09-25 11:06:36 +02:00
import 'package:git_touch/scaffolds/list_stateful.dart';
2019-09-03 10:51:25 +02:00
import 'package:git_touch/utils/utils.dart';
2019-11-08 10:24:00 +01: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-03 10:51:25 +02:00
import 'package:provider/provider.dart';
import '../widgets/event_item.dart';
2019-09-27 14:52:38 +02:00
import 'package:git_touch/models/auth.dart';
2019-03-10 09:09:26 +01:00
class NewsScreen extends StatefulWidget {
@override
NewsScreenState createState() => NewsScreenState();
}
class NewsScreenState extends State<NewsScreen> {
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.
2019-09-27 14:52:38 +02:00
var items = await Provider.of<AuthModel>(context)
2019-09-03 10:51:25 +02:00
.getWithCredentials('/notifications?per_page=1');
if (items is List && items.isNotEmpty) {
Provider.of<NotificationModel>(context).setCount(1);
}
});
}
2019-12-08 07:21:14 +01:00
Future<ListPayload<GithubEvent, int>> fetchEvents([int page = 1]) async {
final auth = Provider.of<AuthModel>(context);
final login = auth.activeAccount.login;
List data = await auth.getWithCredentials(
2019-02-09 07:20:21 +01:00
'/users/$login/received_events?page=$page&per_page=$pageSize');
// Fimber.d(data.length);
2019-03-10 09:09:26 +01:00
var hasMore = data.length == pageSize;
2019-12-08 07:21:14 +01:00
var events = data.map((item) => GithubEvent.fromJson(item)).toList();
2019-03-10 09:09:26 +01:00
return ListPayload(
cursor: page + 1,
hasMore: hasMore,
items: events,
);
}
@override
Widget build(context) {
2019-12-08 07:21:14 +01:00
return ListStatefulScaffold<GithubEvent, int>(
2019-09-11 13:59:47 +02:00
title: AppBarTitle('News'),
2020-01-13 14:07:28 +01:00
// actionBuilder: () {
// return ActionEntry(
// iconData:
// Provider.of<ThemeModel>(context).brightness == Brightness.dark
// ? Ionicons.md_sunny
// : Ionicons.md_moon,
// onTap: () {
// Provider.of<ThemeModel>(context).toggleBrightness();
// },
// );
// },
itemBuilder: (payload) => EventItem(payload),
2019-03-10 09:09:26 +01:00
onRefresh: fetchEvents,
onLoadMore: (page) => fetchEvents(page),
2019-02-03 07:42:50 +01:00
);
}
}