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

157 lines
5.7 KiB
Dart
Raw Normal View History

2022-10-03 12:21:22 +02:00
import 'dart:convert';
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';
2021-01-23 15:08:05 +01:00
import 'package:git_touch/models/auth.dart';
import 'package:git_touch/models/gogs.dart';
import 'package:git_touch/models/theme.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;
2021-01-23 15:08:05 +01:00
import 'package:provider/provider.dart';
import 'package:tuple/tuple.dart';
class GoRepoScreen extends StatelessWidget {
2022-09-21 18:28:21 +02:00
const GoRepoScreen(this.owner, this.name, {this.branch});
2021-01-23 15:08:05 +01:00
final String owner;
final String name;
2021-05-16 09:16:35 +02:00
final String? branch;
2021-01-23 15:08:05 +01:00
@override
Widget build(BuildContext context) {
return RefreshStatefulScaffold<
Tuple3<GogsRepository, MarkdownViewData, List<GogsBranch>>>(
2021-05-16 09:16:35 +02:00
title: AppBarTitle(AppLocalizations.of(context)!.repository),
2021-01-23 15:08:05 +01:00
fetch: () async {
final auth = context.read<AuthModel>();
final repo = await auth.fetchGogs('/repos/$owner/$name').then((v) {
return GogsRepository.fromJson(v);
});
2022-09-06 18:28:12 +02:00
md() =>
2021-01-23 15:08:05 +01:00
auth.fetchGogs('/repos/$owner/$name/contents/README.md').then((v) {
2021-05-16 09:16:35 +02:00
return (v['content'] as String?)?.base64ToUtf8 ?? '';
2021-01-23 15:08:05 +01:00
});
2022-09-06 18:28:12 +02:00
html() => md().then((v) async {
2021-01-23 15:08:05 +01:00
final res = await http.post(
2021-05-16 09:16:35 +02:00
Uri.parse('${auth.activeAccount!.domain}/api/v1/markdown/raw'),
2021-01-23 15:08:05 +01:00
headers: {'Authorization': 'token ${auth.token}'},
body: v,
);
return utf8.decode(res.bodyBytes).normalizedHtml;
});
final readmeData = MarkdownViewData(context, md: md, html: html);
2022-09-24 07:41:46 +02:00
final branches =
2021-01-23 15:08:05 +01:00
await auth.fetchGogs('/repos/$owner/$name/branches').then((v) {
2022-09-24 07:41:46 +02:00
return [
for (var branch in (v is List ? v : [])) GogsBranch.fromJson(branch)
]; // Valid API Response only returned if repo contains >= 2 branches
2021-01-23 15:08:05 +01:00
});
return Tuple3(repo, readmeData, branches);
},
2021-01-31 08:49:28 +01:00
bodyBuilder: (t, _) {
2021-01-23 15:08:05 +01:00
final p = t.item1;
final branches = t.item3;
final theme = context.read<ThemeModel>();
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
RepoHeader(
2021-05-16 09:16:35 +02:00
avatarUrl: p.owner!.avatarUrl,
avatarLink: '/gogs/${p.owner!.username}',
owner: p.owner!.username,
name: p.fullName!.split('/')[1],
2021-01-23 15:08:05 +01:00
description: p.description,
homepageUrl: p.website,
),
CommonStyle.border,
Row(
children: <Widget>[
// TODO: when API is available
EntryItem(
2022-10-04 14:18:04 +02:00
count: p.watchersCount!,
2021-01-23 15:08:05 +01:00
text: 'Watchers',
),
EntryItem(
2022-10-04 14:18:04 +02:00
count: p.starsCount!,
2021-01-23 15:08:05 +01:00
text: 'Stars',
),
EntryItem(
2022-10-04 14:18:04 +02:00
count: p.forksCount!,
2021-01-23 15:08:05 +01:00
text: '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
child: const Text('Code'),
2022-09-20 20:00:03 +02:00
onClick: () {
context.push(
'/gogs/$owner/$name/blob?ref=${branch ?? 'master'}');
},
2021-01-23 15:08:05 +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
child: const Text('Issues'),
2022-09-20 20:00:03 +02:00
onClick: () {
context.push('/gogs/$owner/$name/issues');
},
2021-01-23 15:08:05 +01:00
),
2022-09-22 17:37:06 +02:00
const AntListItem(
prefix: Icon(Octicons.git_pull_request),
child: Text(
2021-01-23 15:08:05 +01:00
'Pull requests'), // TODO: when API endpoint is available
),
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(
'/gogs/$owner/$name/commits?ref=${branch ?? 'master'}');
},
2021-01-23 15:08:05 +01: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 ?? 'master'}${branches.length.toString()}'),
onClick: () async {
2021-01-23 15:08:05 +01: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('/gogs/$owner/$name?branch=$ref',
2021-01-23 15:08:05 +01:00
replace: true);
}
},
),
);
},
2022-09-22 17:37:06 +02:00
child: Text(AppLocalizations.of(context)!.branches),
2021-01-23 15:08:05 +01:00
),
],
),
CommonStyle.verticalGap,
MarkdownView(t.item2),
],
);
},
);
}
}