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

130 lines
4.8 KiB
Dart
Raw Normal View History

2022-10-03 12:21:22 +02:00
import 'dart:convert';
2020-02-02 16:06:54 +01:00
2022-10-03 12:21:22 +02:00
import 'package:antd_mobile/antd_mobile.dart';
2020-02-02 11:58:05 +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';
2022-10-07 18:55:47 +02:00
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
2020-02-02 11:58:05 +01:00
import 'package:git_touch/models/auth.dart';
import 'package:git_touch/models/bitbucket.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/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';
2020-02-02 11:58:05 +01:00
import 'package:provider/provider.dart';
import 'package:tuple/tuple.dart';
class BbRepoScreen extends StatelessWidget {
2022-09-21 18:28:21 +02:00
const BbRepoScreen(this.owner, this.name, {this.branch});
2020-02-02 11:58:05 +01:00
final String owner;
final String name;
2021-05-16 09:16:35 +02:00
final String? branch;
2020-02-02 11:58:05 +01:00
@override
Widget build(BuildContext context) {
2021-05-16 09:16:35 +02:00
return RefreshStatefulScaffold<Tuple3<BbRepo, String?, List<BbBranch>>>(
2022-10-07 19:06:03 +02:00
title: Text(AppLocalizations.of(context)!.repository),
fetch: () async {
2020-10-04 14:37:23 +02:00
final auth = context.read<AuthModel>();
2020-02-02 12:50:00 +01:00
final r = await auth.fetchBbJson('/repositories/$owner/$name');
final repo = BbRepo.fromJson(r);
final res = await auth.fetchBb(
2021-05-16 09:16:35 +02:00
'/repositories/$owner/$name/src/${repo.mainbranch!.name}/README.md');
2020-02-02 16:06:54 +01:00
final readme =
res.statusCode >= 400 ? null : utf8.decode(res.bodyBytes);
final branches = await auth
.fetchBbWithPage('/repositories/$owner/$name/refs/branches')
.then((v) {
2021-06-13 20:13:11 +02:00
return [for (var branch in v.items) BbBranch.fromJson(branch)];
});
return Tuple3(repo, readme, branches);
2020-02-02 11:58:05 +01:00
},
2021-01-31 08:49:28 +01:00
bodyBuilder: (t, _) {
2020-02-02 11:58:05 +01:00
final theme = Provider.of<ThemeModel>(context);
final p = t.item1;
final branches = t.item3;
2020-02-02 11:58:05 +01:00
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
RepoHeader(
avatarUrl: p.avatarUrl,
avatarLink: null,
2020-02-02 12:06:48 +01:00
owner: p.ownerLogin,
2020-02-02 11:58:05 +01:00
name: p.slug,
description: p.description,
homepageUrl: p.website,
),
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)),
2022-09-20 20:00:03 +02:00
onClick: () {
context.push(
'/bitbucket/$owner/$name/src/${branch ?? p.mainbranch!.name}');
},
2022-09-22 17:37:06 +02:00
child: const Text('Code'),
2020-02-02 11:58: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('/bitbucket/$owner/$name/issues');
},
),
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('/bitbucket/$owner/$name/pulls');
},
),
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(
'/bitbucket/$owner/$name/commits/${branch ?? p.mainbranch!.name}');
},
2020-02-02 11:58: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 ?? p.mainbranch!.name)!}${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(
'/bitbucket/$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
),
2020-02-02 11:58:05 +01:00
],
),
CommonStyle.verticalGap,
2020-11-08 08:00:13 +01:00
if (t.item2 != null) MarkdownFlutterView(t.item2),
2020-02-02 11:58:05 +01:00
CommonStyle.verticalGap,
],
);
},
);
}
}