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

301 lines
9.7 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-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/settings.dart';
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';
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-23 12:28:33 +02:00
import 'package:git_touch/widgets/repository_item.dart';
2019-09-30 10:31:07 +02:00
import 'package:git_touch/widgets/action_button.dart';
2019-10-06 15:27:00 +02:00
import 'package:primer/primer.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-11-02 12:32:52 +01:00
Future _query(BuildContext context) async {
2019-10-06 14:25:07 +02:00
var _login = login ?? Provider.of<AuthModel>(context).activeAccount.login;
2019-09-27 14:52:38 +02:00
var data = await Provider.of<AuthModel>(context).query('''
{
2019-11-02 12:32:52 +01:00
repositoryOwner(login: "$_login") {
__typename
... on User {
$userGqlChunk
company
location
email
websiteUrl
starredRepositories {
totalCount
2019-02-03 08:50:17 +01:00
}
2019-11-02 12:32:52 +01:00
followers {
totalCount
}
following {
totalCount
}
repositories(first: 6, ownerAffiliations: OWNER, orderBy: {field: STARGAZERS, direction: DESC}) {
totalCount
nodes {
2019-09-03 10:35:13 +02:00
$repoChunk
}
2019-02-03 08:50:17 +01:00
}
2019-11-02 12:32:52 +01:00
pinnedItems(first: 6) {
nodes {
... on Repository {
$repoChunk
}
2019-10-06 15:27:00 +02:00
}
}
2019-11-02 12:32:52 +01:00
viewerCanFollow
viewerIsFollowing
url
2019-10-06 15:27:00 +02:00
}
2019-11-02 12:32:52 +01:00
... on Organization {
login
name
avatarUrl
description
location
email
websiteUrl
url
pinnedItems(first: 6) {
nodes {
... on Repository {
$repoChunk
}
2019-10-06 15:27:00 +02:00
}
}
2019-11-02 12:32:52 +01:00
pinnableItems(first: 6, types: [REPOSITORY]) {
totalCount
nodes {
... on Repository {
$repoChunk
}
}
}
membersWithRole {
totalCount
}
2019-10-06 15:27:00 +02:00
}
}
}
2019-11-02 12:32:52 +01:00
'''); // Use pinnableItems instead of organization here due to token permission
return 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-01-25 13:35:20 +01:00
@override
Widget build(BuildContext context) {
2019-09-25 11:06:36 +02:00
return RefreshStatefulScaffold(
2019-09-30 11:13:12 +02:00
fetchData: () {
2019-11-02 12:32:52 +01:00
return Future.wait([
_query(context),
_fetchContributions(context),
]);
},
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
);
2019-11-02 12:32:52 +01:00
switch (data[0]['__typename']) {
case 'User':
if (login == null) {
return ActionEntry(
iconData: Icons.settings,
onTap: () {
Provider.of<ThemeModel>(context).pushRoute(
context, (_) => SettingsScreen(),
fullscreenDialog: true);
},
);
} else {
return ActionButton(
title: 'User Actions',
items: [
if (data != null && data[0]['viewerCanFollow'])
ActionItem(
text:
data[0]['viewerIsFollowing'] ? 'Unfollow' : 'Follow',
onPress: () async {
if (data[0]['viewerIsFollowing']) {
await Provider.of<AuthModel>(context)
.deleteWithCredentials('/user/following/$login');
data[0]['viewerIsFollowing'] = false;
} else {
Provider.of<AuthModel>(context)
.putWithCredentials('/user/following/$login');
data[0]['viewerIsFollowing'] = true;
}
},
),
if (data != null) ...[
ActionItem.share(data[0]['url']),
ActionItem.launch(data[0]['url']),
],
],
);
}
break;
case 'Organization':
return ActionButton(
title: 'Organization Actions',
items: [
if (data != null) ...[
2019-11-02 13:54:23 +01:00
ActionItem.share(data[0]['url']),
ActionItem.launch(data[0]['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, _) {
var user = data[0];
var contributions = data[1];
final isOrganization = user['__typename'] == 'Organization';
2019-11-02 12:32:52 +01:00
return Column(
2019-09-05 11:58:14 +02:00
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
2019-10-06 15:27:00 +02:00
UserItem(
2019-11-02 13:54:23 +01:00
login: user['login'],
name: user['name'],
avatarUrl: user['avatarUrl'],
bio: isOrganization ? user['description'] : user['bio'],
2019-10-06 15:27:00 +02:00
inUserScreen: true,
),
2019-10-02 10:09:54 +02:00
CommonStyle.border,
2019-10-06 15:27:00 +02:00
Row(children: [
if (isOrganization) ...[
EntryItem(
2019-11-02 13:54:23 +01:00
count: user['pinnableItems']['totalCount'],
2019-10-06 15:27:00 +02:00
text: 'Repositories',
screenBuilder: (context) =>
2019-11-02 13:54:23 +01:00
RepositoriesScreen.ofOrganization(user['login']),
2019-10-06 15:27:00 +02:00
),
EntryItem(
2019-11-02 13:54:23 +01:00
count: user['membersWithRole']['totalCount'],
2019-10-06 15:27:00 +02:00
text: 'Members',
screenBuilder: (context) =>
2019-11-02 13:54:23 +01:00
UsersScreen.members(user['login']),
2019-10-06 15:27:00 +02:00
),
] else ...[
EntryItem(
2019-11-02 13:54:23 +01:00
count: user['repositories']['totalCount'],
2019-10-06 15:27:00 +02:00
text: 'Repositories',
2019-11-02 13:54:23 +01:00
screenBuilder: (context) => RepositoriesScreen(user['login']),
2019-10-06 15:27:00 +02:00
),
EntryItem(
2019-11-02 13:54:23 +01:00
count: user['starredRepositories']['totalCount'],
2019-10-06 15:27:00 +02:00
text: 'Stars',
screenBuilder: (context) =>
2019-11-02 13:54:23 +01:00
RepositoriesScreen.stars(user['login']),
2019-10-06 15:27:00 +02:00
),
EntryItem(
2019-11-02 13:54:23 +01:00
count: user['followers']['totalCount'],
2019-10-06 15:27:00 +02:00
text: 'Followers',
screenBuilder: (context) =>
2019-11-02 13:54:23 +01:00
UsersScreen.followers(user['login']),
2019-10-06 15:27:00 +02:00
),
EntryItem(
2019-11-02 13:54:23 +01:00
count: user['following']['totalCount'],
2019-10-06 15:27:00 +02:00
text: 'Following',
screenBuilder: (context) =>
2019-11-02 13:54:23 +01:00
UsersScreen.following(user['login']),
2019-10-06 15:27:00 +02:00
),
]
2019-09-05 11:58:14 +02:00
]),
2019-10-02 10:09:54 +02:00
CommonStyle.verticalGap,
2019-10-06 14:25:07 +02:00
if (contributions.isNotEmpty) ...[
UserContributions(contributions),
CommonStyle.verticalGap,
],
2019-09-15 09:08:09 +02:00
TableView(
hasIcon: true,
items: [
2019-11-02 13:54:23 +01:00
if (!isOrganization && isNotNullOrEmpty(user['company']))
2019-09-15 09:08:09 +02:00
TableViewItem(
leftIconData: Octicons.organization,
2019-11-02 13:54:23 +01:00
text: TextContainsOrganization(user['company'],
2019-09-27 12:23:47 +02:00
style: TextStyle(
fontSize: 16, color: PrimerColors.gray900),
overflow: TextOverflow.ellipsis),
2019-09-15 09:08:09 +02:00
),
2019-11-02 13:54:23 +01:00
if (isNotNullOrEmpty(user['location']))
2019-09-15 09:08:09 +02:00
TableViewItem(
leftIconData: Octicons.location,
2019-11-02 13:54:23 +01:00
text: Text(user['location']),
2019-09-15 09:08:09 +02:00
onTap: () {
2019-09-29 07:32:53 +02:00
launchUrl('https://www.google.com/maps/place/' +
2019-11-02 13:54:23 +01:00
(user['location'] as String)
2019-09-15 09:08:09 +02:00
.replaceAll(RegExp(r'\s+'), ''));
},
),
2019-11-02 13:54:23 +01:00
if (isNotNullOrEmpty(user['email']))
2019-09-15 09:08:09 +02:00
TableViewItem(
leftIconData: Octicons.mail,
2019-11-02 13:54:23 +01:00
text: Text(user['email']),
2019-09-15 09:08:09 +02:00
onTap: () {
2019-11-02 13:54:23 +01:00
launchUrl('mailto:' + user['email']);
2019-09-15 09:08:09 +02:00
},
),
2019-11-02 13:54:23 +01:00
if (isNotNullOrEmpty(user['websiteUrl']))
2019-09-15 09:08:09 +02:00
TableViewItem(
leftIconData: Octicons.link,
2019-11-02 13:54:23 +01:00
text: Text(user['websiteUrl']),
2019-09-15 09:08:09 +02:00
onTap: () {
2019-11-02 13:54:23 +01:00
var url = user['websiteUrl'] as String;
2019-09-15 09:08:09 +02:00
if (!url.startsWith('http')) {
url = 'http://$url';
}
2019-09-29 07:32:53 +02:00
launchUrl(url);
2019-09-15 09:08:09 +02:00
},
),
],
),
2019-10-06 09:50:22 +02:00
...buildPinnedItems(
2019-11-02 13:54:23 +01:00
user['pinnedItems']['nodes'],
user[isOrganization ? 'pinnableItems' : 'repositories']
2019-10-06 15:27:00 +02:00
['nodes']),
2019-10-02 10:09:54 +02:00
CommonStyle.verticalGap,
],
);
},
2019-02-03 08:50:17 +01:00
);
2019-01-25 13:35:20 +01:00
}
}