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

403 lines
15 KiB
Dart
Raw Normal View History

import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
2019-02-04 14:38:29 +01:00
import 'package:flutter/cupertino.dart';
2020-01-07 08:07:57 +01:00
import 'package:git_touch/graphql/gh.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-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';
2020-01-20 07:18:45 +01:00
import 'package:git_touch/widgets/entry_item.dart';
2020-01-14 06:51:53 +01:00
import 'package:git_touch/widgets/label.dart';
2020-01-12 07:49:46 +01:00
import 'package:git_touch/widgets/mutation_button.dart';
import 'package:git_touch/widgets/markdown_view.dart';
2020-01-29 11:23:51 +01:00
import 'package:git_touch/widgets/repo_header.dart';
2019-09-08 16:17:29 +02:00
import 'package:git_touch/widgets/table_view.dart';
2020-02-08 04:49:31 +01:00
import 'package:github/github.dart';
2019-09-08 14:07:35 +02:00
import 'package:provider/provider.dart';
import 'package:git_touch/models/theme.dart';
2019-12-07 09:12:18 +01:00
import 'package:tuple/tuple.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
2020-02-07 07:17:05 +01:00
class GhRepoScreen 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;
2020-02-07 07:17:05 +01:00
GhRepoScreen(this.owner, this.name, {this.branch});
2020-01-07 08:07:57 +01:00
Future<GhRepoRepository> _query(BuildContext context) async {
2019-12-07 09:12:18 +01:00
var res = await Provider.of<AuthModel>(context).gqlClient.execute(
2020-01-07 08:07:57 +01:00
GhRepoQuery(
variables: GhRepoArguments(
2019-12-07 09:12:18 +01:00
owner: owner,
name: name,
branchSpecified: branch != null,
branch: branch ?? '')));
return res.data.repository;
2019-02-07 07:35:19 +01:00
}
2020-01-22 11:43:07 +01:00
Widget _buildLanguages(BuildContext context, GhRepoRepository repo) {
final theme = Provider.of<ThemeModel>(context);
return Container(
2020-01-27 08:11:51 +01:00
color: theme.palette.background,
2020-01-22 11:43:07 +01:00
padding: CommonStyle.padding,
height: 300,
child: SingleChildScrollView(
child: Table(children: [
for (final edge in repo.languages.edges)
TableRow(children: [
Container(
padding: CommonStyle.padding,
child: Row(children: <Widget>[
Container(
width: 18,
height: 18,
decoration: BoxDecoration(
color: convertColor(edge.node.color),
shape: BoxShape.circle,
),
),
SizedBox(width: 8),
Text(
edge.node.name,
style: TextStyle(
2020-01-27 08:11:51 +01:00
color: theme.palette.text,
2020-01-22 11:43:07 +01:00
fontSize: 18,
),
),
]),
),
Container(
padding: CommonStyle.padding,
child: Text(
'${(edge.size / repo.languages.totalSize * 100).toStringAsFixed(1)}%',
style: TextStyle(
2020-01-27 08:11:51 +01:00
color: theme.palette.secondaryText,
2020-01-22 11:43:07 +01:00
fontSize: 18,
),
),
),
])
]),
),
);
}
2020-02-06 06:53:43 +01: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';
}
}
String _buildWatchSelectState(GhWatchSubscriptionState state) {
switch (state) {
case GhWatchSubscriptionState.IGNORED:
return 'Ignoring';
case GhWatchSubscriptionState.SUBSCRIBED:
return 'Watching';
case GhWatchSubscriptionState.UNSUBSCRIBED:
return 'Not watching';
default:
return '';
}
}
2020-02-08 04:49:31 +01: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 13:35:20 +01:00
@override
Widget build(BuildContext context) {
2020-02-08 04:49:31 +01:00
final theme = Provider.of<ThemeModel>(context);
final auth = Provider.of<AuthModel>(context);
2020-01-07 08:07:57 +01:00
return RefreshStatefulScaffold<Tuple2<GhRepoRepository, String>>(
2019-09-11 13:59:47 +02:00
title: AppBarTitle('Repository'),
2019-12-07 09:12:18 +01:00
fetchData: () async {
final rs = await Future.wait([
_query(context),
2020-02-08 04:49:31 +01:00
_fetchReadme(context),
2019-12-07 09:12:18 +01:00
]);
2020-01-07 08:07:57 +01:00
return Tuple2(rs[0] as GhRepoRepository, rs[1] as String);
2019-12-07 09:12:18 +01:00
},
2019-11-02 13:54:23 +01:00
actionBuilder: (data, setState) {
2019-12-07 09:12:18 +01:00
final repo = data.item1;
2019-09-25 15:41:44 +02:00
return ActionButton(
title: 'Repository Actions',
2019-09-30 09:46:06 +02:00
items: [
2020-01-01 13:59:20 +01:00
ActionItem(
2020-01-12 07:49:46 +01:00
text: 'Projects(${repo.projects.totalCount})',
2020-01-01 13:59:20 +01:00
url: repo.projectsUrl,
),
ActionItem(
2020-01-12 07:49:46 +01:00
text: 'Releases(${repo.releases.totalCount})',
2020-01-01 13:59:20 +01:00
url: 'https://github.com/$owner/$name/releases',
),
2020-01-27 06:41:17 +01:00
...ActionItem.getUrlActions(repo.url),
2019-09-25 15:41:44 +02:00
],
);
},
2020-01-01 13:59:20 +01:00
bodyBuilder: (data, setState) {
2019-12-07 09:12:18 +01:00
final repo = data.item1;
final readme = data.item2;
final ref = branch == null ? repo.defaultBranchRef : repo.ref;
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-12-07 09:12:18 +01:00
repo.languages.edges.length +
2019-09-14 18:34:10 +02:00
1;
2019-09-08 16:17:29 +02:00
2019-12-21 09:16:17 +01:00
final license = repo.licenseInfo?.spdxId ?? repo.licenseInfo?.name;
2019-11-08 11:29:08 +01: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>[
2020-01-29 11:23:51 +01:00
RepoHeader(
avatarUrl: repo.owner.avatarUrl,
avatarLink: '/${repo.owner.login}',
name: repo.name,
owner: repo.owner.login,
description: repo.description,
homepageUrl: repo.homepageUrl,
actions: [
2020-02-06 06:53:43 +01:00
Row(
children: <Widget>[
MutationButton(
2020-02-06 07:23:54 +01:00
active: repo.viewerSubscription ==
GhRepoSubscriptionState.SUBSCRIBED,
2020-02-06 06:53:43 +01:00
text: _buildWatchState(repo.viewerSubscription),
onPressed: () async {
final vs = GhWatchSubscriptionState.values.where((v) =>
v != GhWatchSubscriptionState.ARTEMIS_UNKNOWN);
theme.showActions(context, [
for (var v in vs)
ActionItem(
text: _buildWatchSelectState(v),
onTap: (_) async {
final res = await auth.gqlClient.execute(
GhWatchQuery(
variables: GhWatchArguments(
id: repo.id,
state: v,
),
),
);
setState(() {
final r = res.data.updateSubscription
.subscribable as GhWatchRepository;
switch (r.viewerSubscription) {
case GhWatchSubscriptionState.IGNORED:
repo.viewerSubscription =
GhRepoSubscriptionState.IGNORED;
break;
case GhWatchSubscriptionState.SUBSCRIBED:
repo.viewerSubscription =
GhRepoSubscriptionState.SUBSCRIBED;
break;
case GhWatchSubscriptionState.UNSUBSCRIBED:
repo.viewerSubscription =
GhRepoSubscriptionState.UNSUBSCRIBED;
break;
default:
}
});
},
)
]);
},
),
SizedBox(width: 8),
MutationButton(
2020-02-06 07:23:54 +01:00
active: repo.viewerHasStarred,
2020-02-06 06:53:43 +01:00
text: repo.viewerHasStarred ? 'Unstar' : 'Star',
onPressed: () async {
final res = await auth.gqlClient.execute(
GhStarQuery(
variables: GhStarArguments(
id: repo.id,
flag: !repo.viewerHasStarred,
),
),
);
setState(() {
repo.viewerHasStarred = res.data.removeStar?.starrable
?.viewerHasStarred ??
2020-01-29 11:23:51 +01:00
res.data.addStar.starrable.viewerHasStarred;
2020-02-06 06:53:43 +01:00
});
},
),
],
2020-01-29 11:23:51 +01: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 13:44:18 +01:00
),
2019-10-02 10:09:54 +02:00
CommonStyle.border,
2019-09-08 16:17:29 +02:00
Row(
children: <Widget>[
EntryItem(
2019-12-07 09:12:18 +01:00
count: repo.watchers.totalCount,
2019-09-09 16:50:22 +02:00
text: 'Watchers',
2019-12-13 06:40:05 +01:00
url: '/$owner/$name/watchers',
2019-09-08 16:17:29 +02:00
),
EntryItem(
2019-12-07 09:12:18 +01:00
count: repo.stargazers.totalCount,
2019-09-09 16:50:22 +02:00
text: 'Stars',
2019-12-13 06:40:05 +01:00
url: '/$owner/$name/stargazers',
2019-09-08 16:17:29 +02:00
),
EntryItem(
2019-12-07 09:12:18 +01:00
count: repo.forks.totalCount,
2019-09-09 16:50:22 +02:00
text: 'Forks',
2020-01-17 07:44:33 +01:00
url: 'https://github.com/$owner/$name/network/members',
2019-09-08 16:17:29 +02:00
),
],
),
2020-01-11 14:22:52 +01:00
if (repo.languages.edges.isNotEmpty) ...[
CommonStyle.border,
CupertinoButton(
padding: EdgeInsets.zero,
minSize: 0,
onPressed: () {
2019-12-21 09:55:25 +01:00
showCupertinoModalPopup(
context: context,
2020-01-22 11:43:07 +01:00
builder: (context) => _buildLanguages(context, repo),
2019-12-21 09:55:25 +01:00
);
},
child: Container(
2020-01-27 08:11:51 +01:00
// color: theme.palette.background,
2019-12-21 09:55:25 +01:00
padding: CommonStyle.padding.copyWith(top: 8, bottom: 8),
child: ClipRRect(
2020-01-14 06:33:02 +01:00
borderRadius: BorderRadius.circular(5),
2019-12-21 09:55:25 +01:00
child: SizedBox(
height: 10,
child: Row(
children: join(
SizedBox(width: 1),
repo.languages.edges
.map((lang) => Container(
color: convertColor(lang.node.color),
width: langWidth *
lang.size /
repo.languages.totalSize))
.toList(),
),
2019-11-05 14:22:41 +01:00
),
),
2019-09-23 10:10:46 +02:00
),
2019-02-04 14:38:29 +01:00
),
),
2020-01-11 14:22:52 +01:00
],
2019-09-15 09:08:09 +02:00
TableView(
hasIcon: true,
items: [
2019-12-07 09:12:18 +01:00
if (ref != null)
TableViewItem(
leftIconData: Octicons.code,
text: Text(repo.primaryLanguage?.name ?? 'Code'),
2020-01-14 06:33:02 +01:00
rightWidget: Text(
(license == null ? '' : '$license') +
filesize(repo.diskUsage * 1000),
),
2019-12-12 13:29:56 +01:00
url: '/$owner/$name/blob/${ref.name}',
2019-09-15 09:08:09 +02:00
),
2019-12-07 09:12:18 +01:00
if (repo.hasIssuesEnabled)
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-12-07 09:12:18 +01:00
Text(numberFormat.format(repo.issues.totalCount)),
2019-12-12 13:29:56 +01:00
url: '/$owner/$name/issues',
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-12-07 09:12:18 +01:00
rightWidget:
Text(numberFormat.format(repo.pullRequests.totalCount)),
2019-12-12 13:29:56 +01:00
url: '/$owner/$name/pulls',
2019-09-15 09:08:09 +02:00
),
2020-01-02 14:31:58 +01:00
TableViewItem(
leftIconData: Octicons.history,
text: Text('Commits'),
2020-01-07 08:07:57 +01:00
rightWidget: Text((ref.target as GhRepoCommit)
2020-01-02 14:31:58 +01:00
.history
?.totalCount
.toString()),
url: '/$owner/$name/commits',
),
2019-12-07 09:12:18 +01:00
if (ref != null) ...[
if (repo.refs != null)
2019-09-23 13:51:49 +02:00
TableViewItem(
leftIconData: Octicons.git_branch,
text: Text('Branches'),
2019-12-07 09:12:18 +01:00
rightWidget: Text(ref.name +
2019-09-23 13:51:49 +02:00
'' +
2019-12-07 09:12:18 +01:00
numberFormat.format(repo.refs.totalCount)),
2019-09-23 13:51:49 +02:00
onTap: () async {
2019-12-07 09:12:18 +01:00
final refs = repo.refs.nodes;
if (refs.length < 2) return;
2020-02-08 04:49:31 +01:00
await theme.showPicker(
2019-09-29 09:35:33 +02:00
context,
PickerGroupItem(
2019-12-07 09:12:18 +01:00
value: ref.name,
2019-09-29 09:35:33 +02:00
items: refs
2019-12-07 09:12:18 +01:00
.map((b) => PickerItem(b.name, text: b.name))
2019-09-29 09:35:33 +02:00
.toList(),
2020-01-27 08:07:16 +01:00
onChange: (ref) {
2019-12-12 14:20:24 +01:00
if (ref != branch) {
2020-02-08 04:49:31 +01:00
theme.push(context, '/$owner/$name?ref=$ref',
2019-12-12 14:20:24 +01:00
replace: true);
2019-10-06 09:52:41 +02:00
}
2019-09-29 09:35:33 +02:00
},
),
);
2019-09-23 13:51:49 +02:00
},
),
],
2019-09-15 09:08:09 +02:00
],
),
2019-09-23 13:45:47 +02:00
if (readme != null)
Container(
2019-10-02 10:09:54 +02:00
padding: CommonStyle.padding,
2020-01-27 08:11:51 +01:00
color: theme.palette.background,
2019-11-06 14:56:52 +01:00
child: MarkdownView(
readme,
basePaths: [owner, name, branch ?? 'master'], // TODO:
),
),
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
);
}
}