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

344 lines
12 KiB
Dart
Raw Normal View History

2019-09-23 13:45:47 +02:00
import 'dart:convert';
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
2019-02-04 14:38:29 +01:00
import 'package:flutter/cupertino.dart';
2019-09-27 14:52:38 +02:00
import 'package:git_touch/models/auth.dart';
2019-09-25 11:06:36 +02:00
import 'package:git_touch/scaffolds/refresh_stateful.dart';
2019-09-30 13:55:27 +02:00
import 'package:git_touch/screens/repositories.dart';
2019-09-09 16:50:22 +02:00
import 'package:git_touch/screens/users.dart';
2019-09-08 16:17:29 +02:00
import 'package:git_touch/utils/utils.dart';
2019-09-11 13:59:47 +02:00
import 'package:git_touch/widgets/app_bar_title.dart';
import 'package:git_touch/widgets/markdown_view.dart';
2019-09-08 16:17:29 +02:00
import 'package:git_touch/widgets/table_view.dart';
2019-09-08 14:07:35 +02:00
import 'package:provider/provider.dart';
import 'package:git_touch/models/theme.dart';
2019-08-30 10:26:56 +02:00
import 'package:git_touch/screens/commits.dart';
2019-08-30 08:08:09 +02:00
import 'package:git_touch/screens/object.dart';
2019-09-23 12:28:33 +02:00
import 'package:git_touch/widgets/repository_item.dart';
2019-02-04 14:38:29 +01:00
import '../widgets/entry_item.dart';
import '../screens/issues.dart';
2019-09-30 10:31:07 +02:00
import 'package:git_touch/widgets/action_button.dart';
2019-02-04 14:38:29 +01:00
2019-09-23 12:28:33 +02:00
class RepositoryScreen extends StatelessWidget {
2019-02-07 07:35:19 +01:00
final String owner;
final String name;
2019-09-23 11:08:51 +02:00
final String branch;
2019-02-07 07:35:19 +01:00
2019-09-23 12:28:33 +02:00
RepositoryScreen(this.owner, this.name, {this.branch});
2019-09-23 14:04:53 +02:00
get branchInfoKey => getBranchQueryKey(branch);
2019-09-23 11:08:51 +02:00
2019-02-07 07:35:19 +01:00
Future queryRepo(BuildContext context) async {
2019-09-23 14:04:53 +02:00
var branchKey = getBranchQueryKey(branch, withParams: true);
2019-09-27 14:52:38 +02:00
var data = await Provider.of<AuthModel>(context).query('''
2019-02-04 14:38:29 +01:00
{
repository(owner: "$owner", name: "$name") {
2019-10-03 04:12:22 +02:00
$repoChunk
id
2019-09-22 05:34:25 +02:00
diskUsage
2019-09-23 10:01:55 +02:00
hasIssuesEnabled
2019-09-23 11:08:51 +02:00
url
viewerHasStarred
viewerSubscription
2019-09-26 20:09:02 +02:00
projectsResourcePath
2019-09-09 16:50:22 +02:00
watchers {
totalCount
}
2019-02-04 14:38:29 +01:00
issues(states: OPEN) {
totalCount
}
pullRequests(states: OPEN) {
totalCount
}
2019-09-26 20:09:02 +02:00
projects {
totalCount
}
2019-09-23 18:34:51 +02:00
releases {
totalCount
}
2019-09-08 16:17:29 +02:00
languages(first: 10, orderBy: {field: SIZE, direction: DESC}) {
totalSize
edges {
size
node {
name
color
}
}
}
2019-09-23 14:04:53 +02:00
$branchKey {
name
2019-08-30 10:26:56 +02:00
target {
... on Commit {
history {
totalCount
}
}
}
}
2019-09-29 09:35:33 +02:00
refs(first: 100, refPrefix: "refs/heads/") {
2019-09-23 11:08:51 +02:00
totalCount
nodes {
name
}
}
licenseInfo {
name
2019-09-13 08:54:18 +02:00
spdxId
}
2019-09-26 20:04:27 +02:00
repositoryTopics(first: 100) {
nodes {
url
topic {
name
}
}
}
2019-02-04 14:38:29 +01:00
}
}
''');
2019-02-07 07:35:19 +01:00
return data['repository'];
}
2019-09-23 13:45:47 +02:00
Future<String> fetchReadme(BuildContext context) async {
2019-09-27 14:52:38 +02:00
var data = await Provider.of<AuthModel>(context)
2019-09-23 13:45:47 +02:00
.getWithCredentials('/repos/$owner/$name/readme');
if (data['content'] == null) {
return null;
}
var bits = base64.decode((data['content'] as String).replaceAll('\n', ''));
var str = utf8.decode(bits);
return str;
}
2019-01-25 13:35:20 +01:00
@override
Widget build(BuildContext context) {
2019-09-25 11:06:36 +02:00
return RefreshStatefulScaffold(
2019-09-11 13:59:47 +02:00
title: AppBarTitle('Repository'),
2019-09-30 11:13:12 +02:00
fetchData: () => Future.wait([
2019-09-23 14:04:53 +02:00
queryRepo(context),
fetchReadme(context),
]),
2019-11-02 13:54:23 +01:00
actionBuilder: (data, setState) {
2019-09-25 15:41:44 +02:00
return ActionButton(
title: 'Repository Actions',
2019-09-30 09:46:06 +02:00
items: [
2019-09-25 16:01:00 +02:00
if (data != null) ...[
2019-09-30 09:46:06 +02:00
ActionItem(
2019-09-25 15:41:44 +02:00
text: data[0]['viewerHasStarred'] ? 'Unstar' : 'Star',
2019-11-02 16:36:49 +01:00
onPress: (_) async {
2019-09-25 15:41:44 +02:00
if (data[0]['viewerHasStarred']) {
2019-09-27 14:52:38 +02:00
await Provider.of<AuthModel>(context)
2019-09-25 15:41:44 +02:00
.deleteWithCredentials('/user/starred/$owner/$name');
data[0]['viewerHasStarred'] = false;
} else {
2019-10-03 06:36:03 +02:00
await Provider.of<AuthModel>(context)
2019-09-25 15:41:44 +02:00
.putWithCredentials('/user/starred/$owner/$name');
data[0]['viewerHasStarred'] = true;
}
2019-11-02 13:54:23 +01:00
setState(() {});
2019-09-25 15:41:44 +02:00
},
),
2019-09-30 09:46:06 +02:00
ActionItem(
2019-09-25 16:01:00 +02:00
text: data[0]['viewerSubscription'] == 'SUBSCRIBED'
? 'Unwatch'
: 'Watch',
2019-11-02 16:36:49 +01:00
onPress: (_) async {
2019-09-25 16:01:00 +02:00
if (data[0]['viewerSubscription'] == 'SUBSCRIBED') {
2019-09-27 14:52:38 +02:00
await Provider.of<AuthModel>(context).deleteWithCredentials(
'/repos/$owner/$name/subscription');
2019-09-25 16:01:00 +02:00
data[0]['viewerSubscription'] = 'UNSUBSCRIBED';
} else {
2019-10-03 06:36:03 +02:00
await Provider.of<AuthModel>(context)
2019-09-25 16:01:00 +02:00
.putWithCredentials('/repos/$owner/$name/subscription');
data[0]['viewerSubscription'] = 'SUBSCRIBED';
}
2019-11-02 13:54:23 +01:00
setState(() {});
2019-09-25 16:01:00 +02:00
},
),
],
2019-09-30 09:46:06 +02:00
if (data != null) ...[
ActionItem.share(data[0]['url']),
ActionItem.launch(data[0]['url']),
],
2019-09-25 15:41:44 +02:00
],
);
},
2019-11-02 13:54:23 +01:00
bodyBuilder: (data, _) {
var repo = data[0];
var readme = data[1] as String;
2019-09-23 13:45:47 +02:00
2019-09-14 18:34:10 +02:00
final langWidth = MediaQuery.of(context).size.width -
2019-10-02 10:09:54 +02:00
CommonStyle.padding.left -
CommonStyle.padding.right -
2019-11-02 13:54:23 +01:00
(repo['languages']['edges'] as List).length +
2019-09-14 18:34:10 +02:00
1;
2019-09-08 16:17:29 +02:00
2019-02-04 14:38:29 +01:00
return Column(
2019-09-08 16:17:29 +02:00
crossAxisAlignment: CrossAxisAlignment.stretch,
2019-02-04 14:38:29 +01:00
children: <Widget>[
2019-11-02 13:54:23 +01:00
RepositoryItem(repo, inRepoScreen: true),
2019-10-02 10:09:54 +02:00
CommonStyle.border,
2019-09-08 16:17:29 +02:00
Row(
children: <Widget>[
EntryItem(
2019-11-02 13:54:23 +01:00
count: repo['watchers']['totalCount'],
2019-09-09 16:50:22 +02:00
text: 'Watchers',
2019-09-23 15:25:18 +02:00
screenBuilder: (context) => UsersScreen.watchers(owner, name),
2019-09-08 16:17:29 +02:00
),
EntryItem(
2019-11-02 13:54:23 +01:00
count: repo['stargazers']['totalCount'],
2019-09-09 16:50:22 +02:00
text: 'Stars',
2019-09-23 15:25:18 +02:00
screenBuilder: (context) => UsersScreen.stars(owner, name),
2019-09-08 16:17:29 +02:00
),
EntryItem(
2019-11-02 13:54:23 +01:00
count: repo['forks']['totalCount'],
2019-09-09 16:50:22 +02:00
text: 'Forks',
2019-09-30 13:55:27 +02:00
screenBuilder: (context) =>
RepositoriesScreen.forks(owner, name),
2019-09-08 16:17:29 +02:00
),
],
),
2019-10-02 10:09:54 +02:00
CommonStyle.verticalGap,
2019-11-02 13:54:23 +01:00
if ((repo['languages']['edges'] as List).isNotEmpty)
2019-09-23 10:10:46 +02:00
Container(
2019-10-02 08:58:11 +02:00
color: Colors.white,
2019-10-02 10:09:54 +02:00
padding: CommonStyle.padding.copyWith(top: 8, bottom: 8),
2019-09-23 10:10:46 +02:00
child: ClipRRect(
borderRadius: BorderRadius.circular(2),
child: SizedBox(
height: 10,
child: Row(
children: join(
SizedBox(width: 1),
2019-11-02 13:54:23 +01:00
(repo['languages']['edges'] as List)
2019-09-23 10:10:46 +02:00
.map((lang) => Container(
color: convertColor(lang['node']['color']),
width: langWidth *
lang['size'] /
2019-11-02 13:54:23 +01:00
repo['languages']['totalSize']))
2019-09-23 10:10:46 +02:00
.toList())),
),
2019-02-04 14:38:29 +01:00
),
),
2019-09-15 09:08:09 +02:00
TableView(
hasIcon: true,
items: [
2019-11-02 13:54:23 +01:00
if (repo[branchInfoKey] != null)
TableViewItem(
leftIconData: Octicons.code,
text: Text('Code'),
rightWidget:
2019-11-02 13:54:23 +01:00
Text(filesize((repo['diskUsage'] as int) * 1000)),
screenBuilder: (_) => ObjectScreen(
2019-11-02 17:02:41 +01:00
owner,
name,
repo[branchInfoKey]['name'] as String,
2019-09-23 14:04:53 +02:00
),
2019-09-15 09:08:09 +02:00
),
2019-11-02 13:54:23 +01:00
if (repo['hasIssuesEnabled'] as bool)
2019-09-23 10:01:55 +02:00
TableViewItem(
leftIconData: Octicons.issue_opened,
text: Text('Issues'),
2019-09-30 11:13:12 +02:00
rightWidget:
2019-11-02 13:54:23 +01:00
Text(numberFormat.format(repo['issues']['totalCount'])),
2019-11-02 17:02:41 +01:00
screenBuilder: (_) => IssuesScreen(owner, name),
2019-09-23 10:01:55 +02:00
),
2019-09-15 09:08:09 +02:00
TableViewItem(
leftIconData: Octicons.git_pull_request,
text: Text('Pull requests'),
2019-09-30 11:13:12 +02:00
rightWidget: Text(
2019-11-02 13:54:23 +01:00
numberFormat.format(repo['pullRequests']['totalCount'])),
2019-11-02 17:02:41 +01:00
screenBuilder: (_) =>
IssuesScreen(owner, name, isPullRequest: true),
2019-09-15 09:08:09 +02:00
),
2019-09-26 20:09:02 +02:00
TableViewItem(
leftIconData: Octicons.project,
text: Text('Projects'),
2019-09-30 11:13:12 +02:00
rightWidget:
2019-11-02 13:54:23 +01:00
Text(numberFormat.format(repo['projects']['totalCount'])),
url: 'https://github.com' + repo['projectsResourcePath'],
2019-09-26 20:09:02 +02:00
),
2019-09-15 09:08:09 +02:00
],
),
2019-10-02 10:09:54 +02:00
CommonStyle.verticalGap,
2019-09-15 09:08:09 +02:00
TableView(
hasIcon: true,
items: [
2019-11-02 13:54:23 +01:00
if (repo[branchInfoKey] != null) ...[
TableViewItem(
leftIconData: Octicons.history,
text: Text('Commits'),
2019-11-02 13:54:23 +01:00
rightWidget: Text(numberFormat.format(repo[branchInfoKey]
2019-09-23 11:08:51 +02:00
['target']['history']['totalCount'])),
screenBuilder: (_) =>
CommitsScreen(owner, name, branch: branch),
),
2019-11-02 13:54:23 +01:00
if (repo['refs'] != null)
2019-09-23 13:51:49 +02:00
TableViewItem(
leftIconData: Octicons.git_branch,
text: Text('Branches'),
2019-11-02 13:54:23 +01:00
rightWidget: Text(repo[branchInfoKey]['name'] +
2019-09-23 13:51:49 +02:00
'' +
2019-11-02 13:54:23 +01:00
numberFormat.format(repo['refs']['totalCount'])),
2019-09-23 13:51:49 +02:00
onTap: () async {
2019-11-02 13:54:23 +01:00
var refs = repo['refs']['nodes'] as List;
if (refs.length < 2) return;
2019-09-29 09:35:33 +02:00
await Provider.of<ThemeModel>(context).showPicker(
context,
PickerGroupItem(
2019-11-02 13:54:23 +01:00
value: repo[branchInfoKey]['name'],
2019-09-29 09:35:33 +02:00
items: refs
.map((b) => PickerItem(b['name'] as String,
text: (b['name'] as String)))
.toList(),
onClose: (result) {
2019-10-06 09:52:41 +02:00
if (result != branch) {
Provider.of<ThemeModel>(context)
.pushReplacementRoute(
context,
(_) => RepositoryScreen(owner, name,
branch: result),
);
}
2019-09-29 09:35:33 +02:00
},
),
);
2019-09-23 13:51:49 +02:00
},
),
],
2019-09-27 12:23:47 +02:00
TableViewItem(
leftIconData: Octicons.tag,
text: Text('Releases'),
2019-09-30 11:13:12 +02:00
rightWidget:
2019-11-02 13:54:23 +01:00
Text((repo['releases']['totalCount'] as int).toString()),
url: repo['url'] + '/releases',
2019-09-27 12:23:47 +02:00
),
2019-09-15 09:08:09 +02:00
TableViewItem(
leftIconData: Octicons.law,
text: Text('License'),
2019-11-02 13:54:23 +01:00
rightWidget: Text(repo['licenseInfo'] == null
2019-09-15 09:08:09 +02:00
? 'Unknown'
2019-11-02 13:54:23 +01:00
: (repo['licenseInfo']['spdxId'] ??
repo['licenseInfo']['name'])),
2019-09-15 09:08:09 +02:00
),
],
),
2019-10-02 10:09:54 +02:00
CommonStyle.verticalGap,
2019-09-23 13:45:47 +02:00
if (readme != null)
Container(
2019-10-02 10:09:54 +02:00
padding: CommonStyle.padding,
2019-10-02 08:58:11 +02:00
color: Colors.white,
2019-09-23 13:45:47 +02:00
child: MarkdownView(readme),
),
2019-10-02 10:09:54 +02:00
CommonStyle.verticalGap,
2019-02-04 14:38:29 +01:00
],
);
},
2019-01-25 13:35:20 +01:00
);
}
}