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

340 lines
11 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2019-01-25 13:35:20 +01:00
import 'package:flutter/cupertino.dart';
2019-12-07 08:28:43 +01:00
import 'package:git_touch/graphql/github_user.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';
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-14 11:19:33 +02:00
import 'package:git_touch/widgets/user_item.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
2019-12-07 08:28:43 +01:00
Iterable<Widget> _buildPinnedItems(Iterable<GithubUserRepository> pinnedItems,
Iterable<GithubUserRepository> repositories) {
String title;
2019-12-07 08:28:43 +01:00
Iterable<GithubUserRepository> items = [];
if (pinnedItems.isNotEmpty) {
title = 'pinned repositories';
items = pinnedItems;
} else if (repositories.isNotEmpty) {
title = 'popular repositories';
items = repositories;
}
if (items.isEmpty) return [];
return [
CommonStyle.verticalGap,
if (title != null) TableViewHeader(title),
...join(
CommonStyle.border,
items.map((item) {
return RepositoryItem.github(item);
}).toList(),
),
];
}
2020-01-01 06:26:20 +01:00
Widget _buildUser(BuildContext context, GithubUserUser user) {
final theme = Provider.of<ThemeModel>(context);
2019-12-13 06:40:05 +01:00
final login = user.login;
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
UserItem(
login: user.login,
name: user.name,
avatarUrl: user.avatarUrl,
bio: Text(user.bio ?? ''),
inUserScreen: true,
),
CommonStyle.border,
Row(children: [
EntryItem(
count: user.repositories.totalCount,
text: 'Repositories',
2019-12-13 06:40:05 +01:00
url: '/$login?tab=repositories',
),
EntryItem(
count: user.starredRepositories.totalCount,
text: 'Stars',
2019-12-13 06:40:05 +01:00
url: '/$login?tab=stars',
),
EntryItem(
count: user.followers.totalCount,
text: 'Followers',
2019-12-13 06:40:05 +01:00
url: '/$login?tab=followers',
),
EntryItem(
count: user.following.totalCount,
text: 'Following',
2019-12-13 06:40:05 +01:00
url: '/$login?tab=following',
),
]),
CommonStyle.verticalGap,
2020-01-01 06:26:20 +01:00
Container(
color: theme.palette.background,
padding: CommonStyle.padding,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
reverse: true,
child: Wrap(
spacing: 3,
children: user.contributionsCollection.contributionCalendar.weeks
.map((week) {
return Wrap(
direction: Axis.vertical,
spacing: 3,
children: week.contributionDays.map((day) {
var color = convertColor(day.color);
if (theme.brightness == Brightness.dark) {
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(),
),
),
),
CommonStyle.verticalGap,
TableView(
hasIcon: true,
items: [
if (isNotNullOrEmpty(user.company))
TableViewItem(
leftIconData: Octicons.organization,
text: TextContainsOrganization(
user.company,
style: TextStyle(fontSize: 16, color: theme.palette.text),
2020-01-01 05:55:27 +01:00
oneLine: true,
),
),
if (isNotNullOrEmpty(user.location))
TableViewItem(
leftIconData: Octicons.location,
text: Text(user.location),
onTap: () {
launchUrl('https://www.google.com/maps/place/' +
user.location.replaceAll(RegExp(r'\s+'), ''));
},
),
if (isNotNullOrEmpty(user.email))
TableViewItem(
leftIconData: Octicons.mail,
text: Text(user.email),
onTap: () {
launchUrl('mailto:' + user.email);
},
),
if (isNotNullOrEmpty(user.websiteUrl))
TableViewItem(
leftIconData: Octicons.link,
text: Text(user.websiteUrl),
onTap: () {
var url = user.websiteUrl;
if (!url.startsWith('http')) {
url = 'http://$url';
}
launchUrl(url);
},
),
],
),
..._buildPinnedItems(
user.pinnedItems.nodes
2019-12-07 08:28:43 +01:00
.where((n) => n is GithubUserRepository)
.cast<GithubUserRepository>(),
user.repositories.nodes),
CommonStyle.verticalGap,
],
);
}
2019-12-07 08:28:43 +01:00
Widget _buildOrganization(
BuildContext context, GithubUserOrganization payload) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
UserItem(
login: payload.login,
name: payload.name,
avatarUrl: payload.avatarUrl,
bio: Text(payload.description ?? ''),
inUserScreen: true,
),
CommonStyle.border,
Row(children: [
EntryItem(
2019-12-07 06:31:54 +01:00
count: payload.pinnableItems.totalCount,
text: 'Repositories',
2019-12-13 06:40:05 +01:00
url: '/$login?tab=repositories',
),
EntryItem(
count: payload.membersWithRole.totalCount,
text: 'Members',
2019-12-13 06:40:05 +01:00
url: '/$login?tab=people',
),
]),
CommonStyle.verticalGap,
TableView(
hasIcon: true,
items: [
if (isNotNullOrEmpty(payload.location))
TableViewItem(
leftIconData: Octicons.location,
text: Text(payload.location),
onTap: () {
launchUrl('https://www.google.com/maps/place/' +
payload.location.replaceAll(RegExp(r'\s+'), ''));
},
),
if (isNotNullOrEmpty(payload.email))
TableViewItem(
leftIconData: Octicons.mail,
text: Text(payload.email),
onTap: () {
launchUrl('mailto:' + payload.email);
},
),
if (isNotNullOrEmpty(payload.websiteUrl))
TableViewItem(
leftIconData: Octicons.link,
text: Text(payload.websiteUrl),
onTap: () {
var url = payload.websiteUrl;
if (!url.startsWith('http')) {
url = 'http://$url';
}
launchUrl(url);
},
),
],
),
..._buildPinnedItems(
payload.pinnedItems.nodes
2019-12-07 08:28:43 +01:00
.where((n) => n is GithubUserRepository)
.cast<GithubUserRepository>(),
payload.pinnableItems.nodes
2019-12-07 08:28:43 +01:00
.where((n) => n is GithubUserRepository)
.cast<GithubUserRepository>(),
),
CommonStyle.verticalGap,
],
);
}
2019-01-25 13:35:20 +01:00
@override
Widget build(BuildContext context) {
2020-01-01 06:26:20 +01:00
return RefreshStatefulScaffold<GithubUserRepositoryOwner>(
fetchData: () async {
2020-01-01 06:26:20 +01:00
final data = await Provider.of<AuthModel>(context).gqlClient.execute(
GithubUserQuery(variables: GithubUserArguments(login: login)));
return data.data.repositoryOwner;
},
2019-11-02 12:32:52 +01:00
title: AppBarTitle('User'), // TODO:
2020-01-01 06:26:20 +01:00
actionBuilder: (payload, _) {
if (payload == null)
2019-10-06 15:27:00 +02:00
return ActionButton(
2019-11-02 12:32:52 +01:00
title: "Actions",
items: [],
2019-10-06 15:27:00 +02:00
);
switch (payload.resolveType) {
2019-11-02 12:32:52 +01:00
case 'User':
2019-12-07 08:28:43 +01:00
final user = payload as GithubUserUser;
2019-12-17 06:34:53 +01:00
return ActionButton(
title: 'User Actions',
items: [
if (user.viewerCanFollow)
ActionItem(
text: user.viewerIsFollowing ? 'Unfollow' : 'Follow',
onPress: (_) async {
if (user.viewerIsFollowing) {
await Provider.of<AuthModel>(context)
.deleteWithCredentials('/user/following/$login');
user.viewerIsFollowing = false;
} else {
Provider.of<AuthModel>(context)
.putWithCredentials('/user/following/$login');
user.viewerIsFollowing = true;
}
},
),
2020-01-01 06:26:20 +01:00
if (payload != null) ...[
2019-12-17 06:34:53 +01:00
ActionItem.share(user.url),
ActionItem.launch(user.url),
2019-11-02 12:32:52 +01:00
],
2019-12-17 06:34:53 +01:00
],
);
2019-11-02 12:32:52 +01:00
case 'Organization':
2019-12-07 08:28:43 +01:00
final organization = payload as GithubUserOrganization;
2019-11-02 12:32:52 +01:00
return ActionButton(
title: 'Organization Actions',
items: [
2020-01-01 06:26:20 +01:00
if (payload != null) ...[
ActionItem.share(organization.url),
ActionItem.launch(organization.url),
2019-11-02 12:32:52 +01:00
],
2019-09-30 09:46:06 +02:00
],
2019-11-02 12:32:52 +01:00
);
default:
return null;
}
},
2020-01-01 06:26:20 +01:00
bodyBuilder: (payload, _) {
switch (payload.resolveType) {
case 'User':
2020-01-01 06:26:20 +01:00
return _buildUser(context, payload as GithubUserUser);
case 'Organization':
2019-12-07 08:28:43 +01:00
return _buildOrganization(
context, payload as GithubUserOrganization);
default:
return null;
}
},
2019-02-03 08:50:17 +01:00
);
2019-01-25 13:35:20 +01:00
}
}