1
0
mirror of https://github.com/git-touch/git-touch synced 2025-03-06 12:17:50 +01:00

82 lines
2.5 KiB
Dart
Raw Normal View History

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