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

358 lines
12 KiB
Dart
Raw Normal View History

2022-09-20 20:00:03 +02:00
import 'package:antd_mobile/antd_mobile.dart';
2021-05-16 09:16:35 +02:00
import 'package:ferry/ferry.dart';
2019-01-25 13:35:20 +01:00
import 'package:flutter/cupertino.dart';
2022-09-17 14:35:45 +02:00
import 'package:flutter/widgets.dart';
2022-09-13 19:19:52 +02:00
import 'package:flutter_gen/gen_l10n/S.dart';
2022-09-06 18:00:51 +02:00
import 'package:git_touch/graphql/__generated__/github.data.gql.dart';
import 'package:git_touch/graphql/__generated__/github.req.gql.dart';
import 'package:git_touch/graphql/__generated__/github.var.gql.dart';
2022-09-13 19:19:52 +02:00
import 'package:git_touch/models/auth.dart';
2019-09-28 18:25:14 +02:00
import 'package:git_touch/models/theme.dart';
2019-09-25 11:06:36 +02:00
import 'package:git_touch/scaffolds/refresh_stateful.dart';
2019-10-06 15:27:00 +02:00
import 'package:git_touch/utils/utils.dart';
2022-09-13 19:19:52 +02:00
import 'package:git_touch/widgets/action_button.dart';
2020-01-27 07:43:10 +01:00
import 'package:git_touch/widgets/action_entry.dart';
2019-09-11 13:59:47 +02:00
import 'package:git_touch/widgets/app_bar_title.dart';
import 'package:git_touch/widgets/contribution.dart';
2019-10-06 15:27:00 +02:00
import 'package:git_touch/widgets/entry_item.dart';
2022-09-13 19:19:52 +02:00
import 'package:git_touch/widgets/mutation_button.dart';
import 'package:git_touch/widgets/repository_item.dart';
2019-09-04 16:59:33 +02:00
import 'package:git_touch/widgets/table_view.dart';
2020-10-04 16:20:21 +02:00
import 'package:git_touch/widgets/text_with_at.dart';
2020-01-29 06:45:22 +01:00
import 'package:git_touch/widgets/user_header.dart';
2022-09-20 20:00:03 +02:00
import 'package:go_router/go_router.dart';
2019-09-08 14:07:35 +02:00
import 'package:provider/provider.dart';
2021-01-17 15:08:32 +01:00
class _Repos extends StatelessWidget {
2022-09-18 10:21:35 +02:00
_Repos(final Iterable<GRepoParts> pinned, final Iterable<GRepoParts>? repos)
2021-01-17 15:08:32 +01:00
: title =
pinned.isNotEmpty ? 'pinned repositories' : 'popular repositories',
repos = pinned.isNotEmpty ? pinned : repos;
2022-09-21 18:28:21 +02:00
final String title;
final Iterable<GRepoParts>? repos;
2021-01-17 15:08:32 +01:00
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TableViewHeader(title),
...join(
CommonStyle.border,
2021-05-16 09:16:35 +02:00
repos!.map((v) {
2021-01-17 15:08:32 +01:00
return RepositoryItem.gh(
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,
isPrivate: v.isPrivate,
isFork: v.isFork,
);
}).toList(),
),
],
);
}
2021-01-17 15:08:32 +01:00
}
class _User extends StatelessWidget {
2022-09-21 18:28:21 +02:00
const _User(this.p, {this.isViewer = false, this.rightWidgets = const []});
2022-09-18 10:21:35 +02:00
final GUserPartsFull? p;
2021-01-17 15:08:32 +01:00
final bool isViewer;
2021-01-31 08:49:28 +01:00
final List<Widget> rightWidgets;
2021-01-17 15:08:32 +01:00
@override
Widget build(BuildContext context) {
final theme = Provider.of<ThemeModel>(context);
2021-05-16 09:16:35 +02:00
final login = p!.login;
2021-01-17 15:08:32 +01:00
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
2020-01-29 06:45:22 +01:00
UserHeader(
2021-05-16 09:16:35 +02:00
avatarUrl: p!.avatarUrl,
name: p!.name,
login: p!.login,
createdAt: p!.createdAt,
bio: p!.bio,
isViewer: isViewer,
2021-01-31 08:49:28 +01:00
rightWidgets: rightWidgets,
2020-01-11 14:22:52 +01:00
),
CommonStyle.border,
Row(children: [
EntryItem(
2021-05-16 09:16:35 +02:00
count: p!.repositories.totalCount,
text: AppLocalizations.of(context)!.repositories,
url: '/github/$login?tab=repositories',
),
EntryItem(
2021-05-16 09:16:35 +02:00
count: p!.starredRepositories.totalCount,
text: AppLocalizations.of(context)!.stars,
url: '/github/$login?tab=stars',
),
EntryItem(
2021-05-16 09:16:35 +02:00
count: p!.followers.totalCount,
text: AppLocalizations.of(context)!.followers,
url: '/github/$login?tab=followers',
),
EntryItem(
2021-05-16 09:16:35 +02:00
count: p!.following.totalCount,
text: AppLocalizations.of(context)!.following,
url: '/github/$login?tab=following',
),
]),
2020-01-02 06:56:50 +01:00
CommonStyle.border,
ContributionWidget(
2020-10-05 10:47:26 +02:00
weeks: [
for (final week
2021-05-16 09:16:35 +02:00
in p!.contributionsCollection.contributionCalendar.weeks)
2020-10-05 10:47:26 +02:00
[
// https://github.com/git-touch/git-touch/issues/122
2020-10-05 10:47:26 +02:00
for (final day in week.contributionDays)
ContributionDay(hexColor: day.color)
2020-10-05 10:47:26 +02:00
]
],
2020-01-01 06:26:20 +01:00
),
2020-01-02 06:56:50 +01:00
CommonStyle.border,
2022-09-20 20:00:03 +02:00
AntList(
items: [
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.rss),
2022-09-13 19:19:52 +02:00
child: Text(AppLocalizations.of(context)!.events),
2022-09-20 20:00:03 +02:00
onClick: () {
context.push('/github/$login?tab=events');
},
2020-10-04 15:04:23 +02:00
),
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.book),
2022-09-13 19:19:52 +02:00
child: Text(AppLocalizations.of(context)!.gists),
2022-09-20 20:00:03 +02:00
onClick: () {
context.push('/github/$login?tab=gists');
},
2020-10-04 15:04:23 +02:00
),
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.home),
2022-09-13 19:19:52 +02:00
child: Text(AppLocalizations.of(context)!.organizations),
2022-09-20 20:00:03 +02:00
onClick: () {
context.push('/github/$login?tab=organizations');
},
),
2021-05-16 09:16:35 +02:00
if (isNotNullOrEmpty(p!.company))
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.organization),
2022-09-13 19:19:52 +02:00
child: TextWithAt(
2021-05-16 09:16:35 +02:00
text: p!.company!,
2022-09-06 18:28:12 +02:00
linkFactory: (text) => '/github/${text.substring(1)}',
2020-01-27 08:11:51 +01:00
style: TextStyle(fontSize: 17, color: theme.palette.text),
2020-01-01 05:55:27 +01:00
oneLine: true,
),
),
2021-05-16 09:16:35 +02:00
if (isNotNullOrEmpty(p!.location))
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.location),
2022-09-13 19:19:52 +02:00
child: Text(p!.location!),
onClick: () {
launchStringUrl(
'https://www.google.com/maps/place/${p!.location!.replaceAll(RegExp(r'\s+'), '')}');
},
),
2021-05-16 09:16:35 +02:00
if (isNotNullOrEmpty(p!.email))
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.mail),
2022-09-13 19:19:52 +02:00
child: Text(p!.email),
onClick: () {
2022-09-06 18:28:12 +02:00
launchStringUrl('mailto:${p!.email}');
},
),
2021-05-16 09:16:35 +02:00
if (isNotNullOrEmpty(p!.websiteUrl))
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.link),
2022-09-13 19:19:52 +02:00
child: Text(p!.websiteUrl!),
onClick: () {
2021-05-16 09:16:35 +02:00
var url = p!.websiteUrl!;
if (!url.startsWith('http')) {
url = 'http://$url';
}
2022-06-26 08:23:50 +02:00
launchStringUrl(url);
},
),
],
),
2020-01-01 09:35:50 +01:00
CommonStyle.verticalGap,
2021-01-17 15:08:32 +01:00
_Repos(
2022-09-18 10:21:35 +02:00
p!.pinnedItems.nodes!.whereType<GRepoParts>(),
2021-05-16 09:16:35 +02:00
p!.repositories.nodes,
2021-01-17 15:08:32 +01:00
),
CommonStyle.verticalGap,
],
);
}
2021-01-17 15:08:32 +01:00
}
class _Org extends StatelessWidget {
2022-09-06 18:28:12 +02:00
const _Org(this.p);
2022-09-21 18:28:21 +02:00
final GUserData_repositoryOwner__asOrganization? p;
2021-01-17 15:08:32 +01:00
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
2020-01-29 06:45:22 +01:00
UserHeader(
2021-05-16 09:16:35 +02:00
avatarUrl: p!.avatarUrl,
name: p!.name,
login: p!.login,
createdAt: p!.createdAt,
bio: p!.description,
2020-01-29 06:45:22 +01:00
),
CommonStyle.border,
Row(children: [
EntryItem(
2021-05-16 09:16:35 +02:00
count: p!.pinnableItems.totalCount,
text: AppLocalizations.of(context)!.repositories,
2022-09-18 10:56:31 +02:00
url: '/github/${p!.login}?tab=repositories',
),
EntryItem(
2021-05-16 09:16:35 +02:00
count: p!.membersWithRole.totalCount,
text: AppLocalizations.of(context)!.members,
url: '/github/${p!.login}?tab=people',
),
]),
2022-09-20 20:00:03 +02:00
AntList(
items: [
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.rss),
2022-09-13 19:19:52 +02:00
child: Text(AppLocalizations.of(context)!.events),
2022-09-20 20:00:03 +02:00
onClick: () {
context.push('/github/${p!.login}?tab=events');
},
2020-10-04 15:04:23 +02:00
),
2021-05-16 09:16:35 +02:00
if (isNotNullOrEmpty(p!.location))
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.location),
2022-09-13 19:19:52 +02:00
child: Text(p!.location!),
onClick: () {
launchStringUrl(
'https://www.google.com/maps/place/${p!.location!.replaceAll(RegExp(r'\s+'), '')}');
},
),
2021-05-16 09:16:35 +02:00
if (isNotNullOrEmpty(p!.email))
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.mail),
2022-09-13 19:19:52 +02:00
child: Text(p!.email!),
onClick: () {
2022-09-06 18:28:12 +02:00
launchStringUrl('mailto:${p!.email!}');
},
),
2021-05-16 09:16:35 +02:00
if (isNotNullOrEmpty(p!.websiteUrl))
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.link),
2022-09-13 19:19:52 +02:00
child: Text(p!.websiteUrl!),
onClick: () {
2021-05-16 09:16:35 +02:00
var url = p!.websiteUrl!;
if (!url.startsWith('http')) {
url = 'http://$url';
}
2022-06-26 08:23:50 +02:00
launchStringUrl(url);
},
),
],
),
2020-01-01 09:35:50 +01:00
CommonStyle.verticalGap,
2021-01-17 15:08:32 +01:00
_Repos(
2022-09-18 10:21:35 +02:00
p!.pinnedItems.nodes!.whereType<GRepoParts>(),
p!.pinnableItems.nodes!.whereType<GRepoParts>(),
),
CommonStyle.verticalGap,
],
);
}
2021-01-17 15:08:32 +01:00
}
2021-01-17 15:08:32 +01:00
class GhViewer extends StatelessWidget {
2019-01-25 13:35:20 +01:00
@override
Widget build(BuildContext context) {
final auth = Provider.of<AuthModel>(context);
2022-09-18 10:21:35 +02:00
return RefreshStatefulScaffold<GUserPartsFull?>(
fetch: () async {
2021-01-17 15:08:32 +01:00
final req = GViewerReq();
2021-05-16 09:16:35 +02:00
final OperationResponse<GViewerData, GViewerVars?> res =
2021-06-14 08:56:42 +02:00
await auth.gqlClient.request(req).first;
2021-05-16 09:16:35 +02:00
return res.data!.viewer;
},
2021-05-16 09:16:35 +02:00
title: AppBarTitle(AppLocalizations.of(context)!.me),
2022-09-06 18:28:12 +02:00
action: const ActionEntry(
2021-02-14 15:17:22 +01:00
iconData: Ionicons.cog,
2021-01-17 15:08:32 +01:00
url: '/settings',
),
2021-01-31 08:49:28 +01:00
bodyBuilder: (p, _) {
2021-01-17 15:08:32 +01:00
return _User(p, isViewer: true);
},
);
}
}
class GhUser extends StatelessWidget {
2022-09-06 18:28:12 +02:00
const GhUser(this.login);
2022-09-21 18:28:21 +02:00
final String login;
2021-01-17 15:08:32 +01:00
@override
Widget build(BuildContext context) {
final auth = Provider.of<AuthModel>(context);
2021-05-16 09:16:35 +02:00
return RefreshStatefulScaffold<GUserData?>(
2021-01-17 15:08:32 +01:00
fetch: () async {
final req = GUserReq((b) => b..vars.login = login);
2021-05-16 09:16:35 +02:00
final OperationResponse<GUserData, GUserVars?> res =
2021-06-14 08:56:42 +02:00
await auth.gqlClient.request(req).first;
2021-01-17 15:08:32 +01:00
return res.data;
},
title: AppBarTitle(login),
2021-01-31 08:49:28 +01:00
actionBuilder: (payload, _) {
2021-01-17 15:08:32 +01:00
return ActionButton(
title: 'User Actions',
2021-05-16 09:16:35 +02:00
items: ActionItem.getUrlActions(payload!.repositoryOwner!.url),
2021-01-17 15:08:32 +01:00
);
},
2021-01-31 08:49:28 +01:00
bodyBuilder: (data, setData) {
2021-05-16 09:16:35 +02:00
if (data!.repositoryOwner!.G__typename == 'User') {
2021-01-31 08:49:28 +01:00
final p = data.repositoryOwner as GUserData_repositoryOwner__asUser;
return _User(
p,
rightWidgets: [
if (p.viewerCanFollow)
MutationButton(
active: p.viewerIsFollowing,
2021-05-30 19:07:31 +02:00
text: p.viewerIsFollowing
? AppLocalizations.of(context)!.unfollow
2021-05-16 09:16:35 +02:00
: AppLocalizations.of(context)!.follow,
2021-01-31 08:49:28 +01:00
onTap: () async {
if (p.viewerIsFollowing) {
2021-06-14 08:56:42 +02:00
await auth.ghClient.users.unfollowUser(p.login);
2021-01-31 08:49:28 +01:00
} else {
2021-06-14 08:56:42 +02:00
await auth.ghClient.users.followUser(p.login);
2021-01-31 08:49:28 +01:00
}
setData(data.rebuild((b) {
final u = b.repositoryOwner
as GUserData_repositoryOwner__asUser;
b.repositoryOwner = u.rebuild((b1) {
2021-05-16 09:16:35 +02:00
b1.viewerIsFollowing = !b1.viewerIsFollowing!;
2021-01-31 08:49:28 +01:00
});
}));
},
)
],
);
2021-01-17 15:08:32 +01:00
} else {
2021-01-31 08:49:28 +01:00
return _Org(data.repositoryOwner
2021-05-16 09:16:35 +02:00
as GUserData_repositoryOwner__asOrganization?);
}
},
2019-02-03 08:50:17 +01:00
);
2019-01-25 13:35:20 +01:00
}
}