1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-17 10:48:50 +01:00

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'], count: payload['pinnableItems']['totalCount'],
text: 'Repositories', text: 'Repositories',
screenBuilder: (context) => screenBuilder: (context) =>
RepositoriesScreen(login, org: true), RepositoriesScreen.ofOrganization(login),
), ),
EntryItem( EntryItem(
count: payload['membersWithRole']['totalCount'], count: payload['membersWithRole']['totalCount'],

View File

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

View File

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