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

251 lines
6.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';
import 'package:url_launcher/url_launcher.dart';
2019-02-07 07:35:19 +01:00
import '../providers/settings.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-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-04 11:32:39 +01:00
var repoChunk = '''
owner {
login
}
name
description
isPrivate
isFork
stargazers {
totalCount
}
forks {
totalCount
}
primaryLanguage {
color
name
}
''';
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> {
bool loading = true;
2019-02-07 07:35:19 +01:00
Map<String, dynamic> payload = {};
@override
void initState() {
super.initState();
nextTick(_refresh);
2019-02-07 07:35:19 +01:00
}
Future queryUser(BuildContext context) async {
var login = widget.login;
var data = await SettingsProvider.of(context).query('''
{
user(login: "$login") {
name
avatarUrl
bio
email
company
location
starredRepositories {
totalCount
}
followers {
totalCount
}
following {
totalCount
}
2019-02-03 08:50:17 +01:00
repositories(first: 6, ownerAffiliations: OWNER, orderBy: {field: STARGAZERS, direction: DESC}) {
totalCount
nodes {
2019-02-04 11:32:39 +01:00
$repoChunk
2019-02-03 08:50:17 +01:00
}
}
pinnedRepositories(first: 6) {
nodes {
2019-02-04 11:32:39 +01:00
$repoChunk
2019-02-03 08:50:17 +01:00
}
}
}
}
''');
2019-02-07 07:35:19 +01:00
return data['user'];
}
2019-02-04 11:32:39 +01:00
Widget _buildRepos() {
String title;
List items;
if (payload['pinnedRepositories']['nodes'].length == 0) {
title = 'Popular repositories';
items = payload['repositories']['nodes'];
} else {
title = 'Pinned repositories';
items = payload['pinnedRepositories']['nodes'];
}
return ListGroup(
title: Text(title),
items: items,
2019-02-06 06:06:11 +01:00
itemBuilder: (item, _) => RepoItem(item),
2019-02-04 11:32:39 +01:00
);
}
Future<void> _refresh() async {
setState(() {
loading = true;
});
2019-02-07 07:35:19 +01:00
var _payload = await queryUser(context);
if (mounted) {
setState(() {
loading = false;
payload = _payload;
});
}
}
Widget _buildEmail() {
// TODO: redesign the UI to show all information
String email = payload['email'] ?? '';
if (email.isNotEmpty) {
return Link(
child: Row(children: <Widget>[
Icon(
Octicons.mail,
color: Colors.black54,
size: 16,
),
Padding(padding: EdgeInsets.only(left: 4)),
Text(email, style: TextStyle(color: Colors.black54, fontSize: 16))
]),
beforeRedirect: () {
launch('mailto:' + email);
},
);
}
String company = payload['company'] ?? '';
if (company.isNotEmpty) {
return Row(children: <Widget>[
Icon(
Octicons.organization,
color: Colors.black54,
size: 16,
),
Padding(padding: EdgeInsets.only(left: 4)),
Text(company, style: TextStyle(color: Colors.black54, fontSize: 16))
]);
}
String location = payload['location'] ?? '';
if (location.isNotEmpty) {
return Row(children: <Widget>[
Icon(
Octicons.location,
color: Colors.black54,
size: 16,
),
Padding(padding: EdgeInsets.only(left: 4)),
Text(location, style: TextStyle(color: Colors.black54, fontSize: 16))
]);
}
return Container();
}
2019-01-25 13:35:20 +01:00
@override
Widget build(BuildContext context) {
2019-02-03 08:50:17 +01:00
return RefreshScaffold(
onRefresh: _refresh,
title: Text(widget.login),
trailing: Link(
child: Icon(Icons.settings, size: 24),
screenBuilder: (_) => SettingsScreen(),
material: false,
fullscreenDialog: true,
),
loading: loading,
bodyBuilder: () {
return Column(
children: <Widget>[
Container(
padding: EdgeInsets.all(10),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Avatar(
login: widget.login,
url: payload['avatarUrl'],
size: 28,
2019-02-03 08:50:17 +01:00
),
Padding(padding: EdgeInsets.only(left: 10)),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
2019-02-04 11:32:39 +01:00
Text(payload['name'] ?? widget.login,
style: TextStyle(height: 1.2)),
Padding(padding: EdgeInsets.only(top: 10)),
_buildEmail(),
],
),
)
],
),
2019-02-03 08:50:17 +01:00
),
Container(
decoration: BoxDecoration(
2019-02-04 14:38:29 +01:00
border: Border(
bottom: BorderSide(color: Colors.black12),
top: BorderSide(color: Colors.black12),
),
),
child: Row(
children: <Widget>[
2019-02-04 14:38:29 +01:00
EntryItem(
count: payload['repositories']['totalCount'],
text: 'Repositories',
screenBuilder: (context) => ReposScreen(),
2019-02-04 14:38:29 +01:00
),
EntryItem(
count: payload['starredRepositories']['totalCount'],
text: 'Stars',
screenBuilder: (context) => ReposScreen(),
2019-02-04 14:38:29 +01:00
),
EntryItem(
count: payload['followers']['totalCount'],
text: 'Followers',
screenBuilder: (context) => UsersScreen(),
2019-02-04 14:38:29 +01:00
),
EntryItem(
count: payload['following']['totalCount'],
text: 'Following',
screenBuilder: (context) => UsersScreen(),
2019-02-04 14:38:29 +01:00
),
],
),
2019-02-03 08:50:17 +01:00
),
2019-02-04 11:32:39 +01:00
_buildRepos(),
],
);
},
2019-02-03 08:50:17 +01:00
);
2019-01-25 13:35:20 +01:00
}
}