2019-09-15 11:36:09 +02:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2020-01-31 07:09:00 +01:00
|
|
|
import 'package:git_touch/models/github.dart';
|
2019-09-25 11:06:36 +02:00
|
|
|
import 'package:git_touch/scaffolds/refresh_stateful.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';
|
2019-09-08 14:07:35 +02:00
|
|
|
import 'package:provider/provider.dart';
|
2019-08-30 08:08:09 +02:00
|
|
|
import 'package:git_touch/utils/utils.dart';
|
|
|
|
|
|
|
|
class ObjectScreen extends StatelessWidget {
|
|
|
|
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;
|
|
|
|
ObjectScreen(this.owner, this.name, this.ref, {this.path, this.raw});
|
2019-08-30 08:08:09 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-01-31 07:09:00 +01:00
|
|
|
return RefreshStatefulScaffold(
|
2020-01-31 04:50:41 +01:00
|
|
|
// canRefresh: !_isImage, // TODO:
|
|
|
|
title: AppBarTitle(path == null ? 'Files' : path),
|
2019-09-30 11:13:12 +02:00
|
|
|
fetchData: () async {
|
2020-01-31 10:55:12 +01:00
|
|
|
// Do not request again for images
|
|
|
|
if (path != null &&
|
|
|
|
raw != null &&
|
|
|
|
['png', 'jpg', 'jpeg', 'gif', 'webp'].contains(path.ext))
|
|
|
|
return {'download_url': raw};
|
|
|
|
|
2020-01-31 07:09:00 +01:00
|
|
|
final suffix = path == null ? '' : '/$path';
|
2020-01-31 07:54:46 +01:00
|
|
|
final res = await Provider.of<AuthModel>(context)
|
|
|
|
.getWithCredentials('/repos/$owner/$name/contents$suffix?ref=$ref');
|
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-01-31 07:09:00 +01:00
|
|
|
if (data is Map) {
|
|
|
|
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-01-31 07:09:00 +01:00
|
|
|
if (data is List) {
|
|
|
|
final items = data.map((t) => GithubTreeItem.fromJson(t)).toList();
|
|
|
|
items.sort((a, b) {
|
|
|
|
return sortByKey('dir', a.type, b.type);
|
|
|
|
});
|
|
|
|
return ObjectTree(
|
|
|
|
items: items.map((v) {
|
|
|
|
// if (item.type == 'commit') return null;
|
2020-01-31 10:55:12 +01:00
|
|
|
final uri = Uri(
|
|
|
|
path: '/$owner/$name/blob/$ref',
|
|
|
|
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]
|
|
|
|
final v = GithubTreeItem.fromJson(data);
|
|
|
|
return BlobView(
|
|
|
|
path,
|
2020-01-31 07:31:24 +01:00
|
|
|
base64Text: v.content?.dropLineBreak,
|
2020-01-31 07:09:00 +01:00
|
|
|
networkUrl: v.downloadUrl,
|
|
|
|
);
|
2019-08-30 08:23:58 +02:00
|
|
|
}
|
2019-08-30 08:08:09 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|