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

85 lines
2.8 KiB
Dart
Raw Normal View History

2019-09-15 11:36:09 +02:00
import 'package:flutter/cupertino.dart';
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';
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(
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 {
// Do not request again for images
if (path != null &&
raw != null &&
['png', 'jpg', 'jpeg', 'gif', 'webp'].contains(path.ext))
return {'download_url': raw};
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');
return res;
2019-08-30 08:08:09 +02:00
},
2019-11-02 13:54:23 +01:00
actionBuilder: (data, _) {
if (data is Map) {
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 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;
final uri = Uri(
path: '/$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]
final v = GithubTreeItem.fromJson(data);
return BlobView(
path,
2020-01-31 07:31:24 +01:00
base64Text: v.content?.dropLineBreak,
networkUrl: v.downloadUrl,
);
2019-08-30 08:23:58 +02:00
}
2019-08-30 08:08:09 +02:00
},
);
}
}