2019-09-15 11:36:09 +02:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2020-01-07 08:07:57 +01:00
|
|
|
import 'package:git_touch/graphql/gh.dart';
|
2019-09-28 18:25:14 +02:00
|
|
|
import 'package:git_touch/models/theme.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-12-13 06:13:45 +01:00
|
|
|
import 'package:path/path.dart' as p;
|
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';
|
|
|
|
|
2019-12-13 06:13:45 +01:00
|
|
|
final objectRouter = RouterScreen(
|
|
|
|
'/:owner/:name/blob/:ref',
|
|
|
|
(context, params) => ObjectScreen(
|
|
|
|
params['owner'].first,
|
|
|
|
params['name'].first,
|
|
|
|
params['ref'].first,
|
|
|
|
paths: params['path']?.first?.urldecode?.split('/') ?? [],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2019-08-30 08:08:09 +02:00
|
|
|
class ObjectScreen extends StatelessWidget {
|
|
|
|
final String owner;
|
|
|
|
final String name;
|
|
|
|
final String branch;
|
|
|
|
final List<String> paths;
|
|
|
|
|
2019-12-07 09:52:54 +01:00
|
|
|
ObjectScreen(this.owner, this.name, this.branch, {this.paths = const []});
|
2019-08-30 08:08:09 +02:00
|
|
|
|
2019-10-03 05:00:59 +02:00
|
|
|
String get _expression => '$branch:$_path';
|
2019-09-15 11:36:09 +02:00
|
|
|
String get _extname {
|
2019-08-31 16:44:27 +02:00
|
|
|
if (paths.isEmpty) return '';
|
2019-12-13 06:13:45 +01:00
|
|
|
var dotext = p.extension(paths.last);
|
2019-08-31 16:44:27 +02:00
|
|
|
if (dotext.isEmpty) return '';
|
|
|
|
return dotext.substring(1);
|
|
|
|
}
|
|
|
|
|
2019-10-03 05:00:59 +02:00
|
|
|
String get _path => paths.join('/');
|
2019-12-07 09:52:54 +01:00
|
|
|
bool get _isImage => ['png', 'jpg', 'jpeg', 'gif', 'webp'].contains(_extname);
|
2019-09-15 11:36:09 +02:00
|
|
|
|
2019-08-30 08:08:09 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-01-07 08:07:57 +01:00
|
|
|
return RefreshStatefulScaffold<GhObjectGitObject>(
|
2019-12-07 09:52:54 +01:00
|
|
|
canRefresh: !_isImage,
|
2019-10-03 05:00:59 +02:00
|
|
|
title: AppBarTitle(_path.isEmpty ? 'Files' : _path),
|
2019-09-30 11:13:12 +02:00
|
|
|
fetchData: () async {
|
2019-12-07 09:39:56 +01:00
|
|
|
final res = await Provider.of<AuthModel>(context)
|
|
|
|
.gqlClient
|
2020-01-07 08:07:57 +01:00
|
|
|
.execute(GhObjectQuery(
|
|
|
|
variables: GhObjectArguments(
|
2019-12-07 09:39:56 +01:00
|
|
|
owner: owner,
|
|
|
|
name: name,
|
|
|
|
expression: _expression,
|
|
|
|
),
|
|
|
|
));
|
2019-12-07 10:00:23 +01:00
|
|
|
final data = res.data.repository.object;
|
2019-08-30 09:20:29 +02:00
|
|
|
|
2019-12-07 10:00:23 +01:00
|
|
|
if (data.resolveType == 'Tree') {
|
2020-01-07 08:07:57 +01:00
|
|
|
(data as GhObjectTree).entries.sort((a, b) {
|
2019-12-07 10:00:23 +01:00
|
|
|
if (a.type == 'tree' && b.type == 'blob') {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.type == 'blob' && b.type == 'tree') {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
2019-08-30 09:20:29 +02:00
|
|
|
|
2019-12-07 10:00:23 +01:00
|
|
|
return data;
|
2019-08-30 08:08:09 +02:00
|
|
|
},
|
2019-11-02 13:54:23 +01:00
|
|
|
actionBuilder: (data, _) {
|
2019-12-07 09:39:56 +01:00
|
|
|
switch (data.resolveType) {
|
|
|
|
case 'Blob':
|
2020-01-07 08:07:57 +01:00
|
|
|
final blob = data as GhObjectBlob;
|
2020-01-12 10:13:48 +01:00
|
|
|
final theme = Provider.of<ThemeModel>(context);
|
2019-09-28 18:25:14 +02:00
|
|
|
return ActionEntry(
|
2020-01-12 10:13:48 +01:00
|
|
|
iconData: Icons.settings,
|
2019-09-28 18:25:14 +02:00
|
|
|
onTap: () {
|
2020-01-12 10:13:48 +01:00
|
|
|
theme.push(context, '/choose-code-theme');
|
2019-09-28 18:25:14 +02:00
|
|
|
},
|
2019-09-15 11:36:09 +02:00
|
|
|
);
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
2019-11-02 13:54:23 +01:00
|
|
|
bodyBuilder: (data, _) {
|
2019-12-07 09:39:56 +01:00
|
|
|
switch (data.resolveType) {
|
|
|
|
case 'Tree':
|
2020-01-30 05:55:55 +01:00
|
|
|
return ObjectTree(
|
|
|
|
items: (data as GhObjectTree).entries.map((v) {
|
2019-12-12 14:06:12 +01:00
|
|
|
// if (item.type == 'commit') return null;
|
2020-01-30 05:55:55 +01:00
|
|
|
final p = [...paths, v.name].join('/').urlencode;
|
2020-01-30 08:31:46 +01:00
|
|
|
|
|
|
|
return ObjectTreeItem(
|
|
|
|
name: v.name,
|
|
|
|
type: v.type,
|
|
|
|
url: '/$owner/$name/blob/$branch?path=$p',
|
|
|
|
size: v.object.resolveType == 'Blob'
|
|
|
|
? (v.object as GhObjectBlob).byteSize
|
|
|
|
: null,
|
|
|
|
);
|
2019-12-07 09:39:56 +01:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
case 'Blob':
|
2020-01-30 07:43:58 +01:00
|
|
|
// TODO: Markdown base path
|
|
|
|
// basePaths: [owner, name, branch, ...paths]
|
|
|
|
return BlobView(
|
|
|
|
_path,
|
|
|
|
text: (data as GhObjectBlob).text,
|
|
|
|
networkUrl:
|
|
|
|
'https://raw.githubusercontent.com/$owner/$name/$branch/$_path', // TODO: private
|
|
|
|
);
|
2019-08-30 08:23:58 +02:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2019-08-30 08:08:09 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|