2019-01-31 07:20:48 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
2019-09-03 10:51:25 +02:00
|
|
|
import 'package:git_touch/models/notification.dart';
|
|
|
|
import 'package:git_touch/utils/utils.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';
|
2019-02-08 07:06:48 +01:00
|
|
|
import '../scaffolds/list.dart';
|
2019-01-31 07:20:48 +01:00
|
|
|
import '../widgets/event_item.dart';
|
2019-09-08 14:07:35 +02:00
|
|
|
import 'package:git_touch/models/settings.dart';
|
2019-03-10 09:09:26 +01:00
|
|
|
|
|
|
|
class NewsFilter {
|
|
|
|
static const all = 'all';
|
|
|
|
|
|
|
|
/// The same as GitHub dashboard
|
|
|
|
///
|
|
|
|
/// Exclude issue and pull request events
|
|
|
|
static const github = 'github';
|
|
|
|
}
|
2019-01-31 07:20:48 +01:00
|
|
|
|
|
|
|
class NewsScreen extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
NewsScreenState createState() => NewsScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class NewsScreenState extends State<NewsScreen> {
|
2019-03-10 09:09:26 +01:00
|
|
|
String filter = NewsFilter.github;
|
|
|
|
|
2019-09-03 10:51:25 +02:00
|
|
|
@override
|
|
|
|
initState() {
|
|
|
|
super.initState();
|
|
|
|
// FIXME:
|
|
|
|
nextTick(() async {
|
|
|
|
// Check if there are unread notification items.
|
|
|
|
// 1 item is enough since count is not displayed for now.
|
2019-09-08 14:07:35 +02:00
|
|
|
var items = await Provider.of<SettingsModel>(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-03-13 15:02:16 +01:00
|
|
|
int get pageSize {
|
|
|
|
if (filter == NewsFilter.all) {
|
|
|
|
return 30;
|
|
|
|
}
|
|
|
|
return 60;
|
|
|
|
}
|
|
|
|
|
2019-03-10 09:09:26 +01:00
|
|
|
Future<ListPayload<EventPayload, int>> fetchEvents([int page = 1]) async {
|
2019-09-08 14:07:35 +02:00
|
|
|
var settings = Provider.of<SettingsModel>(context);
|
2019-02-07 09:46:21 +01:00
|
|
|
var login = settings.activeLogin;
|
2019-02-09 07:20:21 +01:00
|
|
|
List data = await settings.getWithCredentials(
|
|
|
|
'/users/$login/received_events?page=$page&per_page=$pageSize');
|
2019-03-10 10:18:38 +01:00
|
|
|
// print(data.length);
|
2019-03-10 09:09:26 +01:00
|
|
|
var hasMore = data.length == pageSize;
|
2019-07-25 16:04:08 +02:00
|
|
|
var events =
|
|
|
|
data.map<EventPayload>((item) => EventPayload.fromJson(item)).toList();
|
2019-03-10 09:09:26 +01:00
|
|
|
|
|
|
|
return ListPayload(
|
|
|
|
cursor: page + 1,
|
|
|
|
hasMore: hasMore,
|
|
|
|
items: events,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-31 07:20:48 +01:00
|
|
|
@override
|
|
|
|
Widget build(context) {
|
2019-03-10 09:09:26 +01:00
|
|
|
return ListScaffold<EventPayload, int>(
|
2019-09-11 13:59:47 +02:00
|
|
|
title: AppBarTitle('News'),
|
2019-02-09 06:29:44 +01:00
|
|
|
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
|
|
|
);
|
2019-01-31 07:20:48 +01:00
|
|
|
}
|
|
|
|
}
|