2019-01-31 07:20:48 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/cupertino.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-02-07 07:35:19 +01:00
|
|
|
import '../providers/settings.dart';
|
2019-01-31 07:20:48 +01:00
|
|
|
import '../utils/utils.dart';
|
|
|
|
|
|
|
|
class NewsScreen extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
NewsScreenState createState() => NewsScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class NewsScreenState extends State<NewsScreen> {
|
2019-02-10 05:54:48 +01:00
|
|
|
Future<List<EventPayload>> fetchEvents(int page) async {
|
2019-02-07 09:46:21 +01:00
|
|
|
var settings = SettingsProvider.of(context);
|
|
|
|
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-02-09 06:29:44 +01:00
|
|
|
// print(data);
|
2019-02-10 05:54:48 +01:00
|
|
|
return data
|
|
|
|
.map<EventPayload>((item) => EventPayload.fromJson(item))
|
|
|
|
.toList();
|
2019-02-07 07:35:19 +01:00
|
|
|
}
|
|
|
|
|
2019-01-31 07:20:48 +01:00
|
|
|
@override
|
|
|
|
Widget build(context) {
|
2019-02-09 06:29:44 +01:00
|
|
|
// FIXME: can't add generic type here. Don't know why
|
|
|
|
// type '(Event) => EventItem' is not a subtype of type '(dynamic) => Widget'
|
2019-02-03 07:42:50 +01:00
|
|
|
return ListScaffold(
|
2019-02-05 13:57:05 +01:00
|
|
|
title: Text('News'),
|
2019-02-09 06:29:44 +01:00
|
|
|
itemBuilder: (payload) => EventItem(payload),
|
2019-02-03 07:42:50 +01:00
|
|
|
onRefresh: () async {
|
2019-02-09 06:29:44 +01:00
|
|
|
var page = 1;
|
2019-02-07 09:46:21 +01:00
|
|
|
var items = await fetchEvents(page);
|
2019-02-09 06:29:44 +01:00
|
|
|
return ListPayload(
|
2019-02-09 07:20:21 +01:00
|
|
|
cursor: page + 1, hasMore: items.length == pageSize, items: items);
|
2019-02-03 07:42:50 +01:00
|
|
|
},
|
2019-02-09 06:29:44 +01:00
|
|
|
onLoadMore: (page) async {
|
2019-02-07 09:46:21 +01:00
|
|
|
var items = await fetchEvents(page);
|
2019-02-09 06:29:44 +01:00
|
|
|
return ListPayload(
|
2019-02-09 07:20:21 +01:00
|
|
|
cursor: page + 1, hasMore: items.length == pageSize, items: items);
|
2019-02-03 07:42:50 +01:00
|
|
|
},
|
|
|
|
);
|
2019-01-31 07:20:48 +01:00
|
|
|
}
|
|
|
|
}
|