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

214 lines
8.2 KiB
Dart
Raw Normal View History

2022-10-03 12:21:22 +02:00
import 'dart:convert';
import 'dart:io';
2022-09-13 19:19:52 +02:00
2022-10-03 12:21:22 +02:00
import 'package:antd_mobile/antd_mobile.dart';
2022-09-17 14:35:45 +02:00
import 'package:flutter/widgets.dart';
2022-09-13 19:19:52 +02:00
import 'package:flutter_gen/gen_l10n/S.dart';
2022-10-07 18:55:47 +02:00
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
2020-10-17 13:06:11 +02:00
import 'package:git_touch/models/auth.dart';
import 'package:git_touch/models/gitee.dart';
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';
import 'package:git_touch/widgets/mutation_button.dart';
2020-10-17 13:06:11 +02:00
import 'package:git_touch/widgets/repo_header.dart';
2022-09-20 20:00:03 +02:00
import 'package:go_router/go_router.dart';
2022-09-13 19:19:52 +02:00
import 'package:http/http.dart' as http;
2020-10-17 13:06:11 +02:00
import 'package:provider/provider.dart';
import 'package:tuple/tuple.dart';
class StatusPayload {
2022-09-21 18:28:21 +02:00
StatusPayload(this.isWatching, this.isStarred);
bool isWatching;
bool isStarred;
}
2020-10-17 13:06:11 +02:00
class GeRepoScreen extends StatelessWidget {
2022-09-21 18:28:21 +02:00
const GeRepoScreen(this.owner, this.name, {this.branch});
2020-10-17 13:06:11 +02:00
final String owner;
final String name;
2021-05-16 09:16:35 +02:00
final String? branch;
2020-10-17 13:06:11 +02:00
@override
Widget build(BuildContext context) {
return RefreshStatefulScaffold<
Tuple4<GiteeRepo, MarkdownViewData, List<GiteeBranch>, StatusPayload>>(
2021-05-16 09:16:35 +02: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);
});
2022-09-06 18:28:12 +02:00
md() => auth.fetchGitee('/repos/$owner/$name/readme').then((v) {
2022-09-13 19:19:52 +02:00
return (v['content'] as String?)?.base64ToUtf8 ?? '';
});
2022-09-06 18:28:12 +02:00
html() => md().then((v) async {
2020-11-08 08:00:13 +01:00
final res = await http.post(
2021-05-16 09:16:35 +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);
final branches =
await auth.fetchGitee('/repos/$owner/$name/branches').then((v) {
return [for (var branch in v) GiteeBranch.fromJson(branch)];
});
2022-09-24 07:41:46 +02:00
final isStarred = await auth
.fetchGitee('/user/starred/$owner/$name', requestType: 'NO CONTENT')
.then((v) => v.statusCode == HttpStatus.noContent);
2022-09-24 07:41:46 +02:00
final isWatching = await auth
.fetchGitee('/user/subscriptions/$owner/$name',
requestType: 'NO CONTENT')
.then((v) => v.statusCode == HttpStatus.noContent);
2022-09-24 07:41:46 +02:00
final 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;
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-05-16 09:16:35 +02: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 {
2022-09-24 07:41:46 +02:00
final 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);
},
),
2022-09-06 18:28:12 +02:00
const 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);
},
)
])
]),
2020-10-17 13:06:11 +02:00
Row(
children: <Widget>[
EntryItem(
2022-10-04 14:18:04 +02:00
count: p.watchersCount!,
2020-10-17 13:06:11 +02:00
text: 'Watchers',
url: '/gitee/$owner/$name/watchers',
),
EntryItem(
2022-10-04 14:18:04 +02:00
count: p.stargazersCount!,
2020-10-17 13:06:11 +02:00
text: 'Stars',
url: '/gitee/$owner/$name/stargazers',
),
EntryItem(
2022-10-04 14:18:04 +02:00
count: p.forksCount!,
2020-10-17 13:06:11 +02:00
text: 'Forks',
url: '/gitee/$owner/$name/forks',
),
],
),
CommonStyle.border,
2022-09-20 20:00:03 +02:00
AntList(
2022-09-22 17:37:06 +02:00
children: [
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.code),
2022-09-13 19:19:52 +02:00
extra: Text(p.license ?? ''),
2022-09-20 20:00:03 +02:00
onClick: () {
context.push(
'/gitee/$owner/$name/tree/${branch ?? p.defaultBranch}');
},
2022-09-22 17:37:06 +02:00
child: const Text('Code'),
2020-10-17 13:06:11 +02:00
),
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.issue_opened),
2022-09-13 19:19:52 +02:00
extra: Text(numberFormat.format(p.openIssuesCount)),
2022-09-20 20:00:03 +02:00
onClick: () {
context.push('/gitee/$owner/$name/issues');
},
2022-09-22 17:37:06 +02:00
child: const Text('Issues'),
2020-10-17 13:06:11 +02:00
),
2021-05-16 09:16:35 +02:00
if (p.pullRequestsEnabled!)
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.git_pull_request),
2022-09-13 19:19:52 +02:00
child: const Text('Pull requests'),
2022-09-20 20:00:03 +02:00
onClick: () {
context.push('/gitee/$owner/$name/pulls');
},
2020-10-17 13:06:11 +02:00
),
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.history),
2022-09-13 19:19:52 +02:00
child: const Text('Commits'),
2022-09-20 20:00:03 +02:00
onClick: () {
context.push(
'/gitee/$owner/$name/commits?branch=${branch ?? p.defaultBranch}');
},
2020-10-17 13:06:11 +02:00
),
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.git_branch),
2022-09-13 19:19:52 +02:00
extra: Text(
'${(branch ?? p.defaultBranch)!}${branches.length}'),
onClick: () async {
2021-06-13 19:23:16 +02:00
if (branches.length < 2) return;
2021-06-13 19:23:16 +02:00
await theme.showPicker(
context,
PickerGroupItem(
value: branch,
items: branches
.map((b) => PickerItem(b.name, text: b.name))
.toList(),
onClose: (ref) {
if (ref != branch) {
2022-09-22 19:50:45 +02:00
context.pushUrl('/gitee/$owner/$name?branch=$ref',
2021-06-13 19:23:16 +02:00
replace: true);
}
},
),
);
},
2022-09-22 17:37:06 +02:00
child: Text(AppLocalizations.of(context)!.branches),
2021-06-13 19:23:16 +02:00
),
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.organization),
child: const Text('Contributors'),
onClick: () {
context.push('/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
],
);
},
);
}
}