2020-11-01 16:43:51 +01:00
|
|
|
import 'dart:convert';
|
2021-01-11 15:31:54 +01:00
|
|
|
import 'dart:io';
|
2020-10-17 13:06:11 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:git_touch/models/auth.dart';
|
|
|
|
import 'package:git_touch/models/gitee.dart';
|
2021-01-09 17:52:45 +01:00
|
|
|
import 'package:git_touch/models/theme.dart';
|
2020-10-17 13:06:11 +02:00
|
|
|
import 'package:git_touch/scaffolds/refresh_stateful.dart';
|
|
|
|
import 'package:git_touch/utils/utils.dart';
|
|
|
|
import 'package:git_touch/widgets/app_bar_title.dart';
|
|
|
|
import 'package:git_touch/widgets/entry_item.dart';
|
2020-11-04 09:41:04 +01:00
|
|
|
import 'package:git_touch/widgets/markdown_view.dart';
|
2021-01-11 15:31:54 +01:00
|
|
|
import 'package:git_touch/widgets/mutation_button.dart';
|
2020-10-17 13:06:11 +02:00
|
|
|
import 'package:git_touch/widgets/repo_header.dart';
|
|
|
|
import 'package:git_touch/widgets/table_view.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:tuple/tuple.dart';
|
2020-11-01 16:43:51 +01:00
|
|
|
import 'package:http/http.dart' as http;
|
2021-02-03 05:04:41 +01:00
|
|
|
import 'package:flutter_gen/gen_l10n/S.dart';
|
2020-10-17 13:06:11 +02:00
|
|
|
|
2021-01-11 15:31:54 +01:00
|
|
|
class StatusPayload {
|
|
|
|
bool isWatching;
|
|
|
|
bool isStarred;
|
|
|
|
StatusPayload(this.isWatching, this.isStarred);
|
|
|
|
}
|
|
|
|
|
2020-10-17 13:06:11 +02:00
|
|
|
class GeRepoScreen extends StatelessWidget {
|
|
|
|
final String owner;
|
|
|
|
final String name;
|
2021-01-09 17:52:45 +01:00
|
|
|
final String branch;
|
|
|
|
GeRepoScreen(this.owner, this.name, {this.branch});
|
2020-10-17 13:06:11 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-01-09 17:52:45 +01:00
|
|
|
return RefreshStatefulScaffold<
|
2021-01-11 15:31:54 +01:00
|
|
|
Tuple4<GiteeRepo, MarkdownViewData, List<GiteeBranch>, StatusPayload>>(
|
2021-02-03 05:04:41 +01:00
|
|
|
title: AppBarTitle(AppLocalizations.of(context).repository),
|
2020-10-17 13:06:11 +02:00
|
|
|
fetch: () async {
|
|
|
|
final auth = context.read<AuthModel>();
|
2020-11-08 08:00:13 +01:00
|
|
|
final repo = await auth.fetchGitee('/repos/$owner/$name').then((v) {
|
|
|
|
return GiteeRepo.fromJson(v);
|
|
|
|
});
|
2020-11-01 16:43:51 +01:00
|
|
|
|
2020-11-08 08:00:13 +01:00
|
|
|
final md =
|
|
|
|
() => auth.fetchGitee('/repos/$owner/$name/readme').then((v) {
|
|
|
|
return (v['content'] as String)?.base64ToUtf8;
|
|
|
|
});
|
|
|
|
final html = () => md().then((v) async {
|
|
|
|
final res = await http.post(
|
2021-05-16 06:09:27 +02:00
|
|
|
Uri.parse('${auth.activeAccount.domain}/api/v5/markdown'),
|
2020-11-08 08:00:13 +01:00
|
|
|
headers: {'Authorization': 'token ${auth.token}'},
|
|
|
|
body: {'text': v},
|
|
|
|
);
|
|
|
|
return utf8.decode(res.bodyBytes).normalizedHtml;
|
|
|
|
});
|
|
|
|
final readmeData = MarkdownViewData(context, md: md, html: html);
|
2021-01-09 17:52:45 +01:00
|
|
|
final branches =
|
|
|
|
await auth.fetchGitee('/repos/$owner/$name/branches').then((v) {
|
|
|
|
return [for (var branch in v) GiteeBranch.fromJson(branch)];
|
|
|
|
});
|
2021-01-11 15:31:54 +01:00
|
|
|
bool isStarred = await auth
|
|
|
|
.fetchGitee('/user/starred/$owner/$name', requestType: 'NO CONTENT')
|
|
|
|
.then((v) => v.statusCode == HttpStatus.noContent);
|
|
|
|
bool isWatching = await auth
|
|
|
|
.fetchGitee('/user/subscriptions/$owner/$name',
|
|
|
|
requestType: 'NO CONTENT')
|
|
|
|
.then((v) => v.statusCode == HttpStatus.noContent);
|
|
|
|
StatusPayload statusPayload = StatusPayload(isWatching, isStarred);
|
|
|
|
return Tuple4(repo, readmeData, branches, statusPayload);
|
2020-10-17 13:06:11 +02:00
|
|
|
},
|
2021-01-31 08:49:28 +01:00
|
|
|
bodyBuilder: (t, setData) {
|
2020-10-17 13:06:11 +02:00
|
|
|
final p = t.item1;
|
2021-01-09 17:52:45 +01:00
|
|
|
final branches = t.item3;
|
|
|
|
final theme = context.read<ThemeModel>();
|
2020-10-17 13:06:11 +02:00
|
|
|
return Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
children: <Widget>[
|
|
|
|
RepoHeader(
|
2021-01-11 15:31:54 +01:00
|
|
|
avatarUrl: p.owner.avatarUrl,
|
|
|
|
avatarLink: '/gitee/${p.namespace.path}',
|
|
|
|
owner: p.namespace.path,
|
|
|
|
name: p.path,
|
|
|
|
description: p.description,
|
|
|
|
homepageUrl: p.homepage,
|
|
|
|
actions: [
|
|
|
|
Row(children: <Widget>[
|
|
|
|
MutationButton(
|
|
|
|
active: t.item4.isWatching,
|
|
|
|
text: t.item4.isWatching ? 'Ignore' : 'Watch',
|
|
|
|
onTap: () async {
|
|
|
|
final String watchType =
|
|
|
|
t.item4.isWatching ? 'ignoring' : 'watching';
|
|
|
|
await context.read<AuthModel>().fetchGitee(
|
|
|
|
'/user/subscriptions/$owner/$name?watch_type=$watchType',
|
|
|
|
requestType: t.item4.isWatching ? 'DELETE' : 'PUT');
|
2021-01-31 08:49:28 +01:00
|
|
|
|
|
|
|
t.item4.isWatching = !t.item4.isWatching;
|
|
|
|
setData(t);
|
2021-01-11 15:31:54 +01:00
|
|
|
},
|
|
|
|
),
|
|
|
|
SizedBox(width: 8),
|
|
|
|
MutationButton(
|
|
|
|
active: t.item4.isStarred,
|
|
|
|
text: t.item4.isStarred ? 'Unstar' : 'Star',
|
|
|
|
onTap: () async {
|
|
|
|
await context.read<AuthModel>().fetchGitee(
|
|
|
|
'/user/starred/$owner/$name',
|
|
|
|
requestType: t.item4.isStarred ? 'DELETE' : 'PUT');
|
|
|
|
|
2021-01-31 08:49:28 +01:00
|
|
|
t.item4.isStarred = !t.item4.isStarred;
|
|
|
|
setData(t);
|
2021-01-11 15:31:54 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
])
|
|
|
|
]),
|
2020-10-17 13:06:11 +02:00
|
|
|
Row(
|
|
|
|
children: <Widget>[
|
|
|
|
EntryItem(
|
2021-01-11 15:31:54 +01:00
|
|
|
count: p.watchersCount,
|
2020-10-17 13:06:11 +02:00
|
|
|
text: 'Watchers',
|
|
|
|
url: '/gitee/$owner/$name/watchers',
|
|
|
|
),
|
|
|
|
EntryItem(
|
|
|
|
count: p.stargazersCount,
|
|
|
|
text: 'Stars',
|
|
|
|
url: '/gitee/$owner/$name/stargazers',
|
|
|
|
),
|
|
|
|
EntryItem(
|
|
|
|
count: p.forksCount,
|
|
|
|
text: 'Forks',
|
|
|
|
url: '/gitee/$owner/$name/forks',
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
CommonStyle.border,
|
|
|
|
TableView(
|
|
|
|
hasIcon: true,
|
|
|
|
items: [
|
|
|
|
TableViewItem(
|
|
|
|
leftIconData: Octicons.code,
|
|
|
|
text: Text('Code'),
|
|
|
|
rightWidget: Text(p.license ?? ''),
|
2021-01-09 17:52:45 +01:00
|
|
|
url:
|
|
|
|
'/gitee/$owner/$name/tree/${branch == null ? p.defaultBranch : branch}',
|
2020-10-17 13:06:11 +02:00
|
|
|
),
|
|
|
|
TableViewItem(
|
|
|
|
leftIconData: Octicons.issue_opened,
|
|
|
|
text: Text('Issues'),
|
|
|
|
rightWidget: Text(numberFormat.format(p.openIssuesCount)),
|
2021-01-06 05:52:58 +01:00
|
|
|
url: '/gitee/$owner/$name/issues',
|
2020-10-17 13:06:11 +02:00
|
|
|
),
|
|
|
|
if (p.pullRequestsEnabled)
|
|
|
|
TableViewItem(
|
|
|
|
leftIconData: Octicons.git_pull_request,
|
|
|
|
text: Text('Pull requests'),
|
2021-01-06 05:52:58 +01:00
|
|
|
url: '/gitee/$owner/$name/pulls',
|
2020-10-17 13:06:11 +02:00
|
|
|
),
|
|
|
|
TableViewItem(
|
|
|
|
leftIconData: Octicons.history,
|
|
|
|
text: Text('Commits'),
|
2021-01-09 17:52:45 +01:00
|
|
|
url:
|
|
|
|
'/gitee/$owner/$name/commits?branch=${branch == null ? p.defaultBranch : branch}',
|
2020-10-17 13:06:11 +02:00
|
|
|
),
|
2021-01-09 17:52:45 +01:00
|
|
|
if (branches != null)
|
|
|
|
TableViewItem(
|
|
|
|
leftIconData: Octicons.git_branch,
|
2021-02-03 05:04:41 +01:00
|
|
|
text: Text(AppLocalizations.of(context).branches),
|
2021-01-09 17:52:45 +01:00
|
|
|
rightWidget: Text(
|
|
|
|
(branch == null ? p.defaultBranch : branch) +
|
|
|
|
' • ' +
|
|
|
|
branches.length.toString()),
|
|
|
|
onTap: () async {
|
|
|
|
if (branches.length < 2) return;
|
|
|
|
|
|
|
|
await theme.showPicker(
|
|
|
|
context,
|
|
|
|
PickerGroupItem(
|
|
|
|
value: branch,
|
|
|
|
items: branches
|
|
|
|
.map((b) => PickerItem(b.name, text: b.name))
|
|
|
|
.toList(),
|
|
|
|
onClose: (ref) {
|
|
|
|
if (ref != branch) {
|
|
|
|
theme.push(
|
|
|
|
context, '/gitee/$owner/$name?branch=$ref',
|
|
|
|
replace: true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
TableViewItem(
|
|
|
|
leftIconData: Octicons.organization,
|
|
|
|
text: Text('Contributors'),
|
|
|
|
url: '/gitee/$owner/$name/contributors'),
|
2020-10-17 13:06:11 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
CommonStyle.verticalGap,
|
2020-11-08 08:00:13 +01:00
|
|
|
MarkdownView(t.item2)
|
2020-10-17 13:06:11 +02:00
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|