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

128 lines
4.5 KiB
Dart
Raw Normal View History

2022-10-03 12:21:22 +02:00
import 'dart:convert';
2020-11-08 08:00:13 +01:00
2022-10-03 12:21:22 +02:00
import 'package:antd_mobile/antd_mobile.dart';
2020-01-29 11:50:17 +01:00
import 'package:filesize/filesize.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';
2020-01-29 11:50:17 +01:00
import 'package:git_touch/models/auth.dart';
import 'package:git_touch/models/gitea.dart';
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';
import 'package:git_touch/widgets/markdown_view.dart';
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-01-29 11:50:17 +01:00
import 'package:provider/provider.dart';
import 'package:tuple/tuple.dart';
2020-02-07 15:20:06 +01:00
class GtRepoScreen extends StatelessWidget {
2022-09-21 18:28:21 +02:00
const GtRepoScreen(this.owner, this.name);
2020-01-29 11:50:17 +01:00
final String owner;
final String name;
@override
Widget build(BuildContext context) {
2020-11-08 08:00:13 +01:00
return RefreshStatefulScaffold<Tuple2<GiteaRepository, MarkdownViewData>>(
2021-05-16 09:16:35 +02:00
title: AppBarTitle(AppLocalizations.of(context)!.repository),
fetch: () async {
2020-10-04 14:37:23 +02:00
final auth = context.read<AuthModel>();
2020-11-08 08:00:13 +01:00
final repo = await auth.fetchGitea('/repos/$owner/$name').then((v) {
return GiteaRepository.fromJson(v);
});
2022-09-06 18:28:12 +02:00
md() =>
2020-11-08 08:00:13 +01:00
auth.fetchGitea('/repos/$owner/$name/contents/README.md').then((v) {
2021-05-16 09:16:35 +02:00
return (v['content'] as String?)?.base64ToUtf8 ?? '';
2020-11-08 08:00:13 +01:00
});
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/v1/markdown/raw'),
2020-11-08 08:00:13 +01:00
headers: {'Authorization': 'token ${auth.token}'},
body: v,
);
return utf8.decode(res.bodyBytes).normalizedHtml;
});
final readmeData = MarkdownViewData(context, md: md, html: html);
return Tuple2(repo, readmeData);
2020-01-29 11:50:17 +01:00
},
2021-01-31 08:49:28 +01:00
bodyBuilder: (t, _) {
2020-01-29 11:50:17 +01:00
final p = t.item1;
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
RepoHeader(
2021-05-16 09:16:35 +02:00
avatarUrl: p.owner!.avatarUrl,
avatarLink: '/gitea/${p.owner!.login}',
owner: p.owner!.login,
2020-01-29 11:50:17 +01:00
name: p.name,
description: p.description,
homepageUrl: p.website,
),
CommonStyle.border,
Row(
children: <Widget>[
EntryItem(
2022-10-04 14:18:04 +02:00
count: 0, // TODO:
text: 'Watchers',
url: '/gitea/$owner/$name/watchers',
),
2020-01-29 11:50:17 +01:00
EntryItem(
2022-10-04 14:18:04 +02:00
count: p.starsCount!,
2020-01-29 11:50:17 +01:00
text: 'Stars',
url: '/gitea/$owner/$name/stargazers',
2020-01-29 11:50:17 +01:00
),
EntryItem(
2022-10-04 14:18:04 +02:00
count: p.forksCount!,
text: 'Forks',
url: '/gitea/$owner/$name/forks',
2020-01-29 11:50:17 +01:00
),
],
),
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(filesize(p.size! * 1000)),
2022-09-20 20:00:03 +02:00
onClick: () {
context.push('/gitea/$owner/$name/blob');
},
2022-09-22 17:37:06 +02:00
child: const Text('Code'),
2020-01-29 11:50:17 +01: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('/gitea/$owner/$name/issues');
},
2022-09-22 17:37:06 +02:00
child: const Text('Issues'),
2020-01-29 11:50:17 +01:00
),
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.git_pull_request),
2022-09-13 19:19:52 +02:00
extra: Text(numberFormat.format(p.openPrCounter)),
2022-09-20 20:00:03 +02:00
onClick: () {
context.push('/gitea/$owner/$name/pulls');
},
2022-09-22 17:37:06 +02:00
child: const Text('Pull requests'),
2020-01-29 11:50:17 +01: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('/gitea/$owner/$name/commits');
},
2020-01-29 11:50:17 +01:00
),
],
),
CommonStyle.verticalGap,
2020-11-08 08:00:13 +01:00
MarkdownView(t.item2),
2020-01-29 11:50:17 +01:00
],
);
},
);
}
}