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

275 lines
8.3 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-09-23 12:28:33 +02:00
import 'package:git_touch/screens/repositories.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-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 12:23:47 +02:00
import 'package:primer/primer.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-02-04 14:38:29 +01:00
import '../widgets/entry_item.dart';
2019-09-23 12:28:33 +02:00
import 'package:git_touch/widgets/repository_item.dart';
2019-02-20 09:31:22 +01:00
import '../widgets/action.dart';
2019-02-04 14:38:29 +01:00
import '../screens/users.dart';
import '../screens/settings.dart';
2019-01-31 07:37:25 +01:00
import '../utils/utils.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-09-13 11:00:45 +02:00
final bool isMe;
2019-02-07 07:35:19 +01:00
2019-09-13 11:00:45 +02:00
UserScreen(this.login, {this.isMe = false});
2019-02-07 07:35:19 +01:00
2019-09-13 10:55:27 +02:00
Future query(BuildContext context) async {
2019-09-27 14:52:38 +02:00
var data = await Provider.of<AuthModel>(context).query('''
{
user(login: "$login") {
2019-09-27 15:02:55 +02:00
$userGqlChunk
company
location
2019-09-04 16:59:33 +02:00
email
websiteUrl
starredRepositories {
totalCount
}
followers {
totalCount
}
following {
totalCount
}
2019-09-14 12:26:32 +02:00
repositories(first: 6, ownerAffiliations: OWNER, orderBy: {field: STARGAZERS, direction: DESC}) {
2019-02-03 08:50:17 +01:00
totalCount
nodes {
2019-02-04 11:32:39 +01:00
$repoChunk
2019-02-03 08:50:17 +01:00
}
}
2019-09-14 12:26:32 +02:00
pinnedItems(first: 6) {
2019-02-03 08:50:17 +01:00
nodes {
2019-09-03 10:35:13 +02:00
... on Repository {
$repoChunk
}
2019-02-03 08:50:17 +01:00
}
}
viewerCanFollow
viewerIsFollowing
url
}
}
''');
2019-02-07 07:35:19 +01:00
return data['user'];
}
2019-02-04 11:32:39 +01:00
2019-09-14 12:26:32 +02:00
Iterable<Widget> _buildRepos(payload) {
2019-09-21 17:54:25 +02:00
String title;
2019-09-21 19:56:26 +02:00
List items = [];
2019-09-14 12:26:32 +02:00
2019-09-21 19:56:26 +02:00
var pinnedItems = payload['pinnedItems']['nodes'] as List;
var repositories = payload['repositories']['nodes'] as List;
if (pinnedItems.isNotEmpty) {
2019-09-21 17:54:25 +02:00
title = 'pinned repositories';
2019-09-21 19:56:26 +02:00
items = pinnedItems;
} else if (repositories.isNotEmpty) {
2019-09-21 17:54:25 +02:00
title = 'popular repositories';
2019-09-21 19:56:26 +02:00
items = repositories;
2019-02-04 11:32:39 +01:00
}
2019-09-14 12:26:32 +02:00
items = items
.where((x) => x != null)
.toList(); // TODO: Pinned items may include somethings other than repo
if (items.isEmpty) return [];
return [
2019-09-14 20:05:34 +02:00
borderView1,
2019-09-21 17:54:25 +02:00
if (title != null) TableViewHeader(title),
2019-09-28 18:25:14 +02:00
borderView,
2019-09-14 12:26:32 +02:00
...join(
2019-09-14 17:48:01 +02:00
borderView,
2019-09-14 12:26:32 +02:00
items.map((item) {
2019-09-23 12:28:33 +02:00
return RepositoryItem(item);
2019-09-14 12:26:32 +02:00
}).toList(),
2019-09-28 18:25:14 +02:00
),
borderView,
2019-09-14 12:26:32 +02:00
];
2019-02-04 11:32:39 +01:00
}
Widget _buildContributions(List<ContributionsInfo> contributions) {
2019-09-14 18:20:46 +02:00
final row = Row(
children: <Widget>[],
crossAxisAlignment: CrossAxisAlignment.start,
);
Column column;
contributions.asMap().forEach((i, v) {
var rect = SizedBox(
width: 10,
height: 10,
child: DecoratedBox(
decoration: BoxDecoration(
color: convertColor(v.color),
),
),
);
if (i % 7 == 0) {
column = Column(children: <Widget>[rect]);
row.children.add(column);
row.children.add(SizedBox(width: 3));
} else {
column.children.add(SizedBox(height: 3));
column.children.add(rect);
}
});
return Container(
2019-09-28 18:25:14 +02:00
color: Colors.white,
padding: EdgeInsets.all(10),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
2019-09-14 18:20:46 +02:00
reverse: true,
child: row,
),
);
}
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-24 13:58:34 +02:00
onRefresh: () {
return Future.wait(
2019-09-13 10:55:27 +02:00
[query(context), getContributions(login)],
);
},
2019-09-13 09:55:58 +02:00
title: AppBarTitle('User'),
trailingBuilder: (data) {
2019-09-13 11:00:45 +02:00
if (isMe) {
2019-09-28 18:25:14 +02:00
return ActionEntry(
iconData: Icons.settings,
onTap: () {
Provider.of<ThemeModel>(context).pushRoute(
context, (_) => SettingsScreen(),
fullscreenDialog: true);
},
);
} else {
2019-09-25 15:41:44 +02:00
return ActionButton(
title: 'User Actions',
2019-09-30 09:46:06 +02:00
items: [
2019-09-25 15:41:44 +02:00
if (data != null && data[0]['viewerCanFollow'])
2019-09-30 09:46:06 +02:00
ActionItem(
2019-09-25 15:41:44 +02:00
text: data[0]['viewerIsFollowing'] ? 'Unfollow' : 'Follow',
onPress: () async {
if (data[0]['viewerIsFollowing']) {
2019-09-27 14:52:38 +02:00
await Provider.of<AuthModel>(context)
2019-09-25 15:41:44 +02:00
.deleteWithCredentials('/user/following/$login');
data[0]['viewerIsFollowing'] = false;
} else {
2019-09-27 14:52:38 +02:00
Provider.of<AuthModel>(context)
2019-09-25 15:41:44 +02:00
.putWithCredentials('/user/following/$login');
data[0]['viewerIsFollowing'] = true;
}
},
),
2019-09-30 09:46:06 +02:00
if (data != null) ...[
ActionItem.share(data[0]['url']),
ActionItem.launch(data[0]['url']),
],
2019-09-25 15:41:44 +02:00
],
);
}
},
bodyBuilder: (data) {
var payload = data[0];
var contributions = data[1] as List<ContributionsInfo>;
return Column(
2019-09-05 11:58:14 +02:00
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
2019-09-27 15:02:55 +02:00
UserItem.fromData(payload, inUserScreen: true),
2019-09-14 17:48:01 +02:00
borderView,
2019-09-05 11:58:14 +02:00
Row(children: <Widget>[
EntryItem(
count: payload['repositories']['totalCount'],
text: 'Repositories',
2019-09-23 12:28:33 +02:00
screenBuilder: (context) => RepositoriesScreen(login),
2019-02-04 14:38:29 +01:00
),
2019-09-05 11:58:14 +02:00
EntryItem(
count: payload['starredRepositories']['totalCount'],
text: 'Stars',
screenBuilder: (context) => RepositoriesScreen.stars(login),
),
2019-09-05 11:58:14 +02:00
EntryItem(
count: payload['followers']['totalCount'],
text: 'Followers',
2019-09-23 15:25:18 +02:00
screenBuilder: (context) => UsersScreen.followers(login),
2019-09-05 11:58:14 +02:00
),
EntryItem(
count: payload['following']['totalCount'],
text: 'Following',
2019-09-23 15:25:18 +02:00
screenBuilder: (context) => UsersScreen.following(login),
2019-09-05 11:58:14 +02:00
),
]),
2019-09-28 18:25:14 +02:00
borderView,
2019-09-14 20:05:34 +02:00
borderView1,
2019-09-28 18:25:14 +02:00
borderView,
_buildContributions(contributions),
2019-09-28 18:25:14 +02:00
borderView,
2019-09-14 20:05:34 +02:00
borderView1,
2019-09-15 09:08:09 +02:00
TableView(
hasIcon: true,
items: [
if (isNotNullOrEmpty(payload['company']))
TableViewItem(
leftIconData: Octicons.organization,
2019-09-27 12:23:47 +02:00
text: TextContainsOrganization(payload['company'],
style: TextStyle(
fontSize: 16, color: PrimerColors.gray900),
overflow: TextOverflow.ellipsis),
2019-09-15 09:08:09 +02:00
),
if (isNotNullOrEmpty(payload['location']))
TableViewItem(
leftIconData: Octicons.location,
text: Text(payload['location']),
onTap: () {
2019-09-29 07:32:53 +02:00
launchUrl('https://www.google.com/maps/place/' +
2019-09-15 09:08:09 +02:00
(payload['location'] as String)
.replaceAll(RegExp(r'\s+'), ''));
},
),
if (isNotNullOrEmpty(payload['email']))
TableViewItem(
leftIconData: Octicons.mail,
text: Text(payload['email']),
onTap: () {
2019-09-29 07:32:53 +02:00
launchUrl('mailto:' + payload['email']);
2019-09-15 09:08:09 +02:00
},
),
if (isNotNullOrEmpty(payload['websiteUrl']))
TableViewItem(
leftIconData: Octicons.link,
text: Text(payload['websiteUrl']),
onTap: () {
var url = payload['websiteUrl'] as String;
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-09-14 12:26:32 +02:00
..._buildRepos(payload),
2019-09-28 18:25:14 +02:00
borderView1,
],
);
},
2019-02-03 08:50:17 +01:00
);
2019-01-25 13:35:20 +01:00
}
}