1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-18 03:09:36 +01:00

refactor(gh): fetch contents with github lib

This commit is contained in:
Rongjian Zhang 2020-02-07 21:44:27 +08:00
parent a4b54d317c
commit 0ee36143f8
2 changed files with 32 additions and 17 deletions

View File

@ -4,6 +4,7 @@ import 'dart:async';
import 'package:git_touch/models/bitbucket.dart';
import 'package:git_touch/models/gitea.dart';
import 'package:git_touch/utils/request_serilizer.dart';
import 'package:github/github.dart';
import 'package:gql_http_link/gql_http_link.dart';
import 'package:artemis/artemis.dart';
import 'package:fimber/fimber.dart';
@ -328,6 +329,7 @@ class AuthModel with ChangeNotifier {
// https://stackoverflow.com/a/50116077
rootKey = UniqueKey();
activeAccountIndex = index;
_ghClient = null;
_gqlClient = null;
notifyListeners();
}
@ -339,6 +341,15 @@ class AuthModel with ChangeNotifier {
var _timeoutDuration = Duration(seconds: 10);
// var _timeoutDuration = Duration(seconds: 1);
GitHub _ghClient;
GitHub get ghClient {
if (token == null) return null;
if (_ghClient == null) {
_ghClient = GitHub(auth: Authentication.withToken(token));
}
return _ghClient;
}
ArtemisClient _gqlClient;
ArtemisClient get gqlClient {
if (token == null) return null;

View File

@ -1,14 +1,14 @@
import 'package:flutter/cupertino.dart';
import 'package:git_touch/models/github.dart';
import 'package:git_touch/scaffolds/refresh_stateful.dart';
import 'package:git_touch/utils/utils.dart';
import 'package:git_touch/widgets/action_entry.dart';
import 'package:git_touch/widgets/app_bar_title.dart';
import 'package:git_touch/widgets/blob_view.dart';
import 'package:git_touch/widgets/object_tree.dart';
import 'package:flutter/material.dart';
import 'package:git_touch/models/auth.dart';
import 'package:github/github.dart';
import 'package:provider/provider.dart';
import 'package:git_touch/utils/utils.dart';
class GhObjectScreen extends StatelessWidget {
final String owner;
@ -20,23 +20,32 @@ class GhObjectScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return RefreshStatefulScaffold(
return RefreshStatefulScaffold<RepositoryContents>(
// canRefresh: !_isImage, // TODO:
title: AppBarTitle(path == null ? 'Files' : path),
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};
['png', 'jpg', 'jpeg', 'gif', 'webp'].contains(path.ext)) {
return RepositoryContents(
file: GitHubFile(downloadUrl: raw, content: ''),
);
}
final suffix = path == null ? '' : '/$path';
final res = await Provider.of<AuthModel>(context)
.getWithCredentials('/repos/$owner/$name/contents$suffix?ref=$ref');
final auth = Provider.of<AuthModel>(context);
final res = await auth.ghClient.repositories
.getContents(RepositorySlug(owner, name), suffix, ref: ref);
if (res.isDirectory) {
res.tree.sort((a, b) {
return sortByKey('dir', a.type, b.type);
});
}
return res;
},
actionBuilder: (data, _) {
if (data is Map) {
if (data.isFile) {
return ActionEntry(
iconData: Icons.settings,
url: '/choose-code-theme',
@ -44,13 +53,9 @@ class GhObjectScreen extends StatelessWidget {
}
},
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);
});
if (data.isDirectory) {
return ObjectTree(
items: items.map((v) {
items: data.tree.map((v) {
// if (item.type == 'commit') return null;
final uri = Uri(
path: '/$owner/$name/blob/$ref',
@ -71,11 +76,10 @@ class GhObjectScreen extends StatelessWidget {
} else {
// TODO: Markdown base path
// basePaths: [owner, name, branch, ...paths]
final v = GithubTreeItem.fromJson(data);
return BlobView(
path,
base64Text: v.content?.dropLineBreak,
networkUrl: v.downloadUrl,
text: data.file.text,
networkUrl: data.file.downloadUrl,
);
}
},