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

View File

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