1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-24 06:42:07 +01:00
git-touch-android-ios-app/lib/screens/bb_object.dart

73 lines
2.3 KiB
Dart
Raw Normal View History

2020-02-02 12:50:00 +01:00
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:git_touch/models/auth.dart';
import 'package:git_touch/models/bitbucket.dart';
import 'package:git_touch/scaffolds/refresh_stateful.dart';
import 'package:git_touch/utils/utils.dart';
import 'package:git_touch/widgets/action_entry.dart';
import 'package:git_touch/widgets/app_bar_title.dart';
import 'package:git_touch/widgets/blob_view.dart';
import 'package:git_touch/widgets/object_tree.dart';
import 'package:provider/provider.dart';
import 'package:path/path.dart' as p;
class BbObjectScreen extends StatelessWidget {
final String owner;
final String name;
final String ref;
final String path;
BbObjectScreen(this.owner, this.name, this.ref, {this.path});
@override
Widget build(BuildContext context) {
2020-02-02 16:02:58 +01:00
final auth = Provider.of<AuthModel>(context);
2020-02-02 12:50:00 +01:00
return RefreshStatefulScaffold(
title: AppBarTitle(path ?? 'Files'),
fetchData: () async {
2020-02-02 16:02:58 +01:00
final res = await auth
2020-02-02 12:50:00 +01:00
.fetchBb('/repositories/$owner/$name/src/$ref/${path ?? ''}');
if (res.headers[HttpHeaders.contentTypeHeader] == 'text/plain') {
2020-02-02 16:06:54 +01:00
return utf8.decode(res.bodyBytes);
2020-02-02 12:50:00 +01:00
} else {
2020-02-02 16:06:54 +01:00
return BbPagination.fromJson(json.decode(utf8.decode(res.bodyBytes)))
.values;
2020-02-02 12:50:00 +01:00
}
},
actionBuilder: (p, _) {
if (p is String) {
return ActionEntry(
iconData: Icons.settings,
url: '/choose-code-theme',
);
} else {
return null;
}
},
bodyBuilder: (pl, _) {
if (pl is String) {
return BlobView(path, text: pl);
} else if (pl is List) {
final items = pl.map((t) => BbTree.fromJson(t)).toList();
items.sort((a, b) {
return sortByKey('dir', a.type, b.type);
});
return ObjectTree(items: [
for (var v in items)
ObjectTreeItem(
name: p.basename(v.path),
type: v.type,
size: v.type == 'commit_file' ? v.size : null,
url:
'/bitbucket/$owner/$name/src/$ref?path=${v.path.urlencode}',
2020-02-02 16:02:58 +01:00
downloadUrl: v.links['self']['href'] as String,
2020-02-02 12:50:00 +01:00
),
]);
} else {
return null;
}
},
);
}
}