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

82 lines
2.5 KiB
Dart
Raw Normal View History

2019-02-04 11:32:39 +01:00
import 'package:flutter/material.dart';
2019-09-25 11:06:36 +02:00
import 'package:git_touch/scaffolds/list_stateful.dart';
2019-09-11 13:59:47 +02:00
import 'package:git_touch/widgets/app_bar_title.dart';
2019-09-27 14:52:38 +02:00
import 'package:git_touch/models/auth.dart';
2019-09-08 14:07:35 +02:00
import 'package:provider/provider.dart';
2019-02-09 06:36:15 +01:00
import '../utils/utils.dart';
2019-09-23 12:28:33 +02:00
import 'package:git_touch/widgets/repository_item.dart';
2019-02-04 11:32:39 +01:00
2019-09-23 12:28:33 +02:00
class RepositoriesScreen extends StatelessWidget {
final String title;
final String scope;
2019-09-30 13:55:27 +02:00
final String scopeParams;
final String resource;
2019-09-30 13:55:27 +02:00
final String extraFilter;
final String queryChunk;
2019-02-09 06:36:15 +01:00
2019-09-30 13:55:27 +02:00
RepositoriesScreen(String login)
: title = 'Repositories',
scope = 'user',
2019-09-30 13:55:27 +02:00
scopeParams = 'login: "$login"',
resource = 'repositories',
2019-09-30 13:55:27 +02:00
extraFilter = ', orderBy: {field: UPDATED_AT, direction: DESC}',
queryChunk = repoChunk;
RepositoriesScreen.stars(String login)
: title = 'Stars',
scope = 'user',
2019-09-30 13:55:27 +02:00
scopeParams = 'login: "$login"',
resource = 'starredRepositories',
2019-09-30 13:55:27 +02:00
extraFilter = ', orderBy: {field: STARRED_AT, direction: DESC}',
queryChunk = repoChunk;
RepositoriesScreen.ofOrganization(String login)
: title = 'Repositories',
scope = 'organization',
2019-09-30 13:55:27 +02:00
scopeParams = 'login: "$login"',
resource = 'pinnableItems',
2019-09-30 13:55:27 +02:00
extraFilter = ', types: [REPOSITORY]',
queryChunk = '... on Repository { $repoChunk }';
RepositoriesScreen.forks(String owner, String name)
: title = 'Forks',
scope = 'repository',
scopeParams = 'owner: "$owner", name: "$name"',
resource = 'forks',
extraFilter = ', orderBy: {field: CREATED_AT, direction: DESC}',
queryChunk = repoChunk;
2019-02-09 06:36:15 +01:00
2019-09-21 19:02:14 +02:00
Future<ListPayload> _queryRepos(BuildContext context, [String cursor]) async {
2019-02-09 06:36:15 +01:00
var cursorChunk = cursor == null ? '' : ', after: "$cursor"';
2019-09-27 14:52:38 +02:00
var data = await Provider.of<AuthModel>(context).query('''
2019-02-09 06:36:15 +01:00
{
2019-09-30 13:55:27 +02:00
$scope($scopeParams) {
$resource(first: $pageSize$cursorChunk$extraFilter) {
2019-02-09 06:36:15 +01:00
pageInfo {
hasNextPage
endCursor
}
nodes {
2019-09-30 13:55:27 +02:00
$queryChunk
2019-02-09 06:36:15 +01:00
}
}
}
}
''');
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) {
2019-09-25 11:06:36 +02:00
return ListStatefulScaffold(
title: AppBarTitle(title),
2019-09-21 19:02:14 +02:00
onRefresh: () => _queryRepos(context),
onLoadMore: (cursor) => _queryRepos(context, cursor),
2019-09-23 12:28:33 +02:00
itemBuilder: (payload) => RepositoryItem(payload),
2019-02-09 06:36:15 +01:00
);
2019-02-04 11:32:39 +01:00
}
}