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

336 lines
10 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-04 16:59:33 +02:00
import 'package:git_touch/widgets/table_view.dart';
import 'package:primer/primer.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:share/share.dart';
import 'package:github_contributions/github_contributions.dart';
2019-09-08 14:07:35 +02:00
import 'package:git_touch/models/settings.dart';
import 'package:provider/provider.dart';
import '../scaffolds/refresh.dart';
2019-02-03 08:50:17 +01:00
import '../widgets/avatar.dart';
2019-02-04 14:38:29 +01:00
import '../widgets/entry_item.dart';
import '../widgets/list_group.dart';
import '../widgets/repo_item.dart';
import '../widgets/link.dart';
2019-02-20 09:31:22 +01:00
import '../widgets/action.dart';
2019-02-04 11:32:39 +01:00
import '../screens/repos.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-02-07 07:35:19 +01:00
class UserScreen extends StatefulWidget {
final String login;
final bool showSettings;
2019-02-07 07:35:19 +01:00
UserScreen(this.login, {this.showSettings = false});
2019-02-07 07:35:19 +01:00
_UserScreenState createState() => _UserScreenState();
}
class _UserScreenState extends State<UserScreen> {
2019-03-10 16:34:34 +01:00
Future query() async {
2019-02-07 07:35:19 +01:00
var login = widget.login;
2019-09-08 14:07:35 +02:00
var data = await Provider.of<SettingsModel>(context).query('''
{
user(login: "$login") {
name
avatarUrl
bio
company
location
2019-09-04 16:59:33 +02:00
email
websiteUrl
starredRepositories {
totalCount
}
followers {
totalCount
}
following {
totalCount
}
2019-02-10 12:15:50 +01:00
repositories(first: $pageSize, 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-03 10:35:13 +02:00
pinnedItems(first: $pageSize) {
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
Widget _buildRepos(payload) {
2019-02-04 11:32:39 +01:00
String title;
List items;
2019-09-03 10:35:13 +02:00
if (payload['pinnedItems']['nodes'].length == 0) {
2019-02-04 11:32:39 +01:00
title = 'Popular repositories';
items = payload['repositories']['nodes'];
} else {
title = 'Pinned repositories';
2019-09-03 10:35:13 +02:00
items = payload['pinnedItems']['nodes'];
2019-02-04 11:32:39 +01:00
}
return ListGroup(
2019-03-10 14:26:05 +01:00
title: Text(
title,
style: TextStyle(fontSize: 16),
),
items: items,
itemBuilder: (item, _) {
2019-09-09 16:50:22 +02:00
return RepoItem(item);
2019-03-10 14:26:05 +01:00
},
);
2019-02-04 11:32:39 +01:00
}
2019-09-04 16:59:33 +02:00
TableViewItem _buildTableViewItem({
String placeholder,
IconData iconData,
String text,
Function onTap,
}) {
var leftWidget = Icon(iconData, size: 20, color: PrimerColors.blue500);
var usePlaceholder = text == null || text.isEmpty;
var itemText = usePlaceholder
2019-09-08 16:17:29 +02:00
? Text(placeholder, style: TextStyle(color: PrimerColors.gray300))
: Text(text);
2019-09-04 16:59:33 +02:00
return TableViewItem(
leftWidget: leftWidget,
text: itemText,
2019-09-07 10:45:04 +02:00
rightWidget: onTap == null
? null
: Icon(CupertinoIcons.right_chevron,
size: 18, color: PrimerColors.gray300),
2019-09-05 11:58:14 +02:00
onTap: onTap,
2019-09-04 16:59:33 +02:00
);
}
Widget _buildContributions(List<ContributionsInfo> contributions) {
var 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(
padding: EdgeInsets.all(10),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: row,
),
);
}
2019-01-25 13:35:20 +01:00
@override
Widget build(BuildContext context) {
2019-02-03 08:50:17 +01:00
return RefreshScaffold(
onRefresh: () {
return Future.wait(
[query(), getContributions(widget.login)],
);
},
title: Text(widget.login),
trailingBuilder: (data) {
var payload = data[0];
if (widget.showSettings) {
return Link(
child: Icon(Icons.settings, size: 24),
screenBuilder: (_) => SettingsScreen(),
material: false,
fullscreenDialog: true,
);
} else {
2019-06-20 16:59:13 +02:00
List<MyAction> actions = [];
2019-02-20 09:31:22 +01:00
if (payload['viewerCanFollow']) {
2019-06-20 16:59:13 +02:00
actions.add(MyAction(
2019-02-20 09:31:22 +01:00
text: payload['viewerIsFollowing'] ? 'Unfollow' : 'Follow',
onPress: () async {
if (payload['viewerIsFollowing']) {
2019-09-08 14:07:35 +02:00
await Provider.of<SettingsModel>(context)
2019-02-20 09:31:22 +01:00
.deleteWithCredentials('/user/following/${widget.login}');
payload['viewerIsFollowing'] = false;
} else {
2019-09-08 14:07:35 +02:00
Provider.of<SettingsModel>(context)
2019-02-20 09:31:22 +01:00
.putWithCredentials('/user/following/${widget.login}');
payload['viewerIsFollowing'] = true;
}
},
));
}
actions.addAll([
2019-06-20 16:59:13 +02:00
MyAction(
2019-02-20 09:31:22 +01:00
text: 'Share',
onPress: () {
Share.share(payload['url']);
},
),
2019-06-20 16:59:13 +02:00
MyAction(
2019-02-20 09:31:22 +01:00
text: 'Open in Browser',
onPress: () {
launch(payload['url']);
},
),
]);
return ActionButton(title: 'User Actions', actions: actions);
}
},
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>[
Container(
2019-09-05 11:58:14 +02:00
padding: EdgeInsets.all(12),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
2019-09-05 11:58:14 +02:00
Avatar(url: payload['avatarUrl'], size: 30),
2019-08-31 16:17:35 +02:00
SizedBox(width: 10),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
2019-09-04 16:59:33 +02:00
Row(children: <Widget>[
Text(
payload['name'] ?? widget.login,
2019-09-05 11:58:14 +02:00
style: TextStyle(
color: PrimerColors.blue500, fontSize: 16),
2019-09-04 16:59:33 +02:00
),
Text(
'(${widget.login})',
2019-09-05 11:58:14 +02:00
style: TextStyle(
color: PrimerColors.gray500, fontSize: 16),
2019-09-04 16:59:33 +02:00
),
]),
2019-09-05 11:58:14 +02:00
SizedBox(height: 4),
2019-02-10 12:45:08 +01:00
Text(
2019-09-04 16:59:33 +02:00
payload['bio'] == null ||
(payload['bio'] as String).isEmpty
? 'No bio'
: payload['bio'],
2019-09-05 11:58:14 +02:00
style: TextStyle(
color: PrimerColors.gray500, fontSize: 15),
2019-02-10 12:45:08 +01:00
),
],
),
)
],
),
2019-02-03 08:50:17 +01:00
),
2019-09-05 11:58:14 +02:00
BorderView(),
Row(children: <Widget>[
EntryItem(
count: payload['repositories']['totalCount'],
text: 'Repositories',
screenBuilder: (context) => ReposScreen(login: widget.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) =>
ReposScreen(login: widget.login, star: true),
),
2019-09-05 11:58:14 +02:00
EntryItem(
count: payload['followers']['totalCount'],
text: 'Followers',
screenBuilder: (context) => UsersScreen(login: widget.login),
),
EntryItem(
count: payload['following']['totalCount'],
text: 'Following',
screenBuilder: (context) =>
UsersScreen(login: widget.login, following: true),
),
]),
BorderView(height: 10),
_buildContributions(contributions),
2019-09-05 11:58:14 +02:00
BorderView(height: 10),
2019-09-04 16:59:33 +02:00
TableView(items: [
_buildTableViewItem(
2019-09-05 11:58:14 +02:00
iconData: Octicons.organization,
placeholder: 'Company',
text: payload['company'],
),
2019-09-04 16:59:33 +02:00
_buildTableViewItem(
2019-09-07 10:45:04 +02:00
iconData: Octicons.location,
placeholder: 'Location',
text: payload['location'],
onTap: payload['location'] == null
? null
: () {
launch('https://www.google.com/maps/place/' +
(payload['location'] as String)
.replaceAll(RegExp(r'\s+'), ''));
}),
2019-09-04 16:59:33 +02:00
_buildTableViewItem(
2019-09-05 11:58:14 +02:00
iconData: Octicons.mail,
placeholder: 'Email',
text: payload['email'],
onTap: (payload['email'] as String).isEmpty
? null
: () {
launch('mailto:' + payload['email']);
},
),
2019-09-04 16:59:33 +02:00
_buildTableViewItem(
2019-09-05 11:58:14 +02:00
iconData: Octicons.link,
placeholder: 'Website',
text: payload['websiteUrl'],
onTap: payload['websiteUrl'] == null
? null
: () {
var url = payload['websiteUrl'] as String;
if (!url.startsWith('http')) {
url = 'http://$url';
}
launch(url);
},
),
2019-09-04 16:59:33 +02:00
]),
2019-09-05 11:58:14 +02:00
// _buildRepos(payload),
],
);
},
2019-02-03 08:50:17 +01:00
);
2019-01-25 13:35:20 +01:00
}
}