1
0
mirror of https://github.com/git-touch/git-touch synced 2025-02-15 19:10:45 +01:00

94 lines
2.9 KiB
Dart
Raw Normal View History

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