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

181 lines
4.8 KiB
Dart
Raw Normal View History

2019-02-04 14:38:29 +01:00
import 'dart:convert';
import 'package:flutter/material.dart';
2019-02-04 14:38:29 +01:00
import 'package:flutter/cupertino.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:share/share.dart';
import 'package:url_launcher/url_launcher.dart';
2019-02-07 07:35:19 +01:00
import '../providers/settings.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';
2019-02-20 09:31:22 +01:00
import '../widgets/action.dart';
2019-02-04 14:38:29 +01:00
2019-02-07 07:35:19 +01:00
class RepoScreen extends StatefulWidget {
final String owner;
final String name;
RepoScreen(this.owner, this.name);
@override
_RepoScreenState createState() => _RepoScreenState();
2019-02-04 14:38:29 +01:00
}
2019-02-07 07:35:19 +01:00
class _RepoScreenState extends State<RepoScreen> {
get owner => widget.owner;
get name => widget.name;
2019-02-07 07:35:19 +01:00
Future queryRepo(BuildContext context) async {
var data = await SettingsProvider.of(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 {
login
}
name
isPrivate
isFork
description
stargazers {
totalCount
}
forks {
totalCount
}
primaryLanguage {
color
name
}
issues(states: OPEN) {
totalCount
}
pullRequests(states: OPEN) {
totalCount
}
url
defaultBranchRef {
name
}
viewerHasStarred
viewerSubscription
2019-02-04 14:38:29 +01:00
}
}
''');
2019-02-07 07:35:19 +01:00
return data['repository'];
}
2019-02-07 07:35:19 +01:00
Future fetchReadme(BuildContext context) async {
var owner = widget.owner;
var name = widget.name;
var data = await SettingsProvider.of(context)
.getWithCredentials('/repos/$owner/$name/readme');
2019-02-14 05:42:25 +01:00
if (data['content'] == null) {
return '';
}
2019-02-07 07:35:19 +01:00
var bits = base64.decode(data['content'].replaceAll('\n', ''));
var str = utf8.decode(bits);
return str;
}
2019-01-25 13:35:20 +01:00
@override
Widget build(BuildContext context) {
2019-02-04 14:38:29 +01:00
return RefreshScaffold(
title: Text(widget.owner + '/' + widget.name),
2019-02-20 09:31:22 +01:00
trailingBuilder: (data) {
var payload = data[0];
return ActionButton(title: 'Repository Actions', actions: [
Action(
text: payload['viewerHasStarred'] ? 'Unstar' : 'Star',
onPress: () async {
if (payload['viewerHasStarred']) {
await SettingsProvider.of(context)
.deleteWithCredentials('/user/starred/$owner/$name');
payload['viewerHasStarred'] = false;
} else {
SettingsProvider.of(context)
.putWithCredentials('/user/starred/$owner/$name');
payload['viewerHasStarred'] = true;
}
},
),
// TODO: watch
Action(
text: 'Share',
onPress: () {
Share.share(payload['url']);
},
),
Action(
text: 'Open in Browser',
onPress: () {
launch(payload['url']);
},
),
2019-02-20 09:31:22 +01:00
]);
},
onRefresh: () => Future.wait([
queryRepo(context),
fetchReadme(context),
]),
bodyBuilder: (data) {
var payload = data[0];
var readme = data[1];
2019-02-04 14:38:29 +01:00
return Column(
children: <Widget>[
RepoItem(payload),
Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(color: Colors.black12),
top: BorderSide(color: Colors.black12),
),
),
child: Row(
children: <Widget>[
EntryItem(
count: payload['issues']['totalCount'],
text: 'Issues',
screenBuilder: (context) => IssuesScreen(
owner: widget.owner,
name: widget.name,
),
2019-02-04 14:38:29 +01:00
),
EntryItem(
count: payload['pullRequests']['totalCount'],
text: 'Pull Requests',
screenBuilder: (context) => IssuesScreen(
owner: widget.owner,
name: widget.name,
isPullRequest: true,
),
2019-02-04 14:38:29 +01:00
),
EntryItem(
text: 'Files',
url: payload['url'] + '?files=1',
),
EntryItem(
text: 'Commits',
url: payload['url'] +
'/commits/' +
payload['defaultBranchRef']['name'],
),
2019-02-04 14:38:29 +01:00
],
),
),
Container(
padding: EdgeInsets.all(16),
child: MarkdownBody(data: readme),
),
],
);
},
2019-01-25 13:35:20 +01:00
);
}
}