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

332 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-28 18:25:14 +02: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';
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-10-06 14:25:07 +02:00
import 'package:git_touch/widgets/user_contributions.dart';
2019-09-14 11:19:33 +02:00
import 'package:git_touch/widgets/user_item.dart';
import 'package:github_contributions/github_contributions.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';
import 'package:tuple/tuple.dart';
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
Future<GithubUserRepositoryOwner> _query(BuildContext context) async {
final data = await Provider.of<AuthModel>(context)
.gqlClient
2019-12-07 08:28:43 +01:00
.execute(GithubUserQuery(variables: GithubUserArguments(login: login)));
return data.data.repositoryOwner;
2019-10-06 15:27:00 +02:00
}
2019-11-02 12:32:52 +01:00
Future<List<ContributionsInfo>> _fetchContributions(
2019-10-06 14:25:07 +02:00
BuildContext context) async {
var _login = login ?? Provider.of<AuthModel>(context).activeAccount.login;
switch (Provider.of<AuthModel>(context).activeAccount.platform) {
case PlatformType.gitlab:
return [];
default:
try {
return await getContributions(_login);
} catch (err) {
return [];
}
}
}
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(),
),
];
}
2019-12-07 08:28:43 +01:00
Widget _buildUser(BuildContext context, GithubUserUser user,
List<ContributionsInfo> contributions) {
final theme = Provider.of<ThemeModel>(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
UserItem(
login: user.login,
name: user.name,
avatarUrl: user.avatarUrl,
bio: user.bio,
inUserScreen: true,
),
CommonStyle.border,
Row(children: [
EntryItem(
count: user.repositories.totalCount,
text: 'Repositories',
screenBuilder: (context) => RepositoriesScreen(user.login),
),
EntryItem(
count: user.starredRepositories.totalCount,
text: 'Stars',
screenBuilder: (context) => RepositoriesScreen.stars(user.login),
),
EntryItem(
count: user.followers.totalCount,
text: 'Followers',
2019-12-07 16:10:30 +01:00
screenBuilder: (context) =>
UsersScreen(user.login, UsersScreenType.follower),
),
EntryItem(
count: user.following.totalCount,
text: 'Following',
2019-12-07 16:10:30 +01:00
screenBuilder: (context) =>
UsersScreen(user.login, UsersScreenType.following),
),
]),
CommonStyle.verticalGap,
UserContributions(contributions),
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),
overflow: TextOverflow.ellipsis,
),
),
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: 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-07 15:25:37 +01:00
screenBuilder: (context) => RepositoriesScreen(payload.login),
),
EntryItem(
count: payload.membersWithRole.totalCount,
text: 'Members',
2019-12-07 16:10:30 +01:00
screenBuilder: (context) =>
UsersScreen(payload.login, UsersScreenType.member),
),
]),
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) {
return RefreshStatefulScaffold<
2019-12-07 08:28:43 +01:00
Tuple2<GithubUserRepositoryOwner, List<ContributionsInfo>>>(
fetchData: () async {
final vs = await Future.wait([
2019-11-02 12:32:52 +01:00
_query(context),
_fetchContributions(context),
]);
2019-12-07 08:28:43 +01:00
return Tuple2(vs[0] as GithubUserRepositoryOwner,
vs[1] as List<ContributionsInfo>);
},
2019-11-02 12:32:52 +01:00
title: AppBarTitle('User'), // TODO:
2019-11-02 13:54:23 +01:00
actionBuilder: (data, _) {
2019-11-02 12:32:52 +01:00
if (data == 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
);
final payload = data.item1;
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-11-02 12:32:52 +01:00
if (login == null) {
return ActionEntry(
iconData: Icons.settings,
onTap: () {
2019-12-12 15:02:58 +01:00
Provider.of<ThemeModel>(context).push(context, '/settings');
2019-11-02 12:32:52 +01:00
},
);
} else {
return ActionButton(
title: 'User Actions',
items: [
if (user.viewerCanFollow)
2019-11-02 12:32:52 +01:00
ActionItem(
text: user.viewerIsFollowing ? 'Unfollow' : 'Follow',
2019-11-02 16:36:49 +01:00
onPress: (_) async {
if (user.viewerIsFollowing) {
2019-11-02 12:32:52 +01:00
await Provider.of<AuthModel>(context)
.deleteWithCredentials('/user/following/$login');
user.viewerIsFollowing = false;
2019-11-02 12:32:52 +01:00
} else {
Provider.of<AuthModel>(context)
.putWithCredentials('/user/following/$login');
user.viewerIsFollowing = true;
2019-11-02 12:32:52 +01:00
}
},
),
if (data != null) ...[
ActionItem.share(user.url),
ActionItem.launch(user.url),
2019-11-02 12:32:52 +01:00
],
],
);
}
break;
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: [
if (data != 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;
}
},
2019-11-02 13:54:23 +01:00
bodyBuilder: (data, _) {
final payload = data.item1;
switch (payload.resolveType) {
case 'User':
2019-12-07 08:28:43 +01:00
return _buildUser(context, payload as GithubUserUser, data.item2);
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
}
}