2019-02-04 11:32:39 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2020-01-07 08:07:57 +01:00
|
|
|
import 'package:git_touch/graphql/gh.dart';
|
2019-09-25 11:06:36 +02:00
|
|
|
import 'package:git_touch/scaffolds/list_stateful.dart';
|
2019-12-07 15:25:37 +01:00
|
|
|
import 'package:git_touch/utils/utils.dart';
|
2019-09-11 13:59:47 +02:00
|
|
|
import 'package:git_touch/widgets/app_bar_title.dart';
|
2019-09-27 14:52:38 +02:00
|
|
|
import 'package:git_touch/models/auth.dart';
|
2019-09-08 14:07:35 +02:00
|
|
|
import 'package:provider/provider.dart';
|
2019-09-23 12:28:33 +02:00
|
|
|
import 'package:git_touch/widgets/repository_item.dart';
|
2020-01-11 12:25:33 +01:00
|
|
|
import 'package:timeago/timeago.dart' as timeago;
|
2019-02-04 11:32:39 +01:00
|
|
|
|
2019-09-23 12:28:33 +02:00
|
|
|
class RepositoriesScreen extends StatelessWidget {
|
2019-12-07 15:25:37 +01:00
|
|
|
final String owner;
|
2019-09-23 15:45:53 +02:00
|
|
|
final String title;
|
2019-12-07 15:25:37 +01:00
|
|
|
final bool isStar;
|
2019-02-09 06:36:15 +01:00
|
|
|
|
2019-12-07 15:25:37 +01:00
|
|
|
RepositoriesScreen(this.owner)
|
2019-09-23 15:45:53 +02:00
|
|
|
: title = 'Repositories',
|
2019-12-07 15:25:37 +01:00
|
|
|
isStar = false;
|
|
|
|
RepositoriesScreen.stars(this.owner)
|
2019-09-23 15:45:53 +02:00
|
|
|
: title = 'Stars',
|
2019-12-07 15:25:37 +01:00
|
|
|
isStar = true;
|
2019-02-09 06:36:15 +01:00
|
|
|
|
2020-01-07 08:07:57 +01:00
|
|
|
Future<ListPayload<GhReposRepository, String>> _query(BuildContext context,
|
2019-12-07 15:25:37 +01:00
|
|
|
[String cursor]) async {
|
|
|
|
final res = await Provider.of<AuthModel>(context).gqlClient.execute(
|
2020-01-07 08:07:57 +01:00
|
|
|
GhReposQuery(
|
|
|
|
variables:
|
|
|
|
GhReposArguments(owner: owner, isStar: isStar, after: cursor)));
|
2019-12-07 15:25:37 +01:00
|
|
|
final data = res.data.repositoryOwner;
|
|
|
|
switch (data.resolveType) {
|
|
|
|
case 'User':
|
2020-01-07 08:07:57 +01:00
|
|
|
final user = data as GhReposUser;
|
2019-12-07 15:25:37 +01:00
|
|
|
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':
|
2020-01-07 08:07:57 +01:00
|
|
|
final org = data as GhReposOrganization;
|
2019-12-07 15:25:37 +01:00
|
|
|
return ListPayload(
|
|
|
|
cursor: org.pinnableItems.pageInfo.endCursor,
|
|
|
|
items: org.pinnableItems.nodes,
|
|
|
|
hasMore: org.pinnableItems.pageInfo.hasNextPage,
|
|
|
|
);
|
|
|
|
default:
|
|
|
|
return null;
|
2019-02-09 06:36:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 11:32:39 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-01-07 08:07:57 +01:00
|
|
|
return ListStatefulScaffold<GhReposRepository, String>(
|
2019-12-07 15:25:37 +01:00
|
|
|
title: AppBarTitle(title),
|
|
|
|
onRefresh: () => _query(context),
|
|
|
|
onLoadMore: (cursor) => _query(context, cursor),
|
2020-01-11 12:52:17 +01:00
|
|
|
itemBuilder: (v) {
|
|
|
|
return RepositoryItem.gh(
|
2020-01-29 06:06:55 +01:00
|
|
|
owner: v.owner.login,
|
|
|
|
avatarUrl: v.owner.avatarUrl,
|
|
|
|
name: v.name,
|
|
|
|
description: v.description,
|
|
|
|
starCount: v.stargazers.totalCount,
|
|
|
|
forkCount: v.forks.totalCount,
|
|
|
|
primaryLanguageName: v.primaryLanguage?.name,
|
|
|
|
primaryLanguageColor: v.primaryLanguage?.color,
|
|
|
|
note: 'Updated ${timeago.format(v.updatedAt)}',
|
2020-01-11 12:52:17 +01:00
|
|
|
isPrivate: v.isPrivate,
|
|
|
|
isFork: v.isFork,
|
2019-12-07 15:25:37 +01:00
|
|
|
);
|
|
|
|
});
|
2019-02-04 11:32:39 +01:00
|
|
|
}
|
|
|
|
}
|