1
0
mirror of https://github.com/git-touch/git-touch synced 2025-01-31 16:14:49 +01:00

288 lines
8.6 KiB
Dart
Raw Normal View History

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