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

158 lines
4.6 KiB
Dart
Raw Normal View History

2019-02-04 14:38:29 +01:00
import 'package:flutter/material.dart';
2020-01-07 08:07:57 +01:00
import 'package:git_touch/graphql/gh.dart';
import 'package:git_touch/models/theme.dart';
2019-09-25 11:06:36 +02:00
import 'package:git_touch/scaffolds/list_stateful.dart';
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-14 11:19:33 +02:00
import 'package:git_touch/widgets/user_item.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-12-07 16:10:30 +01:00
enum UsersScreenType {
follower,
following,
member,
watch,
star,
}
2019-02-04 14:38:29 +01:00
2020-02-07 07:17:05 +01:00
class GhUsersScreen extends StatelessWidget {
2019-12-07 16:10:30 +01:00
final String login;
final String repoName;
final UsersScreenType type;
2020-02-07 07:17:05 +01:00
GhUsersScreen(this.login, this.type, {this.repoName = ''});
2019-12-07 16:10:30 +01:00
String get _title {
switch (type) {
case UsersScreenType.follower:
return 'Followers';
case UsersScreenType.following:
return 'Following';
case UsersScreenType.member:
return 'Members';
case UsersScreenType.watch:
return 'Watchers';
case UsersScreenType.star:
return 'Stargazers';
default:
return null;
}
}
2020-01-07 08:07:57 +01:00
Future<ListPayload<GhUsersUser, String>> _queryUsers(BuildContext context,
2019-12-07 16:10:30 +01:00
[String cursor]) async {
final res = await Provider.of<AuthModel>(context).gqlClient.execute(
2020-01-07 08:07:57 +01:00
GhUsersQuery(
variables: GhUsersArguments(
2019-12-07 16:10:30 +01:00
login: login,
repoName: repoName,
isFollowers: type == UsersScreenType.follower,
isFollowing: type == UsersScreenType.following,
isStar: type == UsersScreenType.star,
isWatch: type == UsersScreenType.watch,
isMember: type == UsersScreenType.member,
after: cursor)));
switch (type) {
case UsersScreenType.follower:
final payload = res.data.user.followers;
return ListPayload(
cursor: payload.pageInfo.endCursor,
hasMore: payload.pageInfo.hasNextPage,
items: payload.nodes,
);
case UsersScreenType.following:
final payload = res.data.user.following;
return ListPayload(
cursor: payload.pageInfo.endCursor,
hasMore: payload.pageInfo.hasNextPage,
items: payload.nodes,
);
case UsersScreenType.member:
final payload = res.data.organization.membersWithRole;
return ListPayload(
cursor: payload.pageInfo.endCursor,
hasMore: payload.pageInfo.hasNextPage,
items: payload.nodes,
);
case UsersScreenType.watch:
final payload = res.data.repository.watchers;
return ListPayload(
cursor: payload.pageInfo.endCursor,
hasMore: payload.pageInfo.hasNextPage,
items: payload.nodes,
);
case UsersScreenType.star:
final payload = res.data.repository.stargazers;
return ListPayload(
cursor: payload.pageInfo.endCursor,
hasMore: payload.pageInfo.hasNextPage,
items: payload.nodes,
);
default:
return null;
}
}
Widget _buildBio(BuildContext context, String company, String location,
DateTime createdAt) {
final theme = Provider.of<ThemeModel>(context);
if (company != null && company.isNotEmpty) {
return Row(
children: <Widget>[
Icon(
Octicons.organization,
size: 15,
2020-01-27 08:11:51 +01:00
color: theme.palette.secondaryText,
),
SizedBox(width: 4),
Text(company),
],
);
}
if (location != null && location.isNotEmpty) {
return Row(
children: <Widget>[
Icon(
Octicons.location,
size: 15,
2020-01-27 08:11:51 +01:00
color: theme.palette.secondaryText,
),
SizedBox(width: 4),
Text(location),
],
);
}
return Row(
children: <Widget>[
Icon(
Octicons.clock,
size: 15,
2020-01-27 08:11:51 +01:00
color: theme.palette.secondaryText,
),
SizedBox(width: 4),
2020-01-01 08:58:49 +01:00
Text('Joined on ${dateFormat.format(createdAt)}'),
],
);
}
2019-02-04 14:38:29 +01:00
@override
Widget build(BuildContext context) {
2020-01-07 08:07:57 +01:00
return ListStatefulScaffold<GhUsersUser, String>(
2019-12-07 16:10:30 +01:00
title: AppBarTitle(_title),
2019-09-13 10:55:27 +02:00
onRefresh: () => _queryUsers(context),
onLoadMore: (cursor) => _queryUsers(context, cursor),
2019-12-07 16:10:30 +01:00
itemBuilder: (payload) {
2020-02-01 03:32:29 +01:00
return UserItem.gh(
2019-12-07 16:10:30 +01:00
login: payload.login,
2020-02-01 03:32:29 +01:00
// name: payload.name,
2019-12-07 16:10:30 +01:00
avatarUrl: payload.avatarUrl,
bio: _buildBio(
context, payload.company, payload.location, payload.createdAt),
2019-12-07 16:10:30 +01:00
);
},
);
2019-02-04 14:38:29 +01:00
}
}