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

91 lines
2.8 KiB
Dart
Raw Normal View History

2019-09-15 11:36:09 +02:00
import 'package:flutter/cupertino.dart';
2019-09-25 11:06:36 +02:00
import 'package:git_touch/scaffolds/refresh_stateful.dart';
import 'package:git_touch/utils/utils.dart';
2019-09-28 18:25:14 +02:00
import 'package:git_touch/widgets/action_entry.dart';
2019-09-11 13:59:47 +02:00
import 'package:git_touch/widgets/app_bar_title.dart';
2020-01-30 07:43:58 +01:00
import 'package:git_touch/widgets/blob_view.dart';
2020-01-30 05:55:55 +01:00
import 'package:git_touch/widgets/object_tree.dart';
2019-08-30 08:08:09 +02:00
import 'package:flutter/material.dart';
2019-09-27 14:52:38 +02:00
import 'package:git_touch/models/auth.dart';
import 'package:github/github.dart';
2019-09-08 14:07:35 +02:00
import 'package:provider/provider.dart';
2019-08-30 08:08:09 +02:00
2020-02-07 07:17:05 +01:00
class GhObjectScreen extends StatelessWidget {
2019-08-30 08:08:09 +02:00
final String owner;
final String name;
2020-01-31 07:54:46 +01:00
final String ref;
2020-01-31 04:50:41 +01:00
final String path;
final String raw;
2020-02-07 07:17:05 +01:00
GhObjectScreen(this.owner, this.name, this.ref, {this.path, this.raw});
2019-08-30 08:08:09 +02:00
@override
Widget build(BuildContext context) {
return RefreshStatefulScaffold<RepositoryContents>(
2020-01-31 04:50:41 +01:00
// canRefresh: !_isImage, // TODO:
title: AppBarTitle(path == null ? 'Files' : path),
fetch: () async {
// Do not request again for images
if (path != null &&
raw != null &&
['png', 'jpg', 'jpeg', 'gif', 'webp'].contains(path.ext)) {
return RepositoryContents(
file: GitHubFile(downloadUrl: raw, content: ''),
);
}
final suffix = path == null ? '' : '/$path';
2020-10-04 14:37:23 +02:00
final res = await context
.read<AuthModel>()
.ghClient
.repositories
.getContents(RepositorySlug(owner, name), suffix, ref: ref);
if (res.isDirectory) {
res.tree.sort((a, b) {
return sortByKey('dir', a.type, b.type);
});
}
return res;
2019-08-30 08:08:09 +02:00
},
2019-11-02 13:54:23 +01:00
actionBuilder: (data, _) {
if (data.isFile) {
return ActionEntry(
iconData: Icons.settings,
2020-01-31 09:54:01 +01:00
url: '/choose-code-theme',
);
2019-09-15 11:36:09 +02:00
}
},
2019-11-02 13:54:23 +01:00
bodyBuilder: (data, _) {
if (data.isDirectory) {
return ObjectTree(
items: data.tree.map((v) {
// if (item.type == 'commit') return null;
final uri = Uri(
path: '/github/$owner/$name/blob/$ref',
queryParameters: {
'path': v.path,
...(v.downloadUrl == null ? {} : {'raw': v.downloadUrl}),
},
).toString();
return ObjectTreeItem(
name: v.name,
type: v.type,
url: uri.toString(),
2020-01-31 09:40:20 +01:00
downloadUrl: v.downloadUrl,
size: v.type == 'file' ? v.size : null,
);
}),
);
} else {
// TODO: Markdown base path
// basePaths: [owner, name, branch, ...paths]
return BlobView(
path,
text: data.file.text,
networkUrl: data.file.downloadUrl,
);
2019-08-30 08:23:58 +02:00
}
2019-08-30 08:08:09 +02:00
},
);
}
}