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

198 lines
5.3 KiB
Dart
Raw Normal View History

2019-09-15 11:36:09 +02:00
import 'package:flutter/cupertino.dart';
import 'package:flutter_highlight/theme_map.dart';
import 'package:git_touch/models/code.dart';
2019-09-25 11:06:36 +02:00
import 'package:git_touch/scaffolds/refresh_stateful.dart';
2019-09-15 11:36:09 +02:00
import 'package:git_touch/screens/code_settings.dart';
2019-08-31 16:44:27 +02:00
import 'package:git_touch/screens/image_view.dart';
2019-09-11 13:59:47 +02:00
import 'package:git_touch/widgets/app_bar_title.dart';
2019-09-15 11:36:09 +02:00
import 'package:git_touch/widgets/link.dart';
2019-09-14 11:19:33 +02:00
import 'package:git_touch/widgets/markdown_view.dart';
2019-09-14 18:01:15 +02:00
import 'package:git_touch/widgets/table_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/utils/utils.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,
2019-09-13 17:01:30 +02:00
@required this.branch,
2019-08-30 08:08:09 +02:00
this.paths = const [],
2019-08-30 08:23:58 +02:00
this.type = 'tree',
2019-08-30 08:08:09 +02:00
});
2019-09-15 11:36:09 +02:00
String get _expression => '$branch:' + paths.join('/');
String get _extname {
2019-08-31 16:44:27 +02:00
if (paths.isEmpty) return '';
var dotext = path.extension(paths.last);
if (dotext.isEmpty) return '';
return dotext.substring(1);
}
2019-09-15 11:36:09 +02:00
String get _language => _extname.isEmpty ? 'plaintext' : _extname;
2019-08-31 16:44:27 +02:00
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;
2019-09-14 18:01:15 +02:00
Widget _buildIcon(item) {
2019-08-30 08:08:09 +02:00
switch (item['type']) {
case 'blob':
2019-09-14 18:01:15 +02:00
return SetiIcon(item['name'], size: 36);
2019-09-09 10:50:54 +02:00
case 'tree':
case 'commit':
2019-09-14 18:01:15 +02:00
return Icon(
item['type'] == 'tree'
? Octicons.file_directory
: Octicons.file_submodule,
color: _iconDefaultColor,
size: 24,
);
2019-09-09 10:50:54 +02:00
default:
2019-09-14 18:01:15 +02:00
throw 'Should not be here';
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-09-14 18:01:15 +02:00
return TableView(
2019-09-15 09:08:09 +02:00
hasIcon: true,
2019-09-14 18:01:15 +02:00
items: entries.map((item) {
return TableViewItem(
2019-09-15 09:08:09 +02:00
leftWidget: _buildIcon(item),
text: Text(item['name']),
screenBuilder: (_) {
if (item['type'] == 'commit') return null;
// TODO: All image types
var ext = path.extension(item['name']);
if (ext.isNotEmpty) ext = ext.substring(1);
if (['png', 'jpg', 'jpeg', 'gif', 'webp'].contains(ext)) {
return ImageView(NetworkImage('$rawUrl/' + item['name']));
}
return ObjectScreen(
name: name,
owner: owner,
branch: branch,
paths: [...paths, item['name']],
type: item['type'],
);
},
);
2019-09-14 18:01:15 +02:00
}),
2019-08-30 08:23:58 +02:00
);
}
2019-09-15 11:36:09 +02:00
Widget _buildBlob(BuildContext context, payload) {
var codeProvider = Provider.of<CodeModel>(context);
switch (_extname) {
2019-09-14 11:19:33 +02:00
case 'md':
case 'markdown':
return Padding(
padding: const EdgeInsets.all(12),
child: MarkdownView(payload['text']),
);
default:
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: HighlightView(
payload['text'],
2019-09-15 11:36:09 +02:00
language: _language,
theme: themeMap[codeProvider.theme],
2019-09-14 11:19:33 +02:00
padding: EdgeInsets.all(10),
2019-09-15 11:36:09 +02:00
textStyle: TextStyle(
fontSize: codeProvider.fontSize.toDouble(),
2019-09-15 12:27:46 +02:00
fontFamily: codeProvider.fontFamilyUsed),
2019-09-14 11:19:33 +02:00
),
);
}
2019-08-30 08:23:58 +02:00
}
2019-08-30 08:08:09 +02:00
@override
Widget build(BuildContext context) {
2019-09-25 11:06:36 +02:00
return RefreshStatefulScaffold(
2019-09-11 13:59:47 +02:00
title: AppBarTitle(paths.join('/')),
2019-09-24 13:58:34 +02:00
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") {
2019-09-15 11:36:09 +02:00
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
},
2019-09-15 11:36:09 +02:00
trailingBuilder: (payload) {
switch (type) {
case 'blob':
return Link(
child: Icon(Octicons.settings, size: 20),
material: false,
screenBuilder: (_) =>
CodeSettingsScreen(payload['text'], _language),
);
default:
return null;
}
},
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':
2019-09-15 11:36:09 +02:00
return _buildBlob(context, payload);
2019-08-30 08:23:58 +02:00
default:
return null;
}
2019-08-30 08:08:09 +02:00
},
);
}
}