1
0
mirror of https://github.com/git-touch/git-touch synced 2025-02-15 19:10:45 +01:00

358 lines
12 KiB
Dart
Raw Normal View History

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