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

435 lines
14 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/screens/users.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-10-06 15:27:00 +02:00
import 'package:git_touch/screens/repositories.dart';
2020-01-01 08:58:49 +01:00
import 'package:git_touch/widgets/avatar.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';
2019-09-27 12:23:47 +02:00
import 'package:git_touch/widgets/text_contains_organization.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-09-30 10:31:07 +02:00
import 'package:git_touch/widgets/action_button.dart';
2019-12-13 06:13:45 +01:00
final userRouter = RouterScreen(
2019-12-13 06:40:05 +01:00
'/:login',
(context, parameters) {
final login = parameters['login'].first;
2019-12-13 15:23:18 +01:00
final tab = parameters['tab']?.first;
2019-12-13 06:40:05 +01:00
switch (tab) {
case 'followers':
return UsersScreen(login, UsersScreenType.follower);
case 'following':
return UsersScreen(login, UsersScreenType.following);
case 'people':
return UsersScreen(login, UsersScreenType.member);
case 'stars':
return RepositoriesScreen.stars(login);
case 'repositories':
return RepositoriesScreen(login);
default:
2019-12-13 15:23:18 +01:00
return UserScreen(login);
2019-12-13 06:40:05 +01:00
}
},
);
2019-12-13 06:13:45 +01:00
2019-09-13 10:55:27 +02:00
class UserScreen extends StatelessWidget {
2019-02-07 07:35:19 +01:00
final String login;
2019-11-02 12:32:52 +01:00
UserScreen(this.login);
2019-02-07 07:35:19 +01:00
2020-01-01 09:35:50 +01:00
bool get isViewer => login.isEmpty;
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-11 10:25:01 +01:00
v.owner.login,
v.owner.avatarUrl,
v.name,
v.description,
v.stargazers.totalCount,
v.forks.totalCount,
v.primaryLanguage?.name,
v.primaryLanguage?.color,
2020-01-11 12:25:33 +01:00
null,
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-01 08:58:49 +01:00
Widget _buildHeader(BuildContext context, String avatarUrl, String name,
2020-01-11 14:22:52 +01:00
String login, DateTime createdAt, String bio,
{Widget followWidget}) {
2020-01-01 08:58:49 +01:00
final theme = Provider.of<ThemeModel>(context);
return Container(
padding: CommonStyle.padding,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Row(
children: <Widget>[
2020-01-12 07:49:46 +01:00
Avatar(url: avatarUrl, size: AvatarSize.extraLarge),
2020-01-11 14:22:52 +01:00
if (followWidget != null) ...[
Expanded(child: Container()),
followWidget,
]
],
),
SizedBox(height: 8),
if (name != null) ...[
Text(
name,
style: TextStyle(
2020-01-14 11:13:07 +01:00
color: theme.paletteOf(context).text,
2020-01-11 14:22:52 +01:00
fontSize: 20,
fontWeight: FontWeight.w600,
),
),
SizedBox(height: 4),
],
Text(
login,
style: TextStyle(
2020-01-14 11:13:07 +01:00
color: theme.paletteOf(context).primary,
2020-01-11 14:22:52 +01:00
fontSize: 18,
),
),
SizedBox(height: 8),
Row(
children: <Widget>[
Icon(
Octicons.clock,
size: 16,
2020-01-14 11:13:07 +01:00
color: theme.paletteOf(context).tertiaryText,
2020-01-11 14:22:52 +01:00
),
SizedBox(width: 4),
Text(
'Joined on ${dateFormat.format(createdAt)}',
style: TextStyle(
2020-01-14 11:13:07 +01:00
color: theme.paletteOf(context).tertiaryText,
2020-01-11 14:22:52 +01:00
fontSize: 16,
2020-01-01 08:58:49 +01:00
),
2020-01-11 14:22:52 +01:00
),
2020-01-01 08:58:49 +01:00
],
),
if (bio != null && bio.isNotEmpty) ...[
2020-01-11 14:22:52 +01:00
SizedBox(height: 10),
2020-01-01 08:58:49 +01:00
Text(
bio,
style: TextStyle(
2020-01-14 11:13:07 +01:00
color: theme.paletteOf(context).secondaryText,
2020-01-01 08:58:49 +01:00
fontSize: 17,
),
)
]
],
),
);
}
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;
2020-01-11 14:22:52 +01:00
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
2020-01-11 14:22:52 +01:00
_buildHeader(
context,
2020-01-12 07:49:46 +01:00
p.avatarUrl,
p.name,
p.login,
p.createdAt,
p.bio,
followWidget: p.viewerCanFollow == true
? MutationButton(
text: p.viewerIsFollowing ? 'Unfollow' : 'Follow',
2020-01-11 14:22:52 +01:00
onPressed: () async {
final res = await auth.gqlClient.execute(
GhFollowQuery(
variables: GhFollowArguments(
2020-01-12 07:49:46 +01:00
id: p.id,
flag: !p.viewerIsFollowing,
2020-01-11 14:22:52 +01:00
),
),
);
setState(() {
2020-01-12 07:49:46 +01:00
p.viewerIsFollowing =
2020-01-11 14:22:52 +01:00
res.data.unfollowUser?.user?.viewerIsFollowing ??
res.data.followUser.user.viewerIsFollowing;
});
},
)
: null,
),
CommonStyle.border,
Row(children: [
EntryItem(
2020-01-12 07:49:46 +01:00
count: p.repositories.totalCount,
text: 'Repositories',
2019-12-13 06:40:05 +01:00
url: '/$login?tab=repositories',
),
EntryItem(
2020-01-12 07:49:46 +01:00
count: p.starredRepositories.totalCount,
text: 'Stars',
2019-12-13 06:40:05 +01:00
url: '/$login?tab=stars',
),
EntryItem(
2020-01-12 07:49:46 +01:00
count: p.followers.totalCount,
text: 'Followers',
2019-12-13 06:40:05 +01:00
url: '/$login?tab=followers',
),
EntryItem(
2020-01-12 07:49:46 +01:00
count: p.following.totalCount,
text: 'Following',
2019-12-13 06:40:05 +01:00
url: '/$login?tab=following',
),
]),
2020-01-02 06:56:50 +01:00
CommonStyle.border,
2020-01-01 06:26:20 +01:00
Container(
padding: CommonStyle.padding,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
reverse: true,
child: Wrap(
spacing: 3,
2020-01-12 07:49:46 +01:00
children: p.contributionsCollection.contributionCalendar.weeks
2020-01-01 06:26:20 +01:00
.map((week) {
return Wrap(
direction: Axis.vertical,
spacing: 3,
children: week.contributionDays.map((day) {
var color = convertColor(day.color);
2020-01-16 05:45:04 +01:00
if (theme.brightness == Brightness.dark) {
2020-01-01 06:26:20 +01:00
color = Color.fromRGBO(0xff - color.red,
0xff - color.green, 0xff - color.blue, 1);
}
return SizedBox(
width: 10,
height: 10,
child: DecoratedBox(
decoration: BoxDecoration(color: color),
),
);
}).toList(),
);
}).toList(),
),
),
),
2020-01-02 06:56:50 +01:00
CommonStyle.border,
TableView(
hasIcon: true,
items: [
2020-01-12 07:49:46 +01:00
if (isNotNullOrEmpty(p.company))
TableViewItem(
leftIconData: Octicons.organization,
text: TextContainsOrganization(
2020-01-12 07:49:46 +01:00
p.company,
2020-01-14 11:13:07 +01:00
style: TextStyle(
fontSize: 17, color: theme.paletteOf(context).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,
if (isViewer)
TableView(
hasIcon: true,
items: [
TableViewItem(
2020-01-12 10:13:48 +01:00
leftIconData: Icons.settings,
2020-01-01 09:35:50 +01:00
text: Text('Settings'),
url: '/settings',
),
2020-01-04 06:28:29 +01:00
TableViewItem(
2020-01-12 10:13:48 +01:00
leftIconData: Icons.info_outline,
2020-01-04 06:28:29 +01:00
text: Text('About'),
url: '/about',
),
2020-01-01 09:35:50 +01:00
],
)
else
..._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.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-12 07:49:46 +01:00
_buildHeader(
context, p.avatarUrl, p.name, p.login, p.createdAt, p.description),
CommonStyle.border,
Row(children: [
EntryItem(
2020-01-12 07:49:46 +01:00
count: p.pinnableItems.totalCount,
text: 'Repositories',
2020-01-12 07:49:46 +01:00
url: '/${p.login}?tab=repositories',
),
EntryItem(
2020-01-12 07:49:46 +01:00
count: p.membersWithRole.totalCount,
text: 'Members',
2020-01-12 07:49:46 +01:00
url: '/${p.login}?tab=people',
),
]),
TableView(
hasIcon: true,
items: [
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>(
fetchData: () async {
final data = await auth.gqlClient.execute(GhUserQuery(
variables: GhUserArguments(login: login, isViewer: isViewer)));
2020-01-01 09:35:50 +01:00
return isViewer ? data.data.viewer : data.data.repositoryOwner;
},
2020-01-04 15:46:33 +01:00
title: AppBarTitle(isViewer ? 'Me' : login),
actionBuilder: (payload, setState) {
switch (payload.resolveType) {
2019-11-02 12:32:52 +01:00
case 'User':
2020-01-07 08:07:57 +01:00
final user = payload as GhUserUser;
2019-12-17 06:34:53 +01:00
return ActionButton(
title: 'User Actions',
2020-01-27 06:41:17 +01:00
items: [...ActionItem.getUrlActions(user.url)],
2019-12-17 06:34:53 +01:00
);
2019-11-02 12:32:52 +01:00
case 'Organization':
2020-01-07 08:07:57 +01:00
final organization = payload as GhUserOrganization;
2019-11-02 12:32:52 +01:00
return ActionButton(
title: 'Organization Actions',
items: [
2020-01-27 06:41:17 +01:00
...ActionItem.getUrlActions(organization.url),
2019-09-30 09:46:06 +02:00
],
2019-11-02 12:32:52 +01:00
);
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
}
}