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

97 lines
2.4 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import '../scaffolds/list.dart';
import '../widgets/event_item.dart';
2019-02-07 07:35:19 +01:00
import '../providers/settings.dart';
2019-03-10 09:09:26 +01:00
import '../widgets/action.dart';
class NewsFilter {
static const all = 'all';
/// The same as GitHub dashboard
///
/// Exclude issue and pull request events
static const github = 'github';
}
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-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-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-03-10 10:18:38 +01:00
// print(data.length);
2019-03-10 09:09:26 +01:00
var hasMore = data.length == pageSize;
var events = data
2019-02-10 05:54:48 +01:00
.map<EventPayload>((item) => EventPayload.fromJson(item))
2019-03-10 09:09:26 +01:00
.where(testEvents)
2019-02-10 05:54:48 +01:00
.toList();
2019-03-10 09:09:26 +01:00
return ListPayload(
cursor: page + 1,
hasMore: hasMore,
items: events,
);
}
bool testEvents(EventPayload event) {
switch (filter) {
case NewsFilter.github:
return ![
'IssueCommentEvent',
'IssuesEvent',
'PullRequestEvent',
'PullRequestReviewEvent',
'PullRequestReviewCommentEvent',
].contains(event.type);
default:
return true;
}
2019-02-07 07:35:19 +01:00
}
@override
Widget build(context) {
2019-03-10 09:09:26 +01:00
return ListScaffold<EventPayload, int>(
2019-02-05 13:57:05 +01:00
title: Text('News'),
itemBuilder: (payload) => EventItem(payload),
2019-03-10 09:09:26 +01:00
onRefresh: fetchEvents,
onLoadMore: (page) => fetchEvents(page),
trailingBuiler: ({refresh}) {
return ActionButton(
title: 'Filter',
actions: [
Action(
text: 'Show all items',
onPress: () {
filter = NewsFilter.all;
refresh(force: true);
},
),
Action(
text: 'Only GitHub items',
onPress: () {
filter = NewsFilter.github;
refresh(force: true);
},
),
],
);
2019-02-03 07:42:50 +01:00
},
);
}
}