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

70 lines
1.8 KiB
Dart
Raw Normal View History

2019-02-04 11:32:39 +01:00
import 'package:flutter/material.dart';
2019-02-09 06:36:15 +01:00
import '../scaffolds/list.dart';
import '../providers/settings.dart';
import '../utils/utils.dart';
import '../widgets/repo_item.dart';
2019-02-04 11:32:39 +01:00
/// Repos of user
class ReposScreen extends StatefulWidget {
2019-02-09 06:36:15 +01:00
final String login;
2019-02-09 06:42:18 +01:00
final bool star;
2019-03-10 16:34:34 +01:00
final bool org;
2019-02-09 06:36:15 +01:00
2019-03-10 16:34:34 +01:00
ReposScreen({this.login, this.star = false, this.org = false});
2019-02-09 06:36:15 +01:00
2019-02-04 11:32:39 +01:00
@override
_ReposScreenState createState() => _ReposScreenState();
}
class _ReposScreenState extends State<ReposScreen> {
2019-03-10 16:34:34 +01:00
String get login => widget.login;
String get scope => widget.org ? 'organization' : 'user';
String get resource => widget.star ? 'starredRepositories' : 'repositories';
String get fieldOrderBy {
if (widget.star) {
return 'STARRED_AT';
}
if (widget.org) {
return 'PUSHED_AT';
}
return 'UPDATED_AT';
}
2019-02-09 06:36:15 +01:00
Future<ListPayload> _queryRepos([String cursor]) async {
var cursorChunk = cursor == null ? '' : ', after: "$cursor"';
var data = await SettingsProvider.of(context).query('''
{
2019-03-10 16:34:34 +01:00
$scope(login: "$login") {
$resource(first: $pageSize$cursorChunk, orderBy: {field: $fieldOrderBy, direction: DESC}) {
2019-02-09 06:36:15 +01:00
pageInfo {
hasNextPage
endCursor
}
nodes {
$repoChunk
}
}
}
}
''');
2019-03-10 16:34:34 +01:00
var repo = data[scope][resource];
2019-02-09 06:36:15 +01:00
return ListPayload(
cursor: repo["pageInfo"]["endCursor"],
items: repo["nodes"],
2019-02-09 07:20:21 +01:00
hasMore: repo['pageInfo']['hasNextPage'],
2019-02-09 06:36:15 +01:00
);
}
2019-02-04 11:32:39 +01:00
@override
Widget build(BuildContext context) {
var title = widget.star ? 'stars' : 'repositories';
2019-02-09 06:36:15 +01:00
return ListScaffold(
title: Text('$login\'s $title'),
2019-02-09 06:36:15 +01:00
onRefresh: () => _queryRepos(),
onLoadMore: (cursor) => _queryRepos(cursor),
itemBuilder: (payload) => RepoItem(payload, showOwner: widget.star),
2019-02-09 06:36:15 +01:00
);
2019-02-04 11:32:39 +01:00
}
}