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';
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';
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';
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;
2020-01-11 14:22:52 +01:00
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,
2020-01-12 07:49:46 +01:00
followWidget: p.viewerCanFollow == true
? MutationButton(
2020-02-06 07:23:54 +01:00
active: p.viewerIsFollowing,
2020-01-12 07:49:46 +01:00
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-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: 'Repositories',
url: '/${p.login}?tab=orgrepo',
),
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-27 07:43:10 +01:00
final theme = Provider.of<ThemeModel>(context);
2020-01-07 08:07:57 +01:00
return RefreshStatefulScaffold<GhUserRepositoryOwner>(
fetchData: () 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;
},
2020-01-04 15:46:33 +01:00
title: AppBarTitle(isViewer ? '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
}
}