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

125 lines
4.6 KiB
Dart
Raw Normal View History

2020-02-02 16:06:54 +01:00
import 'dart:convert';
2020-02-02 11:58:05 +01:00
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
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/app_bar_title.dart';
import 'package:git_touch/widgets/markdown_view.dart';
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';
import '../generated/l10n.dart';
2020-02-02 11:58:05 +01:00
class BbRepoScreen extends StatelessWidget {
final String owner;
final String name;
final String branch;
BbRepoScreen(this.owner, this.name, {this.branch});
2020-02-02 11:58:05 +01:00
@override
Widget build(BuildContext context) {
return RefreshStatefulScaffold<Tuple3<BbRepo, String, List<BbBranch>>>(
title: AppBarTitle(S.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(
2020-02-02 11:58:05 +01: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) {
return [for (var branch in v.data) BbBranch.fromJson(branch)];
});
return Tuple3(repo, readme, branches);
2020-02-02 11:58:05 +01:00
},
bodyBuilder: (t, setState) {
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,
TableView(
hasIcon: true,
items: [
TableViewItem(
leftIconData: Octicons.code,
text: Text('Code'),
rightWidget: Text(filesize(p.size)),
url:
'/bitbucket/$owner/$name/src/${branch == null ? p.mainbranch.name : branch}',
2020-02-02 11:58:05 +01:00
),
TableViewItem(
leftIconData: Octicons.issue_opened,
text: Text('Issues'),
url: '/bitbucket/$owner/$name/issues',
),
TableViewItem(
leftIconData: Octicons.git_pull_request,
text: Text('Pull requests'),
url: '/bitbucket/$owner/$name/pulls',
),
2020-02-02 11:58:05 +01:00
TableViewItem(
leftIconData: Octicons.history,
text: Text('Commits'),
url:
'/bitbucket/$owner/$name/commits/${branch == null ? p.mainbranch.name : branch}',
2020-02-02 11:58:05 +01:00
),
if (branches != null)
TableViewItem(
leftIconData: Octicons.git_branch,
text: Text(S.of(context).branches),
rightWidget: Text(
(branch == null ? p.mainbranch.name : 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,
'/bitbucket/$owner/$name?branch=$ref',
replace: true);
}
},
),
);
},
),
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,
],
);
},
);
}
}