2020-04-06 11:18:35 +08:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2019-09-12 21:19:17 +08:00
|
|
|
import 'package:filesize/filesize.dart';
|
2019-01-28 00:37:44 +08:00
|
|
|
import 'package:flutter/material.dart';
|
2019-02-04 21:38:29 +08:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2020-01-07 15:07:57 +08:00
|
|
|
import 'package:git_touch/graphql/gh.dart';
|
2019-09-27 20:52:38 +08:00
|
|
|
import 'package:git_touch/models/auth.dart';
|
2019-09-25 17:06:36 +08:00
|
|
|
import 'package:git_touch/scaffolds/refresh_stateful.dart';
|
2019-09-08 22:17:29 +08:00
|
|
|
import 'package:git_touch/utils/utils.dart';
|
2019-09-11 19:59:47 +08:00
|
|
|
import 'package:git_touch/widgets/app_bar_title.dart';
|
2020-01-20 14:18:45 +08:00
|
|
|
import 'package:git_touch/widgets/entry_item.dart';
|
2020-01-14 13:51:53 +08:00
|
|
|
import 'package:git_touch/widgets/label.dart';
|
2020-02-08 14:04:02 +08:00
|
|
|
import 'package:git_touch/widgets/language_bar.dart';
|
2020-01-12 14:49:46 +08:00
|
|
|
import 'package:git_touch/widgets/mutation_button.dart';
|
2019-09-13 16:27:33 +08:00
|
|
|
import 'package:git_touch/widgets/markdown_view.dart';
|
2020-01-29 18:23:51 +08:00
|
|
|
import 'package:git_touch/widgets/repo_header.dart';
|
2019-09-08 22:17:29 +08:00
|
|
|
import 'package:git_touch/widgets/table_view.dart';
|
2020-02-08 11:49:31 +08:00
|
|
|
import 'package:github/github.dart';
|
2019-09-08 20:07:35 +08:00
|
|
|
import 'package:provider/provider.dart';
|
2019-09-02 21:52:32 +08:00
|
|
|
import 'package:git_touch/models/theme.dart';
|
2019-12-07 16:12:18 +08:00
|
|
|
import 'package:tuple/tuple.dart';
|
2019-09-30 16:31:07 +08:00
|
|
|
import 'package:git_touch/widgets/action_button.dart';
|
2019-02-04 21:38:29 +08:00
|
|
|
|
2020-02-07 14:17:05 +08:00
|
|
|
class GhRepoScreen extends StatelessWidget {
|
2019-02-07 14:35:19 +08:00
|
|
|
final String owner;
|
|
|
|
final String name;
|
2019-09-23 17:08:51 +08:00
|
|
|
final String branch;
|
2020-02-07 14:17:05 +08:00
|
|
|
GhRepoScreen(this.owner, this.name, {this.branch});
|
2019-02-10 18:50:40 +08:00
|
|
|
|
2020-01-07 15:07:57 +08:00
|
|
|
Future<GhRepoRepository> _query(BuildContext context) async {
|
2019-12-07 16:12:18 +08:00
|
|
|
var res = await Provider.of<AuthModel>(context).gqlClient.execute(
|
2020-01-07 15:07:57 +08:00
|
|
|
GhRepoQuery(
|
|
|
|
variables: GhRepoArguments(
|
2019-12-07 16:12:18 +08:00
|
|
|
owner: owner,
|
|
|
|
name: name,
|
|
|
|
branchSpecified: branch != null,
|
|
|
|
branch: branch ?? '')));
|
|
|
|
return res.data.repository;
|
2019-02-07 14:35:19 +08:00
|
|
|
}
|
2019-02-06 22:28:04 +08:00
|
|
|
|
2020-02-06 13:53:43 +08:00
|
|
|
String _buildWatchState(GhRepoSubscriptionState state) {
|
|
|
|
switch (state) {
|
|
|
|
case GhRepoSubscriptionState.IGNORED:
|
|
|
|
return 'Ignoring';
|
|
|
|
case GhRepoSubscriptionState.SUBSCRIBED:
|
|
|
|
return 'Watching';
|
|
|
|
case GhRepoSubscriptionState.UNSUBSCRIBED:
|
|
|
|
return 'Not watching';
|
|
|
|
default:
|
|
|
|
return 'Unknown';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 16:01:32 +05:30
|
|
|
Future<String> _fetchContributors(BuildContext context) async {
|
|
|
|
final auth = Provider.of<AuthModel>(context);
|
|
|
|
final res = await auth.ghClient.getJSON(
|
|
|
|
'/repos/$owner/$name/stats/contributors',
|
|
|
|
);
|
|
|
|
return res.length.toString();
|
|
|
|
}
|
|
|
|
|
2020-02-08 11:49:31 +08:00
|
|
|
Future<String> _fetchReadme(BuildContext context) async {
|
|
|
|
try {
|
|
|
|
final auth = Provider.of<AuthModel>(context);
|
|
|
|
final res = await auth.ghClient.repositories
|
|
|
|
.getReadme(RepositorySlug(owner, name));
|
|
|
|
return res.text;
|
|
|
|
} catch (e) {
|
|
|
|
// 404
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 20:35:20 +08:00
|
|
|
@override
|
2019-01-28 00:37:44 +08:00
|
|
|
Widget build(BuildContext context) {
|
2020-02-08 11:49:31 +08:00
|
|
|
final theme = Provider.of<ThemeModel>(context);
|
|
|
|
final auth = Provider.of<AuthModel>(context);
|
2020-04-25 16:01:32 +05:30
|
|
|
return RefreshStatefulScaffold<Tuple3<GhRepoRepository, String, String>>(
|
2019-09-11 19:59:47 +08:00
|
|
|
title: AppBarTitle('Repository'),
|
2019-12-07 16:12:18 +08:00
|
|
|
fetchData: () async {
|
|
|
|
final rs = await Future.wait([
|
|
|
|
_query(context),
|
2020-02-08 11:49:31 +08:00
|
|
|
_fetchReadme(context),
|
2020-04-25 16:01:32 +05:30
|
|
|
_fetchContributors(context),
|
2019-12-07 16:12:18 +08:00
|
|
|
]);
|
2020-04-25 16:01:32 +05:30
|
|
|
|
|
|
|
return Tuple3(rs[0] as GhRepoRepository, rs[1] as String, rs[2]);
|
2019-12-07 16:12:18 +08:00
|
|
|
},
|
2019-11-02 20:54:23 +08:00
|
|
|
actionBuilder: (data, setState) {
|
2019-12-07 16:12:18 +08:00
|
|
|
final repo = data.item1;
|
2019-09-25 21:41:44 +08:00
|
|
|
return ActionButton(
|
|
|
|
title: 'Repository Actions',
|
2019-09-30 15:46:06 +08:00
|
|
|
items: [
|
2020-01-01 20:59:20 +08:00
|
|
|
ActionItem(
|
2020-01-12 14:49:46 +08:00
|
|
|
text: 'Projects(${repo.projects.totalCount})',
|
2020-01-01 20:59:20 +08:00
|
|
|
url: repo.projectsUrl,
|
|
|
|
),
|
|
|
|
ActionItem(
|
2020-01-12 14:49:46 +08:00
|
|
|
text: 'Releases(${repo.releases.totalCount})',
|
2020-01-01 20:59:20 +08:00
|
|
|
url: 'https://github.com/$owner/$name/releases',
|
|
|
|
),
|
2020-01-27 13:41:17 +08:00
|
|
|
...ActionItem.getUrlActions(repo.url),
|
2019-09-25 21:41:44 +08:00
|
|
|
],
|
|
|
|
);
|
2019-02-10 18:50:40 +08:00
|
|
|
},
|
2020-01-01 20:59:20 +08:00
|
|
|
bodyBuilder: (data, setState) {
|
2019-12-07 16:12:18 +08:00
|
|
|
final repo = data.item1;
|
|
|
|
final readme = data.item2;
|
2020-04-25 16:01:32 +05:30
|
|
|
final contributorsCount = data.item3;
|
2019-12-07 16:12:18 +08:00
|
|
|
final ref = branch == null ? repo.defaultBranchRef : repo.ref;
|
2019-12-21 16:16:17 +08:00
|
|
|
final license = repo.licenseInfo?.spdxId ?? repo.licenseInfo?.name;
|
2019-11-08 18:29:08 +08:00
|
|
|
|
2019-02-04 21:38:29 +08:00
|
|
|
return Column(
|
2019-09-08 22:17:29 +08:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
2019-02-04 21:38:29 +08:00
|
|
|
children: <Widget>[
|
2020-01-29 18:23:51 +08:00
|
|
|
RepoHeader(
|
|
|
|
avatarUrl: repo.owner.avatarUrl,
|
2020-05-12 21:01:30 +05:30
|
|
|
avatarLink: '/github/${repo.owner.login}',
|
2020-01-29 18:23:51 +08:00
|
|
|
name: repo.name,
|
|
|
|
owner: repo.owner.login,
|
|
|
|
description: repo.description,
|
|
|
|
homepageUrl: repo.homepageUrl,
|
|
|
|
actions: [
|
2020-02-06 13:53:43 +08:00
|
|
|
Row(
|
|
|
|
children: <Widget>[
|
|
|
|
MutationButton(
|
2020-02-06 14:23:54 +08:00
|
|
|
active: repo.viewerSubscription ==
|
|
|
|
GhRepoSubscriptionState.SUBSCRIBED,
|
2020-02-06 13:53:43 +08:00
|
|
|
text: _buildWatchState(repo.viewerSubscription),
|
|
|
|
onPressed: () async {
|
2020-04-06 11:18:35 +08:00
|
|
|
final vs = GhRepoSubscriptionState.values.where((v) =>
|
|
|
|
v != GhRepoSubscriptionState.ARTEMIS_UNKNOWN);
|
2020-02-06 13:53:43 +08:00
|
|
|
theme.showActions(context, [
|
|
|
|
for (var v in vs)
|
|
|
|
ActionItem(
|
2020-04-06 11:18:35 +08:00
|
|
|
text: _buildWatchState(v),
|
2020-02-06 13:53:43 +08:00
|
|
|
onTap: (_) async {
|
2020-04-06 11:18:35 +08:00
|
|
|
switch (v) {
|
|
|
|
case GhRepoSubscriptionState.SUBSCRIBED:
|
|
|
|
case GhRepoSubscriptionState.IGNORED:
|
|
|
|
// TODO: https://github.com/SpinlockLabs/github.dart/pull/215
|
|
|
|
// final res = await auth.ghClient.activity
|
|
|
|
// .setRepositorySubscription(
|
|
|
|
// RepositorySlug(
|
|
|
|
// repo.owner.login,
|
|
|
|
// repo.name,
|
|
|
|
// ),
|
|
|
|
// subscribed: v ==
|
|
|
|
// GhRepoSubscriptionState.SUBSCRIBED,
|
|
|
|
// ignored:
|
|
|
|
// v == GhRepoSubscriptionState.IGNORED,
|
|
|
|
// );
|
|
|
|
final slug = RepositorySlug(
|
|
|
|
repo.owner.login, repo.name);
|
|
|
|
final response =
|
|
|
|
await auth.ghClient.request(
|
|
|
|
'PUT',
|
|
|
|
'/repos/${slug.fullName}/subscription',
|
|
|
|
statusCode: StatusCodes.OK,
|
|
|
|
body: json.encode({
|
|
|
|
'subscribed': v ==
|
|
|
|
GhRepoSubscriptionState.SUBSCRIBED,
|
|
|
|
'ignored':
|
|
|
|
v == GhRepoSubscriptionState.IGNORED
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
final res = RepositorySubscription.fromJson(
|
|
|
|
jsonDecode(response.body)
|
|
|
|
as Map<String, dynamic>);
|
|
|
|
setState(() {
|
|
|
|
if (res.subscribed) {
|
|
|
|
repo.viewerSubscription =
|
|
|
|
GhRepoSubscriptionState.SUBSCRIBED;
|
|
|
|
} else if (res.ignored) {
|
|
|
|
repo.viewerSubscription =
|
|
|
|
GhRepoSubscriptionState.IGNORED;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case GhRepoSubscriptionState.UNSUBSCRIBED:
|
|
|
|
await auth.ghClient.activity
|
|
|
|
.deleteRepositorySubscription(
|
|
|
|
RepositorySlug(
|
|
|
|
repo.owner.login,
|
|
|
|
repo.name,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
setState(() {
|
2020-02-06 13:53:43 +08:00
|
|
|
repo.viewerSubscription =
|
|
|
|
GhRepoSubscriptionState.UNSUBSCRIBED;
|
2020-04-06 11:18:35 +08:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
2020-02-06 13:53:43 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
SizedBox(width: 8),
|
|
|
|
MutationButton(
|
2020-02-06 14:23:54 +08:00
|
|
|
active: repo.viewerHasStarred,
|
2020-02-06 13:53:43 +08:00
|
|
|
text: repo.viewerHasStarred ? 'Unstar' : 'Star',
|
|
|
|
onPressed: () async {
|
2020-04-06 11:18:35 +08:00
|
|
|
if (repo.viewerHasStarred) {
|
|
|
|
await auth.ghClient.activity.unstar(
|
|
|
|
RepositorySlug(repo.owner.login, repo.name));
|
|
|
|
} else {
|
|
|
|
await auth.ghClient.activity.star(
|
|
|
|
RepositorySlug(repo.owner.login, repo.name));
|
|
|
|
}
|
2020-02-06 13:53:43 +08:00
|
|
|
setState(() {
|
2020-04-06 11:18:35 +08:00
|
|
|
repo.viewerHasStarred = !repo.viewerHasStarred;
|
2020-02-06 13:53:43 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
2020-01-29 18:23:51 +08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
trailings: <Widget>[
|
|
|
|
if (repo.repositoryTopics.nodes.isNotEmpty)
|
|
|
|
// TODO: link
|
|
|
|
Wrap(
|
|
|
|
spacing: 4,
|
|
|
|
runSpacing: 4,
|
|
|
|
children: repo.repositoryTopics.nodes.map((node) {
|
|
|
|
return MyLabel(
|
|
|
|
name: node.topic.name,
|
|
|
|
// color: Colors.blue.shade50,
|
|
|
|
color: theme.palette.grayBackground,
|
|
|
|
textColor: theme.palette.primary,
|
|
|
|
);
|
|
|
|
}).toList(),
|
|
|
|
)
|
|
|
|
],
|
2020-01-01 20:44:18 +08:00
|
|
|
),
|
2019-10-02 16:09:54 +08:00
|
|
|
CommonStyle.border,
|
2019-09-08 22:17:29 +08:00
|
|
|
Row(
|
|
|
|
children: <Widget>[
|
|
|
|
EntryItem(
|
2019-12-07 16:12:18 +08:00
|
|
|
count: repo.watchers.totalCount,
|
2019-09-09 22:50:22 +08:00
|
|
|
text: 'Watchers',
|
2020-05-12 21:01:30 +05:30
|
|
|
url: '/github/$owner/$name/watchers',
|
2019-09-08 22:17:29 +08:00
|
|
|
),
|
|
|
|
EntryItem(
|
2019-12-07 16:12:18 +08:00
|
|
|
count: repo.stargazers.totalCount,
|
2019-09-09 22:50:22 +08:00
|
|
|
text: 'Stars',
|
2020-05-12 21:01:30 +05:30
|
|
|
url: '/github/$owner/$name/stargazers',
|
2019-09-08 22:17:29 +08:00
|
|
|
),
|
|
|
|
EntryItem(
|
2019-12-07 16:12:18 +08:00
|
|
|
count: repo.forks.totalCount,
|
2019-09-09 22:50:22 +08:00
|
|
|
text: 'Forks',
|
2020-01-17 14:44:33 +08:00
|
|
|
url: 'https://github.com/$owner/$name/network/members',
|
2019-09-08 22:17:29 +08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2020-01-11 21:22:52 +08:00
|
|
|
if (repo.languages.edges.isNotEmpty) ...[
|
|
|
|
CommonStyle.border,
|
2020-02-08 14:04:02 +08:00
|
|
|
LanguageBar([
|
|
|
|
for (var edge in repo.languages.edges)
|
|
|
|
LanguageBarItem(
|
|
|
|
name: edge.node.name,
|
|
|
|
ratio: edge.size / repo.languages.totalSize,
|
|
|
|
hexColor: edge.node.color,
|
|
|
|
)
|
|
|
|
]),
|
2020-01-11 21:22:52 +08:00
|
|
|
],
|
2019-09-15 15:08:09 +08:00
|
|
|
TableView(
|
|
|
|
hasIcon: true,
|
|
|
|
items: [
|
2019-12-07 16:12:18 +08:00
|
|
|
if (ref != null)
|
2019-09-23 16:08:53 +08:00
|
|
|
TableViewItem(
|
|
|
|
leftIconData: Octicons.code,
|
2020-01-22 18:47:58 +08:00
|
|
|
text: Text(repo.primaryLanguage?.name ?? 'Code'),
|
2020-01-14 13:33:02 +08:00
|
|
|
rightWidget: Text(
|
|
|
|
(license == null ? '' : '$license • ') +
|
|
|
|
filesize(repo.diskUsage * 1000),
|
|
|
|
),
|
2020-05-12 21:01:30 +05:30
|
|
|
url: '/github/$owner/$name/blob/${ref.name}',
|
2019-09-15 15:08:09 +08:00
|
|
|
),
|
2019-12-07 16:12:18 +08:00
|
|
|
if (repo.hasIssuesEnabled)
|
2019-09-23 16:01:55 +08:00
|
|
|
TableViewItem(
|
|
|
|
leftIconData: Octicons.issue_opened,
|
|
|
|
text: Text('Issues'),
|
2019-09-30 17:13:12 +08:00
|
|
|
rightWidget:
|
2019-12-07 16:12:18 +08:00
|
|
|
Text(numberFormat.format(repo.issues.totalCount)),
|
2020-05-12 21:01:30 +05:30
|
|
|
url: '/github/$owner/$name/issues',
|
2019-09-23 16:01:55 +08:00
|
|
|
),
|
2019-09-15 15:08:09 +08:00
|
|
|
TableViewItem(
|
|
|
|
leftIconData: Octicons.git_pull_request,
|
|
|
|
text: Text('Pull requests'),
|
2019-12-07 16:12:18 +08:00
|
|
|
rightWidget:
|
|
|
|
Text(numberFormat.format(repo.pullRequests.totalCount)),
|
2020-05-12 21:01:30 +05:30
|
|
|
url: '/github/$owner/$name/pulls',
|
2019-09-15 15:08:09 +08:00
|
|
|
),
|
2020-01-02 21:31:58 +08:00
|
|
|
TableViewItem(
|
|
|
|
leftIconData: Octicons.history,
|
|
|
|
text: Text('Commits'),
|
2020-01-07 15:07:57 +08:00
|
|
|
rightWidget: Text((ref.target as GhRepoCommit)
|
2020-01-02 21:31:58 +08:00
|
|
|
.history
|
|
|
|
?.totalCount
|
|
|
|
.toString()),
|
2020-05-12 21:01:30 +05:30
|
|
|
url: '/github/$owner/$name/commits',
|
2020-01-02 21:31:58 +08:00
|
|
|
),
|
2019-12-07 16:12:18 +08:00
|
|
|
if (ref != null) ...[
|
|
|
|
if (repo.refs != null)
|
2019-09-23 19:51:49 +08:00
|
|
|
TableViewItem(
|
|
|
|
leftIconData: Octicons.git_branch,
|
|
|
|
text: Text('Branches'),
|
2019-12-07 16:12:18 +08:00
|
|
|
rightWidget: Text(ref.name +
|
2019-09-23 19:51:49 +08:00
|
|
|
' • ' +
|
2019-12-07 16:12:18 +08:00
|
|
|
numberFormat.format(repo.refs.totalCount)),
|
2019-09-23 19:51:49 +08:00
|
|
|
onTap: () async {
|
2019-12-07 16:12:18 +08:00
|
|
|
final refs = repo.refs.nodes;
|
2019-09-25 20:55:06 +08:00
|
|
|
if (refs.length < 2) return;
|
|
|
|
|
2020-02-08 11:49:31 +08:00
|
|
|
await theme.showPicker(
|
2019-09-29 15:35:33 +08:00
|
|
|
context,
|
|
|
|
PickerGroupItem(
|
2019-12-07 16:12:18 +08:00
|
|
|
value: ref.name,
|
2019-09-29 15:35:33 +08:00
|
|
|
items: refs
|
2019-12-07 16:12:18 +08:00
|
|
|
.map((b) => PickerItem(b.name, text: b.name))
|
2019-09-29 15:35:33 +08:00
|
|
|
.toList(),
|
2020-04-06 13:20:11 +08:00
|
|
|
onClose: (ref) {
|
2019-12-12 21:20:24 +08:00
|
|
|
if (ref != branch) {
|
2020-05-12 21:01:30 +05:30
|
|
|
theme.push(
|
|
|
|
context, '/github/$owner/$name?ref=$ref',
|
2019-12-12 21:20:24 +08:00
|
|
|
replace: true);
|
2019-10-06 15:52:41 +08:00
|
|
|
}
|
2019-09-29 15:35:33 +08:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2019-09-23 19:51:49 +08:00
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
2020-04-25 16:01:32 +05:30
|
|
|
TableViewItem(
|
|
|
|
leftIconData: Octicons.organization,
|
|
|
|
text: Text('Contributors'),
|
|
|
|
rightWidget: Text(contributorsCount),
|
2020-05-12 21:01:30 +05:30
|
|
|
url: '/github/$owner/$name/contributors',
|
2020-04-25 16:01:32 +05:30
|
|
|
)
|
2019-09-15 15:08:09 +08:00
|
|
|
],
|
|
|
|
),
|
2019-09-23 19:45:47 +08:00
|
|
|
if (readme != null)
|
2019-09-12 20:52:07 +08:00
|
|
|
Container(
|
2019-10-02 16:09:54 +08:00
|
|
|
padding: CommonStyle.padding,
|
2020-01-27 15:11:51 +08:00
|
|
|
color: theme.palette.background,
|
2019-11-06 21:56:52 +08:00
|
|
|
child: MarkdownView(
|
|
|
|
readme,
|
|
|
|
basePaths: [owner, name, branch ?? 'master'], // TODO:
|
|
|
|
),
|
2019-09-12 20:52:07 +08:00
|
|
|
),
|
2019-10-02 16:09:54 +08:00
|
|
|
CommonStyle.verticalGap,
|
2019-02-04 21:38:29 +08:00
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
2019-01-25 20:35:20 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|