refactor(github): use rest api for download_url

This commit is contained in:
Rongjian Zhang 2020-01-31 14:09:00 +08:00
parent 16925f8e1a
commit 05a4ffeeb8
4 changed files with 90 additions and 74 deletions

View File

@ -235,3 +235,16 @@ class GithubTrendingUserRepo {
factory GithubTrendingUserRepo.fromJson(Map<String, dynamic> json) => factory GithubTrendingUserRepo.fromJson(Map<String, dynamic> json) =>
_$GithubTrendingUserRepoFromJson(json); _$GithubTrendingUserRepoFromJson(json);
} }
@JsonSerializable(fieldRename: FieldRename.snake)
class GithubTreeItem {
String name;
String path;
int size;
String type;
String downloadUrl;
String content;
GithubTreeItem();
factory GithubTreeItem.fromJson(Map<String, dynamic> json) =>
_$GithubTreeItemFromJson(json);
}

View File

@ -278,3 +278,23 @@ Map<String, dynamic> _$GithubTrendingUserRepoToJson(
'name': instance.name, 'name': instance.name,
'description': instance.description, 'description': instance.description,
}; };
GithubTreeItem _$GithubTreeItemFromJson(Map<String, dynamic> json) {
return GithubTreeItem()
..name = json['name'] as String
..path = json['path'] as String
..size = json['size'] as int
..type = json['type'] as String
..downloadUrl = json['download_url'] as String
..content = json['content'] as String;
}
Map<String, dynamic> _$GithubTreeItemToJson(GithubTreeItem instance) =>
<String, dynamic>{
'name': instance.name,
'path': instance.path,
'size': instance.size,
'type': instance.type,
'download_url': instance.downloadUrl,
'content': instance.content,
};

View File

@ -1,5 +1,5 @@
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:git_touch/graphql/gh.dart'; import 'package:git_touch/models/github.dart';
import 'package:git_touch/models/theme.dart'; import 'package:git_touch/models/theme.dart';
import 'package:git_touch/scaffolds/refresh_stateful.dart'; import 'package:git_touch/scaffolds/refresh_stateful.dart';
import 'package:git_touch/widgets/action_entry.dart'; import 'package:git_touch/widgets/action_entry.dart';
@ -28,86 +28,69 @@ class ObjectScreen extends StatelessWidget {
final String path; final String path;
ObjectScreen(this.owner, this.name, this.branch, {this.path}); ObjectScreen(this.owner, this.name, this.branch, {this.path});
String get _pathNotNull => path ?? '';
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return RefreshStatefulScaffold<GhObjectGitObject>( return RefreshStatefulScaffold(
// canRefresh: !_isImage, // TODO: // canRefresh: !_isImage, // TODO:
title: AppBarTitle(path == null ? 'Files' : path), title: AppBarTitle(path == null ? 'Files' : path),
fetchData: () async { fetchData: () async {
final res = await Provider.of<AuthModel>(context) final suffix = path == null ? '' : '/$path';
.gqlClient final res = await Provider.of<AuthModel>(context).getWithCredentials(
.execute(GhObjectQuery( '/repos/$owner/$name/contents$suffix?ref=$branch');
variables: GhObjectArguments( return res;
owner: owner,
name: name,
expression: '$branch:$_pathNotNull',
),
));
final data = res.data.repository.object;
if (data.resolveType == 'Tree') {
(data as GhObjectTree).entries.sort((a, b) {
return sortByKey('tree', a.type, b.type);
});
}
return data;
}, },
actionBuilder: (data, _) { actionBuilder: (data, _) {
switch (data.resolveType) { if (data is Map) {
case 'Blob': final theme = Provider.of<ThemeModel>(context);
final blob = data as GhObjectBlob; return ActionEntry(
final theme = Provider.of<ThemeModel>(context); iconData: Icons.settings,
return ActionEntry( onTap: () {
iconData: Icons.settings, theme.push(context, '/choose-code-theme');
onTap: () { },
theme.push(context, '/choose-code-theme'); );
},
);
default:
return null;
} }
}, },
bodyBuilder: (data, _) { bodyBuilder: (data, _) {
switch (data.resolveType) { if (data is List) {
case 'Tree': final items = data.map((t) => GithubTreeItem.fromJson(t)).toList();
return ObjectTree( items.sort((a, b) {
items: (data as GhObjectTree).entries.map((v) { return sortByKey('dir', a.type, b.type);
// if (item.type == 'commit') return null; });
String url; return ObjectTree(
var ext = p.extension(v.name); items: items.map((v) {
if (ext.startsWith('.')) ext = ext.substring(1); // if (item.type == 'commit') return null;
if (['pdf', 'docx', 'doc', 'pptx', 'ppt', 'xlsx', 'xls'] String url;
.contains(ext)) { var ext = p.extension(v.name);
// Let system browser handle these files if (ext.startsWith('.')) ext = ext.substring(1);
url = Uri.encodeFull( if (['pdf', 'docx', 'doc', 'pptx', 'ppt', 'xlsx', 'xls']
'https://raw.githubusercontent.com/$owner/$name/$branch/$path'); .contains(ext)) {
} else { // Let system browser handle these files
url = //
'/$owner/$name/blob/$branch?path=${p.join(_pathNotNull, v.name).urlencode}'; // TODO:
} // Unhandled Exception: PlatformException(Error, Error while launching
// https://github.com/flutter/flutter/issues/49162
url = v.downloadUrl;
} else {
url = '/$owner/$name/blob/$branch?path=${v.path.urlencode}';
}
return ObjectTreeItem( return ObjectTreeItem(
name: v.name, name: v.name,
type: v.type, type: v.type,
url: url, url: url,
size: v.object.resolveType == 'Blob' size: v.type == 'file' ? v.size : null,
? (v.object as GhObjectBlob).byteSize );
: null, }),
); );
}), } else {
); // TODO: Markdown base path
case 'Blob': // basePaths: [owner, name, branch, ...paths]
// TODO: Markdown base path final v = GithubTreeItem.fromJson(data);
// basePaths: [owner, name, branch, ...paths] return BlobView(
return BlobView( path,
path, base64Text: v.content,
text: (data as GhObjectBlob).text, networkUrl: v.downloadUrl,
networkUrl: Uri.encodeFull( );
'https://raw.githubusercontent.com/$owner/$name/$branch/$path'), // TODO: private
);
default:
return null;
} }
}, },
); );

View File

@ -24,11 +24,11 @@ class ObjectTree extends StatelessWidget {
Widget _buildIcon(ObjectTreeItem item) { Widget _buildIcon(ObjectTreeItem item) {
switch (item.type) { switch (item.type) {
case 'blob': case 'blob': // github gql
case 'file': // Gitea case 'file': // github rest, gitea
return SetiIcon(item.name, size: 36); return SetiIcon(item.name, size: 36);
case 'tree': case 'tree': // github gql
case 'dir': // Gitea case 'dir': // github rest, gitea
return Icon( return Icon(
Octicons.file_directory, Octicons.file_directory,
color: PrimerColors.blue300, color: PrimerColors.blue300,