git-touch-android-ios-app/lib/screens/object.dart

186 lines
4.8 KiB
Dart
Raw Normal View History

2019-08-30 08:23:58 +02:00
import 'package:flutter_highlight/themes/github.dart';
2019-08-31 16:44:27 +02:00
import 'package:git_touch/screens/image_view.dart';
2019-08-30 08:23:58 +02:00
import 'package:path/path.dart' as path;
2019-08-30 08:08:09 +02:00
import 'package:flutter/material.dart';
2019-08-30 08:23:58 +02:00
import 'package:flutter_highlight/flutter_highlight.dart';
2019-09-08 14:07:35 +02:00
import 'package:git_touch/models/settings.dart';
import 'package:provider/provider.dart';
2019-08-30 08:08:09 +02:00
import 'package:git_touch/scaffolds/refresh.dart';
import 'package:git_touch/utils/utils.dart';
import 'package:git_touch/widgets/link.dart';
2019-08-30 09:20:29 +02:00
import 'package:primer/primer.dart';
2019-09-09 10:41:43 +02:00
import 'package:seti/seti.dart';
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-08-30 08:23:58 +02:00
final String type;
2019-08-30 08:08:09 +02:00
ObjectScreen({
@required this.owner,
@required this.name,
this.branch = 'master',
this.paths = const [],
2019-08-30 08:23:58 +02:00
this.type = 'tree',
2019-08-30 08:08:09 +02:00
});
2019-08-31 16:44:27 +02:00
String get expression => '$branch:' + paths.join('/');
String get extname {
if (paths.isEmpty) return '';
var dotext = path.extension(paths.last);
if (dotext.isEmpty) return '';
return dotext.substring(1);
}
String get rawUrl =>
'https://raw.githubusercontent.com/$owner/$name/$branch/' +
paths.join('/'); // TODO:
2019-08-30 08:08:09 +02:00
2019-09-09 10:41:43 +02:00
static const _iconDefaultColor = PrimerColors.blue300;
List<Widget> _buildIcon(item) {
2019-08-30 08:08:09 +02:00
switch (item['type']) {
case 'blob':
2019-09-09 10:41:43 +02:00
return [
SizedBox(width: 6),
2019-09-11 07:13:15 +02:00
SetiIcon(item['name'], size: 28),
2019-09-09 10:41:43 +02:00
SizedBox(width: 6),
];
2019-09-09 10:50:54 +02:00
case 'tree':
case 'commit':
2019-09-09 10:41:43 +02:00
return [
SizedBox(width: 10),
2019-09-09 10:50:54 +02:00
Icon(
item['type'] == 'tree'
? Octicons.file_directory
: Octicons.file_submodule,
color: _iconDefaultColor,
size: 20,
),
2019-09-09 10:41:43 +02:00
SizedBox(width: 10),
2019-09-09 10:50:54 +02:00
];
default:
return [];
2019-08-30 08:08:09 +02:00
}
}
2019-08-30 08:23:58 +02:00
String get _subQuery {
switch (type) {
case 'tree':
return '''
... on Tree {
entries {
type
name
}
}
''';
case 'blob':
default:
return '''
... on Blob {
text
}
''';
}
}
Widget _buildTree(payload) {
2019-08-30 09:20:29 +02:00
var entries = payload['entries'] as List;
2019-08-30 08:23:58 +02:00
return Column(
2019-09-09 10:41:43 +02:00
crossAxisAlignment: CrossAxisAlignment.stretch,
children: join(
BorderView(),
entries.map((item) {
return Link(
2019-09-09 10:50:54 +02:00
screenBuilder: item['type'] == 'commit'
? null
: (context) {
// TODO: All image types
var ext = path.extension(item['name']);
if (ext.isNotEmpty) ext = ext.substring(1);
2019-09-09 16:50:22 +02:00
if (['png', 'jpg', 'jpeg', 'gif', 'webp'].contains(ext)) {
2019-09-09 10:50:54 +02:00
return ImageView(NetworkImage('$rawUrl/' + item['name']));
}
return ObjectScreen(
name: name,
owner: owner,
branch: branch,
paths: [...paths, item['name']],
type: item['type'],
);
},
2019-09-09 10:41:43 +02:00
child: Container(
height: 40,
child: Row(
children: <Widget>[
..._buildIcon(item),
Expanded(
child: Text(item['name'],
style: TextStyle(
fontSize: 16, color: PrimerColors.gray900)))
],
),
2019-08-30 08:23:58 +02:00
),
2019-09-09 10:41:43 +02:00
);
}).toList(),
),
2019-08-30 08:23:58 +02:00
);
}
Widget _buildBlob(payload) {
2019-09-02 16:35:20 +02:00
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: HighlightView(
payload['text'],
language: extname.isEmpty ? 'plaintext' : extname,
theme: githubTheme,
padding: EdgeInsets.all(10),
),
2019-08-30 08:23:58 +02:00
);
}
2019-08-30 08:08:09 +02:00
@override
Widget build(BuildContext context) {
return RefreshScaffold(
title: Text(paths.join('/')),
onRefresh: () async {
2019-09-08 14:07:35 +02:00
var data = await Provider.of<SettingsModel>(context).query('''{
2019-08-30 08:08:09 +02:00
repository(owner: "$owner", name: "$name") {
object(expression: "$expression") {
2019-08-30 08:23:58 +02:00
$_subQuery
2019-08-30 08:08:09 +02:00
}
}
}''');
2019-08-30 09:20:29 +02:00
if (type == 'tree') {
var entries = data['repository']['object']['entries'] as List;
entries.sort((a, b) {
if (a['type'] == 'tree' && b['type'] == 'blob') {
return -1;
}
if (a['type'] == 'blob' && b['type'] == 'tree') {
return 1;
}
return 0;
});
}
2019-08-30 08:23:58 +02:00
return data['repository']['object'];
2019-08-30 08:08:09 +02:00
},
bodyBuilder: (payload) {
2019-08-30 08:23:58 +02:00
switch (type) {
case 'tree':
return _buildTree(payload);
case 'blob':
return _buildBlob(payload);
default:
return null;
}
2019-08-30 08:08:09 +02:00
},
);
}
}