refactor: repositories screen constructor

This commit is contained in:
Rongjian Zhang 2019-09-23 21:45:53 +08:00
parent 75e83374be
commit c6acbf9d8c
3 changed files with 34 additions and 27 deletions

View File

@ -123,7 +123,7 @@ class OrganizationScreen extends StatelessWidget {
count: payload['pinnableItems']['totalCount'],
text: 'Repositories',
screenBuilder: (context) =>
RepositoriesScreen(login, org: true),
RepositoriesScreen.ofOrganization(login),
),
EntryItem(
count: payload['membersWithRole']['totalCount'],

View File

@ -6,43 +6,51 @@ import 'package:provider/provider.dart';
import '../utils/utils.dart';
import 'package:git_touch/widgets/repository_item.dart';
// TODO: refactor
class RepositoriesScreen extends StatelessWidget {
final String login;
final bool star;
final bool org;
final String title;
final String scope;
final String resource;
final String extra0;
final String extra1;
final String extra2;
RepositoriesScreen(this.login, {this.star = false, this.org = false});
String get scope => org ? 'organization' : 'user';
String get resource =>
org ? 'pinnableItems' : star ? 'starredRepositories' : 'repositories';
String get fieldOrderBy {
if (star) {
return 'STARRED_AT';
}
if (org) {
return 'PUSHED_AT';
}
return 'UPDATED_AT';
}
RepositoriesScreen(this.login)
: title = 'Repositories',
scope = 'user',
resource = 'repositories',
extra0 = 'orderBy: {field: UPDATED_AT, direction: DESC}',
extra1 = '',
extra2 = '';
RepositoriesScreen.stars(this.login)
: title = 'Stars',
scope = 'user',
resource = 'starredRepositories',
extra0 = 'orderBy: {field: STARRED_AT, direction: DESC}',
extra1 = '',
extra2 = '';
RepositoriesScreen.ofOrganization(this.login)
: title = 'Repositories',
scope = 'organization',
resource = 'pinnableItems',
extra0 = 'types: [REPOSITORY]',
extra1 = '... on Repository {',
extra2 = '}';
Future<ListPayload> _queryRepos(BuildContext context, [String cursor]) async {
var filterChunk = org
? ', types: [REPOSITORY],'
: ', orderBy: {field: $fieldOrderBy, direction: DESC}';
var cursorChunk = cursor == null ? '' : ', after: "$cursor"';
var contentChunk = org ? '''... on Repository { $repoChunk }''' : repoChunk;
var data = await Provider.of<SettingsModel>(context).query('''
{
$scope(login: "$login") {
$resource(first: $pageSize$cursorChunk$filterChunk) {
$resource(first: $pageSize$cursorChunk, $extra0) {
pageInfo {
hasNextPage
endCursor
}
nodes {
$contentChunk
$extra1
$repoChunk
$extra2
}
}
}
@ -60,7 +68,7 @@ class RepositoriesScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ListScaffold(
title: AppBarTitle(star ? 'Stars' : 'Repositories'),
title: AppBarTitle(title),
onRefresh: () => _queryRepos(context),
onLoadMore: (cursor) => _queryRepos(context, cursor),
itemBuilder: (payload) => RepositoryItem(payload),

View File

@ -209,8 +209,7 @@ class UserScreen extends StatelessWidget {
EntryItem(
count: payload['starredRepositories']['totalCount'],
text: 'Stars',
screenBuilder: (context) =>
RepositoriesScreen(login, star: true),
screenBuilder: (context) => RepositoriesScreen.stars(login),
),
EntryItem(
count: payload['followers']['totalCount'],