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';
|
2020-02-07 14:44:27 +01:00
|
|
|
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';
|
2020-02-07 14:44:27 +01:00
|
|
|
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;
|
2020-01-31 10:55:12 +01:00
|
|
|
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) {
|
2020-02-07 14:44:27 +01:00
|
|
|
return RefreshStatefulScaffold<RepositoryContents>(
|
2020-01-31 04:50:41 +01:00
|
|
|
// canRefresh: !_isImage, // TODO:
|
|
|
|
title: AppBarTitle(path == null ? 'Files' : path),
|
2020-10-06 14:52:40 +02:00
|
|
|
fetch: () async {
|
2020-01-31 10:55:12 +01:00
|
|
|
// Do not request again for images
|
|
|
|
if (path != null &&
|
|
|
|
raw != null &&
|
2020-02-07 14:44:27 +01:00
|
|
|
['png', 'jpg', 'jpeg', 'gif', 'webp'].contains(path.ext)) {
|
|
|
|
return RepositoryContents(
|
|
|
|
file: GitHubFile(downloadUrl: raw, content: ''),
|
|
|
|
);
|
|
|
|
}
|
2020-01-31 10:55:12 +01:00
|
|
|
|
2020-01-31 07:09:00 +01:00
|
|
|
final suffix = path == null ? '' : '/$path';
|
2020-10-04 14:37:23 +02:00
|
|
|
final res = await context
|
|
|
|
.read<AuthModel>()
|
|
|
|
.ghClient
|
|
|
|
.repositories
|
2020-02-07 14:44:27 +01:00
|
|
|
.getContents(RepositorySlug(owner, name), suffix, ref: ref);
|
|
|
|
if (res.isDirectory) {
|
|
|
|
res.tree.sort((a, b) {
|
|
|
|
return sortByKey('dir', a.type, b.type);
|
|
|
|
});
|
|
|
|
}
|
2020-01-31 07:09:00 +01:00
|
|
|
return res;
|
2019-08-30 08:08:09 +02:00
|
|
|
},
|
2019-11-02 13:54:23 +01:00
|
|
|
actionBuilder: (data, _) {
|
2020-02-07 14:44:27 +01:00
|
|
|
if (data.isFile) {
|
2020-01-31 07:09:00 +01:00
|
|
|
return ActionEntry(
|
|
|
|
iconData: Icons.settings,
|
2020-01-31 09:54:01 +01:00
|
|
|
url: '/choose-code-theme',
|
2020-01-31 07:09:00 +01:00
|
|
|
);
|
2019-09-15 11:36:09 +02:00
|
|
|
}
|
|
|
|
},
|
2019-11-02 13:54:23 +01:00
|
|
|
bodyBuilder: (data, _) {
|
2020-02-07 14:44:27 +01:00
|
|
|
if (data.isDirectory) {
|
2020-01-31 07:09:00 +01:00
|
|
|
return ObjectTree(
|
2020-02-07 14:44:27 +01:00
|
|
|
items: data.tree.map((v) {
|
2020-01-31 07:09:00 +01:00
|
|
|
// if (item.type == 'commit') return null;
|
2020-01-31 10:55:12 +01:00
|
|
|
final uri = Uri(
|
2020-05-12 17:31:30 +02:00
|
|
|
path: '/github/$owner/$name/blob/$ref',
|
2020-01-31 10:55:12 +01:00
|
|
|
queryParameters: {
|
|
|
|
'path': v.path,
|
|
|
|
...(v.downloadUrl == null ? {} : {'raw': v.downloadUrl}),
|
|
|
|
},
|
|
|
|
).toString();
|
2020-01-31 07:09:00 +01:00
|
|
|
return ObjectTreeItem(
|
|
|
|
name: v.name,
|
|
|
|
type: v.type,
|
2020-01-31 10:55:12 +01:00
|
|
|
url: uri.toString(),
|
2020-01-31 09:40:20 +01:00
|
|
|
downloadUrl: v.downloadUrl,
|
2020-01-31 07:09:00 +01:00
|
|
|
size: v.type == 'file' ? v.size : null,
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// TODO: Markdown base path
|
|
|
|
// basePaths: [owner, name, branch, ...paths]
|
|
|
|
return BlobView(
|
|
|
|
path,
|
2020-02-07 14:44:27 +01:00
|
|
|
text: data.file.text,
|
|
|
|
networkUrl: data.file.downloadUrl,
|
2020-01-31 07:09:00 +01:00
|
|
|
);
|
2019-08-30 08:23:58 +02:00
|
|
|
}
|
2019-08-30 08:08:09 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|