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-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';
|
2020-01-31 04:50:41 +01:00
|
|
|
import 'package:path/path.dart' as p;
|
2019-08-30 08:08:09 +02:00
|
|
|
import 'package:git_touch/utils/utils.dart';
|
|
|
|
|
2020-01-31 04:25:23 +01:00
|
|
|
final objectRouter = RouterScreen('/:owner/:name/blob/:ref', (context, params) {
|
|
|
|
return ObjectScreen(
|
2019-12-13 06:13:45 +01:00
|
|
|
params['owner'].first,
|
|
|
|
params['name'].first,
|
|
|
|
params['ref'].first,
|
2020-01-31 04:50:41 +01:00
|
|
|
path: params['path']?.first,
|
2020-01-31 04:25:23 +01:00
|
|
|
);
|
|
|
|
});
|
2019-12-13 06:13:45 +01:00
|
|
|
|
2019-08-30 08:08:09 +02:00
|
|
|
class ObjectScreen extends StatelessWidget {
|
|
|
|
final String owner;
|
|
|
|
final String name;
|
|
|
|
final String branch;
|
2020-01-31 04:50:41 +01:00
|
|
|
final String path;
|
|
|
|
ObjectScreen(this.owner, this.name, this.branch, {this.path});
|
2019-08-30 08:08:09 +02:00
|
|
|
|
2020-01-31 04:50:41 +01:00
|
|
|
String get _pathNotNull => path ?? '';
|
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>(
|
2020-01-31 04:50:41 +01:00
|
|
|
// canRefresh: !_isImage, // TODO:
|
|
|
|
title: AppBarTitle(path == null ? '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,
|
2020-01-31 04:50:41 +01:00
|
|
|
expression: '$branch:$_pathNotNull',
|
2019-12-07 09:39:56 +01:00
|
|
|
),
|
|
|
|
));
|
2019-12-07 10:00:23 +01:00
|
|
|
final data = res.data.repository.object;
|
|
|
|
if (data.resolveType == 'Tree') {
|
2020-01-07 08:07:57 +01:00
|
|
|
(data as GhObjectTree).entries.sort((a, b) {
|
2020-01-30 12:24:59 +01:00
|
|
|
return sortByKey('tree', a.type, b.type);
|
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 08:31:46 +01:00
|
|
|
return ObjectTreeItem(
|
|
|
|
name: v.name,
|
|
|
|
type: v.type,
|
2020-01-31 04:50:41 +01:00
|
|
|
url:
|
|
|
|
'/$owner/$name/blob/$branch?path=${p.join(_pathNotNull, v.name).urlencode}',
|
2020-01-30 08:31:46 +01:00
|
|
|
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(
|
2020-01-31 04:50:41 +01:00
|
|
|
path,
|
2020-01-30 07:43:58 +01:00
|
|
|
text: (data as GhObjectBlob).text,
|
|
|
|
networkUrl:
|
2020-01-31 04:50:41 +01:00
|
|
|
'https://raw.githubusercontent.com/$owner/$name/$branch/$path', // TODO: private
|
2020-01-30 07:43:58 +01:00
|
|
|
);
|
2019-08-30 08:23:58 +02:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2019-08-30 08:08:09 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|