1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-30 17:47:22 +01:00
git-touch-android-ios-app/lib/screens/repositories.dart
2019-12-12 20:33:42 +08:00

81 lines
2.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:git_touch/graphql/github_repositories.dart';
import 'package:git_touch/scaffolds/list_stateful.dart';
import 'package:git_touch/utils/utils.dart';
import 'package:git_touch/widgets/app_bar_title.dart';
import 'package:git_touch/models/auth.dart';
import 'package:provider/provider.dart';
import 'package:git_touch/widgets/repository_item.dart';
class RepositoriesScreen extends StatelessWidget {
final String owner;
final String title;
final bool isStar;
RepositoriesScreen(this.owner)
: title = 'Repositories',
isStar = false;
RepositoriesScreen.stars(this.owner)
: title = 'Stars',
isStar = true;
Future<ListPayload<GithubRepositoriesRepository, String>> _query(
BuildContext context,
[String cursor]) async {
final res = await Provider.of<AuthModel>(context).gqlClient.execute(
GithubRepositoriesQuery(
variables: GithubRepositoriesArguments(
owner: owner, isStar: isStar, after: cursor)));
final data = res.data.repositoryOwner;
switch (data.resolveType) {
case 'User':
final user = data as GithubRepositoriesUser;
if (isStar) {
return ListPayload(
cursor: user.starredRepositories.pageInfo.endCursor,
items: user.starredRepositories.nodes,
hasMore: user.starredRepositories.pageInfo.hasNextPage,
);
} else {
return ListPayload(
cursor: user.repositories.pageInfo.endCursor,
items: user.repositories.nodes,
hasMore: user.repositories.pageInfo.hasNextPage,
);
}
break;
case 'Organization':
final org = data as GithubRepositoriesOrganization;
return ListPayload(
cursor: org.pinnableItems.pageInfo.endCursor,
items: org.pinnableItems.nodes,
hasMore: org.pinnableItems.pageInfo.hasNextPage,
);
default:
return null;
}
}
@override
Widget build(BuildContext context) {
return ListStatefulScaffold<GithubRepositoriesRepository, String>(
title: AppBarTitle(title),
onRefresh: () => _query(context),
onLoadMore: (cursor) => _query(context, cursor),
itemBuilder: (item) {
return RepositoryItem.raw(
item.owner.login,
item.owner.avatarUrl,
item.name,
item.description,
Octicons.repo,
item.stargazers.totalCount,
item.forks.totalCount,
item.primaryLanguage?.name,
item.primaryLanguage?.color,
[],
);
});
}
}