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

281 lines
8.1 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-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-14 11:19:33 +02:00
import 'package:git_touch/widgets/user_item.dart';
2019-09-04 16:59:33 +02:00
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-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-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-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-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-02-04 11:32:39 +01:00
List items;
2019-09-14 12:26:32 +02:00
if ((payload['pinnedItems']['nodes'] as List).isNotEmpty) {
items = payload['pinnedItems']['nodes'] as List;
} else if ((payload['repositories']['nodes'] as List).isNotEmpty) {
items = payload['repositories']['nodes'] as List;
2019-02-04 11:32:39 +01:00
} else {
2019-09-14 12:26:32 +02:00
items = [];
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 17:48:01 +02:00
borderView10,
2019-09-14 12:26:32 +02:00
// Text('Pinned repositories'),
...join(
2019-09-14 17:48:01 +02:00
borderView,
2019-09-14 12:26:32 +02:00
items.map((item) {
return RepoItem(item);
}).toList(),
)
];
2019-02-04 11:32:39 +01: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(
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) {
var payload = data[0];
2019-09-13 11:00:45 +02:00
if (isMe) {
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-09-13 10:55:27 +02:00
.deleteWithCredentials('/user/following/$login');
2019-02-20 09:31:22 +01:00
payload['viewerIsFollowing'] = false;
} else {
2019-09-08 14:07:35 +02:00
Provider.of<SettingsModel>(context)
2019-09-13 10:55:27 +02:00
.putWithCredentials('/user/following/$login');
2019-02-20 09:31:22 +01:00
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),
2019-09-14 11:19:33 +02:00
child: UserItem(
login,
name: payload['name'],
avatarUrl: payload['avatarUrl'],
bio: payload['bio'],
),
2019-02-03 08:50:17 +01:00
),
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-13 10:55:27 +02:00
screenBuilder: (context) => ReposScreen(login: 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) =>
2019-09-13 10:55:27 +02:00
ReposScreen(login: login, star: true),
),
2019-09-05 11:58:14 +02:00
EntryItem(
count: payload['followers']['totalCount'],
text: 'Followers',
2019-09-13 10:55:27 +02:00
screenBuilder: (context) => UsersScreen(
login: login, type: UsersScreenType.userFollowers),
2019-09-05 11:58:14 +02:00
),
EntryItem(
count: payload['following']['totalCount'],
text: 'Following',
2019-09-13 10:55:27 +02:00
screenBuilder: (context) => UsersScreen(
login: login, type: UsersScreenType.userFollowing),
2019-09-05 11:58:14 +02:00
),
]),
2019-09-14 17:48:01 +02:00
borderView10,
_buildContributions(contributions),
2019-09-14 17:48:01 +02:00
borderView10,
2019-09-04 16:59:33 +02:00
TableView(items: [
2019-09-14 17:48:01 +02:00
if (isNotNullOrEmpty(payload['company']))
TableViewItem(
leftIconData: Octicons.organization,
text: Text(payload['company']),
),
if (isNotNullOrEmpty(payload['location']))
TableViewItem(
leftIconData: Octicons.location,
text: Text(payload['location']),
onTap: () {
launch('https://www.google.com/maps/place/' +
(payload['location'] as String)
.replaceAll(RegExp(r'\s+'), ''));
},
),
if (isNotNullOrEmpty(payload['email']))
TableViewItem(
leftIconData: Octicons.mail,
text: Text(payload['email']),
onTap: () {
launch('mailto:' + payload['email']);
},
),
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';
}
launch(url);
},
),
2019-09-04 16:59:33 +02:00
]),
2019-09-14 12:26:32 +02:00
..._buildRepos(payload),
],
);
},
2019-02-03 08:50:17 +01:00
);
2019-01-25 13:35:20 +01:00
}
}