1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-18 11:19:28 +01:00
git-touch-android-ios-app/lib/screens/user.dart

276 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';
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/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-09-21 17:54:25 +02:00
String title;
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) {
2019-09-21 17:54:25 +02:00
title = 'pinned repositories';
2019-09-14 12:26:32 +02:00
items = payload['pinnedItems']['nodes'] as List;
} else if ((payload['repositories']['nodes'] as List).isNotEmpty) {
2019-09-21 17:54:25 +02:00
title = 'popular repositories';
2019-09-14 12:26:32 +02:00
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 20:05:34 +02:00
borderView1,
2019-09-21 17:54:25 +02:00
if (title != null) TableViewHeader(title),
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) {
return RepoItem(item);
}).toList(),
)
];
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(
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-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(
2019-09-15 12:37:38 +02:00
child: Icon(Icons.settings, size: 20),
screenBuilder: (_) => SettingsScreen(),
material: false,
fullscreenDialog: true,
);
} else {
2019-09-21 19:02:14 +02:00
return ActionButton(title: 'User Actions', actions: [
if (payload['viewerCanFollow'])
MyAction(
text: payload['viewerIsFollowing'] ? 'Unfollow' : 'Follow',
onPress: () async {
if (payload['viewerIsFollowing']) {
await Provider.of<SettingsModel>(context)
.deleteWithCredentials('/user/following/$login');
payload['viewerIsFollowing'] = false;
} else {
Provider.of<SettingsModel>(context)
.putWithCredentials('/user/following/$login');
payload['viewerIsFollowing'] = true;
}
},
),
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']);
},
),
]);
}
},
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-14 20:05:34 +02:00
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-21 19:02:14 +02:00
screenBuilder: (context) => ReposScreen(login),
2019-02-04 14:38:29 +01:00
),
2019-09-05 11:58:14 +02:00
EntryItem(
count: payload['starredRepositories']['totalCount'],
text: 'Stars',
2019-09-21 19:02:14 +02:00
screenBuilder: (context) => ReposScreen(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 20:05:34 +02:00
borderView1,
_buildContributions(contributions),
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,
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-14 12:26:32 +02:00
..._buildRepos(payload),
],
);
},
2019-02-03 08:50:17 +01:00
);
2019-01-25 13:35:20 +01:00
}
}