mirror of
https://github.com/git-touch/git-touch
synced 2025-01-31 08:04:51 +01:00
refactor(github): use rest api for download_url
This commit is contained in:
parent
16925f8e1a
commit
05a4ffeeb8
@ -235,3 +235,16 @@ class GithubTrendingUserRepo {
|
||||
factory GithubTrendingUserRepo.fromJson(Map<String, dynamic> 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);
|
||||
}
|
||||
|
@ -278,3 +278,23 @@ Map<String, dynamic> _$GithubTrendingUserRepoToJson(
|
||||
'name': instance.name,
|
||||
'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,
|
||||
};
|
||||
|
@ -1,5 +1,5 @@
|
||||
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/scaffolds/refresh_stateful.dart';
|
||||
import 'package:git_touch/widgets/action_entry.dart';
|
||||
@ -28,86 +28,69 @@ class ObjectScreen extends StatelessWidget {
|
||||
final String path;
|
||||
ObjectScreen(this.owner, this.name, this.branch, {this.path});
|
||||
|
||||
String get _pathNotNull => path ?? '';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return RefreshStatefulScaffold<GhObjectGitObject>(
|
||||
return RefreshStatefulScaffold(
|
||||
// canRefresh: !_isImage, // TODO:
|
||||
title: AppBarTitle(path == null ? 'Files' : path),
|
||||
fetchData: () async {
|
||||
final res = await Provider.of<AuthModel>(context)
|
||||
.gqlClient
|
||||
.execute(GhObjectQuery(
|
||||
variables: GhObjectArguments(
|
||||
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;
|
||||
final suffix = path == null ? '' : '/$path';
|
||||
final res = await Provider.of<AuthModel>(context).getWithCredentials(
|
||||
'/repos/$owner/$name/contents$suffix?ref=$branch');
|
||||
return res;
|
||||
},
|
||||
actionBuilder: (data, _) {
|
||||
switch (data.resolveType) {
|
||||
case 'Blob':
|
||||
final blob = data as GhObjectBlob;
|
||||
final theme = Provider.of<ThemeModel>(context);
|
||||
return ActionEntry(
|
||||
iconData: Icons.settings,
|
||||
onTap: () {
|
||||
theme.push(context, '/choose-code-theme');
|
||||
},
|
||||
);
|
||||
default:
|
||||
return null;
|
||||
if (data is Map) {
|
||||
final theme = Provider.of<ThemeModel>(context);
|
||||
return ActionEntry(
|
||||
iconData: Icons.settings,
|
||||
onTap: () {
|
||||
theme.push(context, '/choose-code-theme');
|
||||
},
|
||||
);
|
||||
}
|
||||
},
|
||||
bodyBuilder: (data, _) {
|
||||
switch (data.resolveType) {
|
||||
case 'Tree':
|
||||
return ObjectTree(
|
||||
items: (data as GhObjectTree).entries.map((v) {
|
||||
// if (item.type == 'commit') return null;
|
||||
String url;
|
||||
var ext = p.extension(v.name);
|
||||
if (ext.startsWith('.')) ext = ext.substring(1);
|
||||
if (['pdf', 'docx', 'doc', 'pptx', 'ppt', 'xlsx', 'xls']
|
||||
.contains(ext)) {
|
||||
// Let system browser handle these files
|
||||
url = Uri.encodeFull(
|
||||
'https://raw.githubusercontent.com/$owner/$name/$branch/$path');
|
||||
} else {
|
||||
url =
|
||||
'/$owner/$name/blob/$branch?path=${p.join(_pathNotNull, v.name).urlencode}';
|
||||
}
|
||||
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;
|
||||
String url;
|
||||
var ext = p.extension(v.name);
|
||||
if (ext.startsWith('.')) ext = ext.substring(1);
|
||||
if (['pdf', 'docx', 'doc', 'pptx', 'ppt', 'xlsx', 'xls']
|
||||
.contains(ext)) {
|
||||
// Let system browser handle these files
|
||||
//
|
||||
// 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(
|
||||
name: v.name,
|
||||
type: v.type,
|
||||
url: url,
|
||||
size: v.object.resolveType == 'Blob'
|
||||
? (v.object as GhObjectBlob).byteSize
|
||||
: null,
|
||||
);
|
||||
}),
|
||||
);
|
||||
case 'Blob':
|
||||
// TODO: Markdown base path
|
||||
// basePaths: [owner, name, branch, ...paths]
|
||||
return BlobView(
|
||||
path,
|
||||
text: (data as GhObjectBlob).text,
|
||||
networkUrl: Uri.encodeFull(
|
||||
'https://raw.githubusercontent.com/$owner/$name/$branch/$path'), // TODO: private
|
||||
);
|
||||
default:
|
||||
return null;
|
||||
return ObjectTreeItem(
|
||||
name: v.name,
|
||||
type: v.type,
|
||||
url: url,
|
||||
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,
|
||||
base64Text: v.content,
|
||||
networkUrl: v.downloadUrl,
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
@ -24,11 +24,11 @@ class ObjectTree extends StatelessWidget {
|
||||
|
||||
Widget _buildIcon(ObjectTreeItem item) {
|
||||
switch (item.type) {
|
||||
case 'blob':
|
||||
case 'file': // Gitea
|
||||
case 'blob': // github gql
|
||||
case 'file': // github rest, gitea
|
||||
return SetiIcon(item.name, size: 36);
|
||||
case 'tree':
|
||||
case 'dir': // Gitea
|
||||
case 'tree': // github gql
|
||||
case 'dir': // github rest, gitea
|
||||
return Icon(
|
||||
Octicons.file_directory,
|
||||
color: PrimerColors.blue300,
|
||||
|
Loading…
x
Reference in New Issue
Block a user