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

482 lines
16 KiB
Dart
Raw Normal View History

2022-09-20 20:00:03 +02:00
import 'package:antd_mobile/antd_mobile.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-10-07 18:55:47 +02:00
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
2022-09-13 19:19:52 +02:00
import 'package:git_touch/models/auth.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';
import 'package:git_touch/widgets/avatar.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';
2022-10-02 06:49:55 +02:00
import 'package:git_touch/widgets/repo_item.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';
import 'package:gql_github/user.data.gql.dart';
import 'package:gql_github/user.req.gql.dart';
2022-10-04 19:13:15 +02:00
import 'package:intl/intl.dart';
2022-10-01 21:34:55 +02:00
import 'package:maps_launcher/maps_launcher.dart';
2019-09-08 14:07:35 +02:00
import 'package:provider/provider.dart';
2022-10-04 19:13:15 +02:00
class _SponsorItem extends StatelessWidget {
const _SponsorItem({
required this.count,
required this.login,
required this.nodes,
required this.sponsoring,
});
final int count;
final String login;
final Iterable<GSponsorConnectionParts_nodes> nodes;
final bool sponsoring;
@override
Widget build(BuildContext context) {
return AntListItem(
prefix: Icon(sponsoring ? Octicons.heart : Octicons.heart_fill),
extra: Text(count.toString()),
onClick: () {
context.pushUrl(sponsoring
? 'https://github.com/$login?tab=sponsoring'
: 'https://github.com/sponsors/$login#sponsors');
},
child: Row(
children: [
Text(toBeginningOfSentenceCase(
sponsoring ? 'sponsoring' : 'sponsors')!),
const Spacer(),
for (final sponsor in nodes) ...[
const SizedBox(width: 6),
Avatar(
square: sponsor.G__typename != 'User',
url: (sponsor as dynamic).avatarUrl, // TODO: same key here
size: AvatarSize.small,
),
],
],
),
);
}
}
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) {
2022-09-23 19:49:27 +02:00
return AntList(
header: Text(title),
2021-01-17 15:08:32 +01:00
children: [
2022-09-30 22:50:47 +02:00
for (final v in repos!)
2022-10-02 06:49:55 +02:00
RepoItem.gh(
2022-09-30 22:50:47 +02: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,
isPrivate: v.isPrivate,
isFork: v.isFork,
),
2021-01-17 15:08:32 +01:00
],
);
}
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-30 22:50:47 +02:00
final GUserParts 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) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
2020-01-29 06:45:22 +01:00
UserHeader(
2022-09-24 07:22:07 +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,
2022-09-25 17:11:23 +02:00
Row(
children: [
EntryItem(
count: p.followers.totalCount,
text: AppLocalizations.of(context)!.followers,
url: '/github/${p.login}?tab=followers',
),
EntryItem(
count: p.following.totalCount,
text: AppLocalizations.of(context)!.following,
url: '/github/${p.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
2022-09-24 07:22:07 +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
),
2022-09-20 20:00:03 +02:00
AntList(
2022-09-22 17:37:06 +02:00
children: [
2022-10-03 22:09:36 +02:00
if (p.company != null)
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.organization),
2022-09-13 19:19:52 +02:00
child: TextWithAt(
2022-09-24 07:22:07 +02:00
text: p.company!,
2022-09-06 18:28:12 +02:00
linkFactory: (text) => '/github/${text.substring(1)}',
2022-10-07 20:19:07 +02:00
style: TextStyle(color: AntTheme.of(context).colorText),
2020-01-01 05:55:27 +01:00
oneLine: true,
),
),
2022-10-03 22:09:36 +02:00
if (p.location != null)
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.location),
2022-09-24 07:22:07 +02:00
child: Text(p.location!),
2022-09-13 19:19:52 +02:00
onClick: () {
2022-10-01 21:34:55 +02:00
MapsLauncher.launchQuery(p.location!);
},
),
2022-10-03 22:09:36 +02:00
if (p.email.isNotEmpty)
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.mail),
2022-09-24 07:22:07 +02:00
child: Text(p.email),
2022-09-13 19:19:52 +02:00
onClick: () {
2022-09-24 07:22:07 +02:00
launchStringUrl('mailto:${p.email}');
},
),
2022-10-03 22:09:36 +02:00
if (p.websiteUrl != null)
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.link),
2022-09-24 07:22:07 +02:00
child: Text(p.websiteUrl!),
2022-09-13 19:19:52 +02:00
onClick: () {
2022-09-24 07:22:07 +02:00
var url = p.websiteUrl!;
if (!url.startsWith('http')) {
url = 'http://$url';
}
2022-06-26 08:23:50 +02:00
launchStringUrl(url);
},
),
if (p.twitterUsername != null)
AntListItem(
prefix: const Icon(Ionicons.logo_twitter),
child: Text('@${p.twitterUsername!}'),
onClick: () {
launchStringUrl('https://twitter.com/${p.twitterUsername}');
},
),
],
),
2020-01-01 09:35:50 +01:00
CommonStyle.verticalGap,
2022-10-04 15:53:04 +02:00
AntList(children: [
if (p.sponsors.totalCount > 0)
2022-10-04 19:13:15 +02:00
_SponsorItem(
count: p.sponsors.totalCount,
login: p.login,
nodes: p.sponsors.nodes!,
sponsoring: false,
),
2022-10-04 15:53:04 +02:00
if (p.sponsoring.totalCount > 0)
2022-10-04 19:13:15 +02:00
_SponsorItem(
count: p.sponsoring.totalCount,
login: p.login,
nodes: p.sponsoring.nodes!,
sponsoring: true,
2022-10-04 15:53:04 +02:00
),
if (p.organizations.totalCount > 0)
AntListItem(
prefix: const Icon(Octicons.organization),
extra: Text(p.organizations.totalCount.toString()),
child: Row(
children: [
Text(AppLocalizations.of(context)!.organizations),
const Spacer(),
for (final org in p.organizations.nodes!) ...[
const SizedBox(width: 6),
Avatar(
2022-10-04 19:13:15 +02:00
square: true,
2022-10-04 15:53:04 +02:00
url: org.avatarUrl,
size: AvatarSize.small,
),
],
],
),
onClick: () {
context.pushUrl('/github/${p.login}?tab=organizations');
},
),
]),
CommonStyle.verticalGap,
2022-09-25 17:11:23 +02:00
AntList(
children: [
AntListItem(
prefix: const Icon(Octicons.repo),
extra: Text(p.repositories.totalCount.toString()),
onClick: () {
context.pushUrl('/github/${p.login}?tab=repositories');
},
child: Text(AppLocalizations.of(context)!.repositories),
),
AntListItem(
prefix: const Icon(Octicons.star),
extra: Text(p.starredRepositories.totalCount.toString()),
onClick: () {
context.pushUrl('/github/${p.login}?tab=stars');
},
child: Text(AppLocalizations.of(context)!.stars),
),
AntListItem(
prefix: const Icon(Octicons.book),
extra: Text(p.gists.totalCount.toString()),
child: Text(AppLocalizations.of(context)!.gists),
onClick: () {
context.push('/github/${p.login}?tab=gists');
},
),
AntListItem(
prefix: const Icon(Octicons.rss),
child: Text(AppLocalizations.of(context)!.events),
onClick: () {
context.push('/github/${p.login}?tab=events');
},
),
],
),
CommonStyle.verticalGap,
2021-01-17 15:08:32 +01:00
_Repos(
2022-09-24 07:22:07 +02:00
p.pinnedItems.nodes!.whereType<GRepoParts>(),
p.repositories.nodes,
2021-01-17 15:08:32 +01:00
),
CommonStyle.verticalGap,
],
);
}
2021-01-17 15:08:32 +01:00
}
2022-09-24 07:22:07 +02:00
class GhViewerScreen extends StatelessWidget {
const GhViewerScreen({super.key});
2019-01-25 13:35:20 +01:00
@override
Widget build(BuildContext context) {
final auth = Provider.of<AuthModel>(context);
2022-09-30 22:50:47 +02:00
return RefreshStatefulScaffold<GUserParts?>(
fetch: () async {
2021-01-17 15:08:32 +01:00
final req = GViewerReq();
2022-10-01 19:26:34 +02:00
final res = await auth.ghGqlClient.request(req).first;
2022-09-24 07:22:07 +02:00
return res.data?.viewer;
},
2022-10-07 19:06:03 +02:00
title: Text(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, _) {
2022-09-24 07:22:07 +02:00
if (p == null) return null;
2021-01-17 15:08:32 +01:00
return _User(p, isViewer: true);
},
);
}
}
2022-09-24 07:22:07 +02:00
class GhUserScreen extends StatelessWidget {
const GhUserScreen(this.login, {super.key});
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);
2022-10-01 19:26:34 +02:00
final res = await auth.ghGqlClient.request(req).first;
2021-01-17 15:08:32 +01:00
return res.data;
},
2022-10-07 19:06:03 +02:00
title: Text(login),
2021-01-31 08:49:28 +01:00
actionBuilder: (payload, _) {
2022-09-24 07:22:07 +02:00
final url = payload?.user?.url ?? payload?.organization?.url;
if (url == null) return null;
2021-01-17 15:08:32 +01:00
return ActionButton(
title: 'User Actions',
2022-09-24 07:22:07 +02:00
items: ActionItem.getUrlActions(url),
2021-01-17 15:08:32 +01:00
);
},
2021-01-31 08:49:28 +01:00
bodyBuilder: (data, setData) {
2022-09-24 07:22:07 +02:00
if (data?.user != null) {
final p = data!.user!;
2021-01-31 08:49:28 +01:00
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) {
2022-09-24 07:22:07 +02:00
b.user.viewerIsFollowing = !b.user.viewerIsFollowing!;
2021-01-31 08:49:28 +01:00
}));
},
)
],
);
2022-10-04 19:13:15 +02:00
}
if (data?.organization != null) {
2022-09-25 19:26:52 +02:00
final p = data!.organization!;
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
UserHeader(
avatarUrl: p.avatarUrl,
name: p.name,
login: p.login,
createdAt: p.createdAt,
bio: p.description,
rightWidgets: [
MutationButton(
active: p.viewerIsFollowing,
text: p.viewerIsFollowing
? AppLocalizations.of(context)!.unfollow
: AppLocalizations.of(context)!.follow,
onTap: () async {
if (p.viewerIsFollowing) {
await auth.ghClient.users.unfollowUser(p.login);
} else {
await auth.ghClient.users.followUser(p.login);
}
setData(data.rebuild((b) {
b.organization.viewerIsFollowing =
!b.organization.viewerIsFollowing!;
}));
},
),
],
),
AntList(
2022-10-02 06:49:55 +02:00
children: [
2022-10-03 22:09:36 +02:00
if (p.location != null)
2022-10-02 06:49:55 +02:00
AntListItem(
prefix: const Icon(Octicons.location),
child: Text(p.location!),
onClick: () {
launchStringUrl(
'https://www.google.com/maps/place/${p.location!.replaceAll(RegExp(r'\s+'), '')}');
},
),
2022-10-03 22:09:36 +02:00
if (p.email != null)
2022-10-02 06:49:55 +02:00
AntListItem(
prefix: const Icon(Octicons.mail),
child: Text(p.email!),
onClick: () {
launchStringUrl('mailto:${p.email!}');
},
),
2022-10-03 22:09:36 +02:00
if (p.websiteUrl != null)
2022-10-02 06:49:55 +02:00
AntListItem(
prefix: const Icon(Octicons.link),
child: Text(p.websiteUrl!),
onClick: () {
var url = p.websiteUrl!;
if (!url.startsWith('http')) {
url = 'http://$url';
}
launchStringUrl(url);
},
),
if (p.twitterUsername != null)
AntListItem(
prefix: const Icon(Ionicons.logo_twitter),
child: Text('@${p.twitterUsername!}'),
onClick: () {
launchStringUrl(
'https://twitter.com/${p.twitterUsername}');
},
),
2022-10-02 06:49:55 +02:00
],
),
CommonStyle.verticalGap,
2022-10-04 19:13:15 +02:00
AntList(
children: [
if (p.sponsors.totalCount > 0)
_SponsorItem(
count: p.sponsors.totalCount,
login: p.login,
nodes: p.sponsors.nodes!,
sponsoring: false,
),
if (p.sponsoring.totalCount > 0)
_SponsorItem(
count: p.sponsoring.totalCount,
login: p.login,
nodes: p.sponsoring.nodes!,
sponsoring: true,
),
],
),
CommonStyle.verticalGap,
2022-10-02 06:49:55 +02:00
AntList(
2022-09-25 19:26:52 +02:00
children: [
AntListItem(
prefix: const Icon(Octicons.repo),
extra: Text(p.pinnableItems.totalCount.toString()),
child: Text(AppLocalizations.of(context)!.repositories),
onClick: () {
context.pushUrl('/github/${p.login}?tab=repositories');
},
),
AntListItem(
prefix: const Icon(Octicons.rss),
child: Text(AppLocalizations.of(context)!.events),
onClick: () {
context.push('/github/${p.login}?tab=events');
},
),
],
),
CommonStyle.verticalGap,
_Repos(
p.pinnedItems.nodes!.whereType<GRepoParts>(),
p.pinnableItems.nodes!.whereType<GRepoParts>(),
),
CommonStyle.verticalGap,
],
);
}
2022-10-04 19:13:15 +02:00
2022-09-24 07:22:07 +02:00
return null; // TODO: not found
},
2019-02-03 08:50:17 +01:00
);
2019-01-25 13:35:20 +01:00
}
}