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

311 lines
9.7 KiB
Dart
Raw Normal View History

2019-02-04 14:38:29 +01: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-08 14:07:35 +02:00
import 'package:git_touch/models/settings.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';
2019-09-08 16:17:29 +02:00
import 'package:git_touch/widgets/table_view.dart';
import 'package:primer/primer.dart';
2019-09-08 14:07:35 +02:00
import 'package:provider/provider.dart';
2019-02-04 14:38:29 +01:00
import 'package:flutter_markdown/flutter_markdown.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';
import 'package:share/share.dart';
import 'package:url_launcher/url_launcher.dart';
import '../scaffolds/refresh.dart';
2019-02-04 14:38:29 +01:00
import '../widgets/repo_item.dart';
import '../widgets/entry_item.dart';
import '../screens/issues.dart';
import '../screens/user.dart';
2019-03-10 16:34:34 +01:00
import '../screens/organization.dart';
2019-02-20 09:31:22 +01:00
import '../widgets/action.dart';
2019-02-04 14:38:29 +01:00
2019-09-08 14:33:04 +02:00
class RepoScreen extends StatelessWidget {
2019-02-07 07:35:19 +01:00
final String owner;
final String name;
2019-09-08 16:17:29 +02:00
static const _languageBarPadding = 10.0;
2019-02-07 07:35:19 +01:00
RepoScreen(this.owner, this.name);
2019-09-08 14:33:04 +02:00
RepoScreen.fromFullName(String fullName)
: owner = fullName.split('/')[0],
name = fullName.split('/')[1];
2019-02-07 07:35:19 +01:00
Future queryRepo(BuildContext context) async {
2019-09-08 14:07:35 +02:00
var data = await Provider.of<SettingsModel>(context).query('''
2019-02-04 14:38:29 +01:00
{
repository(owner: "$owner", name: "$name") {
id
2019-02-04 14:38:29 +01:00
owner {
__typename
2019-02-04 14:38:29 +01:00
login
2019-03-10 16:34:34 +01:00
url
2019-09-08 15:20:12 +02:00
avatarUrl
2019-02-04 14:38:29 +01:00
}
name
isPrivate
isFork
description
2019-09-09 16:50:22 +02:00
watchers {
totalCount
}
2019-02-04 14:38:29 +01:00
stargazers {
totalCount
}
forks {
totalCount
}
primaryLanguage {
color
name
2019-09-08 16:17:29 +02:00
}
2019-02-04 14:38:29 +01:00
issues(states: OPEN) {
totalCount
}
pullRequests(states: OPEN) {
totalCount
}
2019-09-08 16:17:29 +02:00
languages(first: 10, orderBy: {field: SIZE, direction: DESC}) {
totalSize
edges {
size
node {
name
color
}
}
}
url
defaultBranchRef {
name
2019-08-30 10:26:56 +02:00
target {
... on Commit {
history {
totalCount
}
}
}
}
viewerHasStarred
viewerSubscription
object(expression: "master:README.md") {
... on Blob {
text
}
}
licenseInfo {
name
2019-09-13 08:54:18 +02:00
spdxId
}
2019-02-04 14:38:29 +01:00
}
}
''');
// FIXME: 1. Default branch 2. Other readme file names
2019-02-07 07:35:19 +01:00
return data['repository'];
}
2019-01-25 13:35:20 +01:00
@override
Widget build(BuildContext context) {
2019-02-04 14:38:29 +01:00
return RefreshScaffold(
2019-09-11 13:59:47 +02:00
title: AppBarTitle('Repository'),
trailingBuilder: (payload) {
2019-02-20 09:31:22 +01:00
return ActionButton(title: 'Repository Actions', actions: [
2019-06-20 16:59:13 +02:00
MyAction(
2019-03-10 16:34:34 +01:00
text: '@$owner',
onPress: () {
WidgetBuilder builder;
switch (payload['owner']['__typename']) {
case 'Organization':
2019-03-10 16:34:34 +01:00
// builder = (_) => OrganizationScreen(owner);
// break;
// Seems organization permission is a little complicated
// So we just launch browser currently
launch(payload['owner']['url']);
return;
case 'User':
builder = (_) => UserScreen(owner);
break;
}
Provider.of<ThemeModel>(context)
.pushRoute(context: context, builder: builder);
},
),
2019-06-20 16:59:13 +02:00
MyAction(
2019-02-20 09:31:22 +01:00
text: payload['viewerHasStarred'] ? 'Unstar' : 'Star',
onPress: () async {
if (payload['viewerHasStarred']) {
2019-09-08 14:07:35 +02:00
await Provider.of<SettingsModel>(context)
2019-02-20 09:31:22 +01:00
.deleteWithCredentials('/user/starred/$owner/$name');
payload['viewerHasStarred'] = false;
} else {
2019-09-08 14:07:35 +02:00
Provider.of<SettingsModel>(context)
2019-02-20 09:31:22 +01:00
.putWithCredentials('/user/starred/$owner/$name');
payload['viewerHasStarred'] = true;
}
},
),
// TODO: watch
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']);
},
),
2019-02-20 09:31:22 +01:00
]);
},
onRefresh: () => queryRepo(context),
bodyBuilder: (payload) {
2019-09-08 16:17:29 +02:00
final langWidth =
MediaQuery.of(context).size.width - _languageBarPadding * 2;
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-09-09 16:50:22 +02:00
RepoItem(payload, inRepoScreen: true),
2019-09-08 16:17:29 +02:00
BorderView(),
Row(
children: <Widget>[
EntryItem(
2019-09-09 16:50:22 +02:00
count: payload['watchers']['totalCount'],
text: 'Watchers',
2019-09-08 16:17:29 +02:00
screenBuilder: (context) =>
2019-09-09 16:50:22 +02:00
UsersScreen(login: 'pd4d10'), // FIXME:
2019-09-08 16:17:29 +02:00
),
EntryItem(
2019-09-09 16:50:22 +02:00
count: payload['stargazers']['totalCount'],
text: 'Stars',
screenBuilder: (context) => UsersScreen(login: 'pd4d10'),
2019-09-08 16:17:29 +02:00
),
EntryItem(
2019-09-09 16:50:22 +02:00
count: payload['forks']['totalCount'],
text: 'Forks',
screenBuilder: (context) => UsersScreen(login: 'pd4d10'),
2019-09-08 16:17:29 +02:00
),
],
),
BorderView(height: 10),
Padding(
padding: const EdgeInsets.all(_languageBarPadding),
child: ClipRRect(
borderRadius: BorderRadius.circular(4),
child: Container(
height: 10,
child: Row(
children: (payload['languages']['edges'] as List)
.map((lang) => Container(
color: convertColor(lang['node']['color']),
width: langWidth *
lang['size'] /
payload['languages']['totalSize']))
.toList()),
2019-02-04 14:38:29 +01:00
),
),
),
2019-09-08 16:17:29 +02:00
TableView(items: [
TableViewItem(
leftWidget: Icon(Octicons.code,
size: 20,
color: convertColor(payload['primaryLanguage'] == null
? null
: payload['primaryLanguage']['color'])),
text: Text(payload['primaryLanguage'] == null
? 'Unknown'
: payload['primaryLanguage']['name']),
rightWidget: Row(
children: <Widget>[
Text(filesize(payload['languages']['totalSize'] as int)),
Icon(
CupertinoIcons.right_chevron,
size: 18,
color: PrimerColors.gray300,
),
],
2019-09-08 16:17:29 +02:00
),
screenBuilder: (_) => ObjectScreen(owner: owner, name: name),
),
TableViewItem(
leftWidget: Icon(Octicons.issue_opened, size: 20),
text: Text('Issues'),
rightWidget: Row(
children: <Widget>[
Text(payload['issues']['totalCount'].toString()),
Icon(
CupertinoIcons.right_chevron,
size: 18,
color: PrimerColors.gray300,
),
],
),
screenBuilder: (_) => IssuesScreen(owner: owner, name: name),
),
TableViewItem(
leftWidget: Icon(Octicons.git_pull_request, size: 20),
text: Text('Pull requests'),
rightWidget: Row(
children: <Widget>[
Text(payload['pullRequests']['totalCount'].toString()),
Icon(
CupertinoIcons.right_chevron,
size: 18,
color: PrimerColors.gray300,
),
],
),
screenBuilder: (_) =>
IssuesScreen(owner: owner, name: name, isPullRequest: true),
),
]),
BorderView(height: 10),
TableView(items: [
TableViewItem(
leftWidget: Icon(Octicons.law, size: 20),
text: Text('License'),
rightWidget: Text(payload['licenseInfo'] == null
? 'Unknown'
2019-09-13 08:54:18 +02:00
: (payload['licenseInfo']['spdxId'] ??
payload['licenseInfo']['name'])
.toString()),
),
TableViewItem(
leftWidget: Icon(Octicons.history, size: 20),
text: Text('Commits'),
rightWidget: Row(
children: <Widget>[
Text(payload['defaultBranchRef']['target']['history']
['totalCount']
.toString()),
Icon(
CupertinoIcons.right_chevron,
size: 18,
color: PrimerColors.gray300,
),
],
),
screenBuilder: (_) => CommitsScreen(owner, name),
),
2019-09-08 16:17:29 +02:00
]),
BorderView(height: 10),
if (payload['object'] != null)
Container(
padding: EdgeInsets.all(16),
child: MarkdownBody(data: payload['object']['text']),
),
2019-02-04 14:38:29 +01:00
],
);
},
2019-01-25 13:35:20 +01:00
);
}
}