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

70 lines
1.9 KiB
Dart
Raw Normal View History

2019-02-04 11:32:39 +01:00
import 'package:flutter/material.dart';
2019-09-11 13:59:47 +02:00
import 'package:git_touch/widgets/app_bar_title.dart';
2019-02-09 06:36:15 +01:00
import '../scaffolds/list.dart';
2019-09-08 14:07:35 +02:00
import 'package:git_touch/models/settings.dart';
import 'package:provider/provider.dart';
2019-02-09 06:36:15 +01:00
import '../utils/utils.dart';
import '../widgets/repo_item.dart';
2019-02-04 11:32:39 +01:00
2019-09-21 19:56:26 +02:00
// TODO: refactor
2019-09-21 19:02:14 +02:00
class ReposScreen extends StatelessWidget {
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-09-21 19:02:14 +02:00
ReposScreen(this.login, {this.star = false, this.org = false});
2019-02-09 06:36:15 +01:00
2019-09-21 19:02:14 +02:00
String get scope => org ? 'organization' : 'user';
2019-09-21 19:56:26 +02:00
String get resource =>
org ? 'pinnableItems' : star ? 'starredRepositories' : 'repositories';
2019-03-10 16:34:34 +01:00
String get fieldOrderBy {
2019-09-21 19:02:14 +02:00
if (star) {
2019-03-10 16:34:34 +01:00
return 'STARRED_AT';
}
2019-09-21 19:02:14 +02:00
if (org) {
2019-03-10 16:34:34 +01:00
return 'PUSHED_AT';
}
return 'UPDATED_AT';
}
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-09-21 19:56:26 +02:00
var filterChunk = org
? ', types: [REPOSITORY],'
: ', orderBy: {field: $fieldOrderBy, direction: DESC}';
2019-02-09 06:36:15 +01:00
var cursorChunk = cursor == null ? '' : ', after: "$cursor"';
2019-09-21 19:56:26 +02:00
var contentChunk = org ? '''... on Repository { $repoChunk }''' : repoChunk;
2019-09-08 14:07:35 +02:00
var data = await Provider.of<SettingsModel>(context).query('''
2019-02-09 06:36:15 +01:00
{
2019-03-10 16:34:34 +01:00
$scope(login: "$login") {
2019-09-21 19:56:26 +02:00
$resource(first: $pageSize$cursorChunk$filterChunk) {
2019-02-09 06:36:15 +01:00
pageInfo {
hasNextPage
endCursor
}
nodes {
2019-09-21 19:56:26 +02:00
$contentChunk
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-02-09 06:36:15 +01:00
return ListScaffold(
2019-09-21 19:02:14 +02:00
title: AppBarTitle(star ? 'Stars' : 'Repositories'),
onRefresh: () => _queryRepos(context),
onLoadMore: (cursor) => _queryRepos(context, cursor),
2019-09-09 16:50:22 +02:00
itemBuilder: (payload) => RepoItem(payload),
2019-02-09 06:36:15 +01:00
);
2019-02-04 11:32:39 +01:00
}
}