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

339 lines
13 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-12-07 09:12:18 +01:00
import 'package:git_touch/graphql/github_repository.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';
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-09-23 12:28:33 +02:00
import 'package:git_touch/widgets/repository_item.dart';
2019-12-07 09:12:18 +01:00
import 'package:tuple/tuple.dart';
2019-02-04 14:38:29 +01:00
import '../widgets/entry_item.dart';
2019-09-30 10:31:07 +02:00
import 'package:git_touch/widgets/action_button.dart';
2019-12-21 09:55:25 +01:00
import 'package:charts_flutter/flutter.dart' as charts;
2019-02-04 14:38:29 +01:00
2019-12-13 06:13:45 +01:00
final repositoryRouter = RouterScreen('/:owner/:name', (context, params) {
if (params['ref'] == null) {
return RepositoryScreen(params['owner'].first, params['name'].first);
} else {
return RepositoryScreen(params['owner'].first, params['name'].first,
branch: params['ref'].first);
}
});
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-12-07 09:12:18 +01:00
Future<GithubRepositoryRepository> _query(BuildContext context) async {
var res = await Provider.of<AuthModel>(context).gqlClient.execute(
GithubRepositoryQuery(
variables: GithubRepositoryArguments(
owner: owner,
name: name,
branchSpecified: branch != null,
branch: branch ?? '')));
return res.data.repository;
2019-02-07 07:35:19 +01:00
}
2019-12-07 09:12:18 +01: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-12-07 09:12:18 +01:00
return RefreshStatefulScaffold<Tuple2<GithubRepositoryRepository, 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),
_fetchReadme(context),
]);
return Tuple2(rs[0] as GithubRepositoryRepository, rs[1] as String);
},
2019-11-02 13:54:23 +01:00
actionBuilder: (data, setState) {
2019-12-07 09:12:18 +01:00
if (data == null)
return ActionButton(title: 'Repository Actions', items: []);
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: [
2019-12-07 09:12:18 +01:00
ActionItem(
text: repo.viewerHasStarred ? 'Unstar' : 'Star',
onPress: (_) async {
if (repo.viewerHasStarred) {
await Provider.of<AuthModel>(context)
.deleteWithCredentials('/user/starred/$owner/$name');
repo.viewerHasStarred = false;
} else {
await Provider.of<AuthModel>(context)
.putWithCredentials('/user/starred/$owner/$name');
repo.viewerHasStarred = true;
}
setState(() {});
},
),
// TODO:
// ActionItem(
// text: data[0]['viewerSubscription'] == 'SUBSCRIBED'
// ? 'Unwatch'
// : 'Watch',
// onPress: (_) async {
// if (data[0]['viewerSubscription'] == 'SUBSCRIBED') {
// await Provider.of<AuthModel>(context).deleteWithCredentials(
// '/repos/$owner/$name/subscription');
// data[0]['viewerSubscription'] = 'UNSUBSCRIBED';
// } else {
// await Provider.of<AuthModel>(context)
// .putWithCredentials('/repos/$owner/$name/subscription');
// data[0]['viewerSubscription'] = 'SUBSCRIBED';
// }
// setState(() {});
// },
// ),
ActionItem.share(repo.url),
ActionItem.launch(repo.url),
2019-09-25 15:41:44 +02:00
],
);
},
2019-11-02 13:54:23 +01:00
bodyBuilder: (data, _) {
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-11-08 11:29:08 +01:00
final theme = Provider.of<ThemeModel>(context);
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>[
2019-12-07 09:12:18 +01:00
RepositoryItem.raw(
repo.owner.login,
repo.owner.avatarUrl,
repo.name,
repo.description,
Octicons.repo, // TODO:
repo.stargazers.totalCount,
repo.forks.totalCount,
repo.primaryLanguage?.name,
repo.primaryLanguage?.color,
repo.repositoryTopics.nodes,
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-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',
2019-12-07 15:25:37 +01:00
// screenBuilder: (context) =>
// RepositoriesScreen.forks(owner, name), TODO:
2019-09-08 16:17:29 +02:00
),
],
),
2019-10-02 10:09:54 +02:00
CommonStyle.verticalGap,
2019-12-07 09:12:18 +01:00
if (repo.languages.edges.isNotEmpty)
2019-12-21 09:55:25 +01:00
GestureDetector(
onTap: () {
showCupertinoModalPopup(
context: context,
builder: (context) {
return Container(
color: theme.palette.background,
padding: EdgeInsets.all(40),
height: 400,
child: charts.PieChart(
[
charts.Series<GithubRepositoryLanguageEdge, String>(
id: 'languages',
domainFn: (v, _) => v.node.name,
measureFn: (v, _) => v.size,
colorFn: (v, _) =>
charts.Color.fromHex(code: v.node.color),
data: repo.languages.edges,
labelAccessorFn: (v, _) => v.node.name,
)
],
defaultRenderer: charts.ArcRendererConfig(
arcRendererDecorators: [
charts.ArcLabelDecorator(
insideLabelStyleSpec: charts.TextStyleSpec(
fontSize: 20,
color: charts.Color.white,
),
outsideLabelStyleSpec: charts.TextStyleSpec(
fontSize: 20,
color: charts.Color.black,
),
)
],
),
),
);
},
);
},
child: Container(
// color: theme.palette.background,
padding: CommonStyle.padding.copyWith(top: 8, bottom: 8),
child: ClipRRect(
borderRadius: BorderRadius.circular(2),
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
),
),
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('Code'),
2019-12-21 09:16:17 +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
),
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-12-07 09:12:18 +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-12-07 09:12:18 +01:00
if (ref != null) ...[
TableViewItem(
leftIconData: Octicons.history,
text: Text('Commits'),
2019-12-07 09:22:05 +01:00
rightWidget: Text(
numberFormat.format(
(ref.target as GithubRepositoryCommit)
.history
?.totalCount,
),
),
2019-12-12 13:29:56 +01:00
url: '/$owner/$name/commits',
2019-09-23 11:08:51 +02:00
),
2019-12-07 09:12:18 +01:00
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;
2019-09-29 09:35:33 +02:00
await Provider.of<ThemeModel>(context).showPicker(
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(),
2019-12-12 14:20:24 +01:00
onClose: (ref) {
if (ref != branch) {
Provider.of<ThemeModel>(context).push(
context, '/$owner/$name?ref=$ref',
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-27 12:23:47 +02:00
TableViewItem(
leftIconData: Octicons.tag,
text: Text('Releases'),
2019-12-07 09:12:18 +01:00
rightWidget: Text(repo.releases.totalCount.toString()),
url: repo.url + '/releases',
2019-09-27 12:23:47 +02:00
),
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-11-08 11:29:08 +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
);
}
}