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

352 lines
12 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2019-01-25 13:35:20 +01:00
import 'package:flutter/cupertino.dart';
2020-01-07 08:07:57 +01:00
import 'package:git_touch/graphql/gh.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';
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';
2020-01-12 07:49:46 +01:00
import 'package:git_touch/widgets/mutation_button.dart';
2019-10-06 15:27:00 +02:00
import 'package:git_touch/widgets/entry_item.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';
2019-09-27 14:52:38 +02:00
import 'package:git_touch/models/auth.dart';
2020-01-29 06:45:22 +01:00
import 'package:git_touch/widgets/user_header.dart';
2019-09-08 14:07:35 +02:00
import 'package:provider/provider.dart';
2019-09-30 10:31:07 +02:00
import 'package:git_touch/widgets/action_button.dart';
import '../generated/l10n.dart';
2020-02-07 07:17:05 +01:00
class GhUserScreen extends StatelessWidget {
2019-02-07 07:35:19 +01:00
final String login;
2020-02-07 07:17:05 +01:00
GhUserScreen(this.login);
2020-01-29 11:00:48 +01:00
bool get isViewer => login == null;
2020-01-01 09:35:50 +01:00
2020-01-07 08:07:57 +01:00
Iterable<Widget> _buildPinnedItems(Iterable<GhUserRepository> pinnedItems,
Iterable<GhUserRepository> repositories) {
String title;
2020-01-07 08:07:57 +01:00
Iterable<GhUserRepository> items = [];
if (pinnedItems.isNotEmpty) {
title = 'pinned repositories';
items = pinnedItems;
} else if (repositories.isNotEmpty) {
title = 'popular repositories';
items = repositories;
}
if (items.isEmpty) return [];
return [
if (title != null) TableViewHeader(title),
...join(
CommonStyle.border,
2020-01-11 10:25:01 +01:00
items.map((v) {
2020-01-11 12:52:17 +01:00
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,
2020-01-11 12:52:17 +01:00
isPrivate: v.isPrivate,
isFork: v.isFork,
2020-01-11 10:25:01 +01:00
);
}).toList(),
),
];
}
2020-01-12 07:49:46 +01:00
Widget _buildUser(BuildContext context, GhUserUser p,
2020-01-11 14:22:52 +01:00
void Function(void Function()) setState) {
final theme = Provider.of<ThemeModel>(context);
2020-01-11 14:22:52 +01:00
final auth = Provider.of<AuthModel>(context);
2020-01-12 07:49:46 +01:00
final login = p.login;
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
2020-01-29 06:45:22 +01:00
UserHeader(
avatarUrl: p.avatarUrl,
name: p.name,
login: p.login,
createdAt: p.createdAt,
bio: p.bio,
isViewer: isViewer,
rightWidgets: [
if (p.viewerCanFollow)
MutationButton(
active: p.viewerIsFollowing,
text: p.viewerIsFollowing
? S.of(context).unfollow
: S.of(context).follow,
onTap: () async {
if (p.viewerIsFollowing) {
await auth.ghClient.users.unfollowUser(p.login);
} else {
await auth.ghClient.users.followUser(p.login);
}
setState(() {
p.viewerIsFollowing = !p.viewerIsFollowing;
});
},
)
],
2020-01-11 14:22:52 +01:00
),
CommonStyle.border,
Row(children: [
EntryItem(
2020-01-12 07:49:46 +01:00
count: p.repositories.totalCount,
text: S.of(context).repositories,
url: '/github/$login?tab=repositories',
),
EntryItem(
2020-01-12 07:49:46 +01:00
count: p.starredRepositories.totalCount,
text: S.of(context).stars,
url: '/github/$login?tab=stars',
),
EntryItem(
2020-01-12 07:49:46 +01:00
count: p.followers.totalCount,
text: S.of(context).followers,
url: '/github/$login?tab=followers',
),
EntryItem(
2020-01-12 07:49:46 +01:00
count: p.following.totalCount,
text: S.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
in p.contributionsCollection.contributionCalendar.weeks)
[
// 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,
TableView(
hasIcon: true,
items: [
TableViewItem(
2020-10-04 15:04:23 +02:00
leftIconData: Icons.rss_feed,
text: Text(S.of(context).events),
2020-10-04 15:04:23 +02:00
url: '/github/$login?tab=events',
),
TableViewItem(
leftIconData: Octicons.book,
text: Text(S.of(context).gists),
2020-10-04 15:04:23 +02:00
url: '/github/$login?tab=gists',
),
TableViewItem(
leftIconData: Octicons.home,
text: Text(S.of(context).organizations),
url: '/github/$login?tab=organizations',
),
2020-01-12 07:49:46 +01:00
if (isNotNullOrEmpty(p.company))
TableViewItem(
leftIconData: Octicons.organization,
2020-10-04 16:20:21 +02:00
text: TextWithAt(
text: p.company,
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,
),
),
2020-01-12 07:49:46 +01:00
if (isNotNullOrEmpty(p.location))
TableViewItem(
leftIconData: Octicons.location,
2020-01-12 07:49:46 +01:00
text: Text(p.location),
onTap: () {
launchUrl('https://www.google.com/maps/place/' +
2020-01-12 07:49:46 +01:00
p.location.replaceAll(RegExp(r'\s+'), ''));
},
),
2020-01-12 07:49:46 +01:00
if (isNotNullOrEmpty(p.email))
TableViewItem(
leftIconData: Octicons.mail,
2020-01-12 07:49:46 +01:00
text: Text(p.email),
onTap: () {
2020-01-12 07:49:46 +01:00
launchUrl('mailto:' + p.email);
},
),
2020-01-12 07:49:46 +01:00
if (isNotNullOrEmpty(p.websiteUrl))
TableViewItem(
leftIconData: Octicons.link,
2020-01-12 07:49:46 +01:00
text: Text(p.websiteUrl),
onTap: () {
2020-01-12 07:49:46 +01:00
var url = p.websiteUrl;
if (!url.startsWith('http')) {
url = 'http://$url';
}
launchUrl(url);
},
),
],
),
2020-01-01 09:35:50 +01:00
CommonStyle.verticalGap,
2020-01-27 07:43:10 +01:00
// if (isViewer)
// TableView(
// hasIcon: true,
// items: [
// TableViewItem(
// leftIconData: Icons.settings,
// text: Text('Settings'),
// url: '/settings',
// ),
// TableViewItem(
// leftIconData: Icons.info_outline,
// text: Text('About'),
// url: '/about',
// ),
// ],
// )
// else
..._buildPinnedItems(
p.pinnedItems.nodes
.where((n) => n is GhUserRepository)
.cast<GhUserRepository>(),
p.repositories.nodes),
CommonStyle.verticalGap,
],
);
}
2020-01-12 07:49:46 +01:00
Widget _buildOrganization(BuildContext context, GhUserOrganization p) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
2020-01-29 06:45:22 +01:00
UserHeader(
avatarUrl: p.avatarUrl,
name: p.name,
login: p.login,
createdAt: p.createdAt,
bio: p.description,
),
CommonStyle.border,
Row(children: [
EntryItem(
2020-01-12 07:49:46 +01:00
count: p.pinnableItems.totalCount,
text: S.of(context).repositories,
url: '/github/${p.login}?tab=orgrepo',
),
EntryItem(
2020-01-12 07:49:46 +01:00
count: p.membersWithRole.totalCount,
text: S.of(context).members,
url: '/github/${p.login}?tab=people',
),
]),
TableView(
hasIcon: true,
items: [
2020-10-04 15:04:23 +02:00
TableViewItem(
leftIconData: Icons.rss_feed,
text: Text(S.of(context).events),
2020-10-04 15:04:23 +02:00
url: '/github/$login?tab=events',
),
2020-01-12 07:49:46 +01:00
if (isNotNullOrEmpty(p.location))
TableViewItem(
leftIconData: Octicons.location,
2020-01-12 07:49:46 +01:00
text: Text(p.location),
onTap: () {
launchUrl('https://www.google.com/maps/place/' +
2020-01-12 07:49:46 +01:00
p.location.replaceAll(RegExp(r'\s+'), ''));
},
),
2020-01-12 07:49:46 +01:00
if (isNotNullOrEmpty(p.email))
TableViewItem(
leftIconData: Octicons.mail,
2020-01-12 07:49:46 +01:00
text: Text(p.email),
onTap: () {
2020-01-12 07:49:46 +01:00
launchUrl('mailto:' + p.email);
},
),
2020-01-12 07:49:46 +01:00
if (isNotNullOrEmpty(p.websiteUrl))
TableViewItem(
leftIconData: Octicons.link,
2020-01-12 07:49:46 +01:00
text: Text(p.websiteUrl),
onTap: () {
2020-01-12 07:49:46 +01:00
var url = p.websiteUrl;
if (!url.startsWith('http')) {
url = 'http://$url';
}
launchUrl(url);
},
),
],
),
2020-01-01 09:35:50 +01:00
CommonStyle.verticalGap,
..._buildPinnedItems(
2020-01-12 07:49:46 +01:00
p.pinnedItems.nodes
2020-01-07 08:07:57 +01:00
.where((n) => n is GhUserRepository)
.cast<GhUserRepository>(),
2020-01-12 07:49:46 +01:00
p.pinnableItems.nodes
2020-01-07 08:07:57 +01:00
.where((n) => n is GhUserRepository)
.cast<GhUserRepository>(),
),
CommonStyle.verticalGap,
],
);
}
2019-01-25 13:35:20 +01:00
@override
Widget build(BuildContext context) {
final auth = Provider.of<AuthModel>(context);
2020-01-07 08:07:57 +01:00
return RefreshStatefulScaffold<GhUserRepositoryOwner>(
fetch: () async {
final data = await auth.gqlClient.execute(GhUserQuery(
2020-01-29 11:00:48 +01:00
variables:
GhUserArguments(login: login ?? '', isViewer: isViewer)));
2020-01-01 09:35:50 +01:00
return isViewer ? data.data.viewer : data.data.repositoryOwner;
},
title: AppBarTitle(isViewer ? S.of(context).me : login),
2020-01-27 07:43:10 +01:00
action: isViewer
? ActionEntry(
iconData: Icons.settings,
2020-01-31 09:54:01 +01:00
url: '/settings',
2020-01-27 07:43:10 +01:00
)
: null,
actionBuilder: isViewer
? null
: (payload, setState) {
switch (payload.resolveType) {
case 'User':
final user = payload as GhUserUser;
return ActionButton(
title: 'User Actions',
items: [...ActionItem.getUrlActions(user.url)],
);
case 'Organization':
final organization = payload as GhUserOrganization;
return ActionButton(
title: 'Organization Actions',
items: [
...ActionItem.getUrlActions(organization.url),
],
);
default:
return null;
}
},
2020-01-11 14:22:52 +01:00
bodyBuilder: (payload, setState) {
2020-01-01 09:35:50 +01:00
if (isViewer) {
2020-01-11 14:22:52 +01:00
return _buildUser(context, payload as GhUserUser, setState);
2020-01-01 09:35:50 +01:00
}
switch (payload.resolveType) {
case 'User':
2020-01-11 14:22:52 +01:00
return _buildUser(context, payload as GhUserUser, setState);
case 'Organization':
2020-01-07 08:07:57 +01:00
return _buildOrganization(context, payload as GhUserOrganization);
default:
return null;
}
},
2019-02-03 08:50:17 +01:00
);
2019-01-25 13:35:20 +01:00
}
}