1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-25 23:34:01 +01:00
git-touch-android-ios-app/lib/screens/object.dart

201 lines
5.4 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';
2019-10-06 15:54:55 +02:00
import 'package:flutter_svg/flutter_svg.dart';
2019-09-15 11:36:09 +02:00
import 'package:git_touch/models/code.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/screens/code_theme.dart';
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';
2019-11-02 15:30:26 +01:00
import 'package:git_touch/screens/image_view.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-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-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
2019-11-02 17:02:41 +01:00
ObjectScreen(this.owner, this.name, this.branch,
{this.paths = const [], this.type = 'tree'});
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 '';
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-10-03 05:00:59 +02:00
String get _path => paths.join('/');
2019-09-15 11:36:09 +02:00
2019-08-31 16:44:27 +02:00
String get rawUrl =>
2019-10-03 05:00:59 +02:00
'https://raw.githubusercontent.com/$owner/$name/$branch/$_path'; // 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;
return ObjectScreen(
2019-11-02 17:02:41 +01:00
owner,
name,
branch,
2019-09-15 09:08:09 +02:00
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(
2019-10-02 10:09:54 +02:00
padding: CommonStyle.padding,
2019-11-06 14:56:52 +01:00
child: MarkdownView(payload['text'],
basePaths: [owner, name, branch, ...paths]),
2019-09-14 11:19:33 +02:00
);
2019-10-06 15:54:55 +02:00
case 'svg':
return SvgPicture.network(rawUrl);
2019-09-14 11:19:33 +02:00
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-10-02 10:09:54 +02:00
padding: CommonStyle.padding,
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-11-02 15:30:26 +01:00
// TODO: All image types
if (type == 'blob' &&
['png', 'jpg', 'jpeg', 'gif', 'webp'].contains(_extname)) {
return ImageViewScreen(rawUrl, title: Text(paths.last));
}
2019-09-25 11:06:36 +02:00
return RefreshStatefulScaffold(
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-09-27 14:52:38 +02:00
var data = await Provider.of<AuthModel>(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-11-02 13:54:23 +01:00
actionBuilder: (data, _) {
2019-09-15 11:36:09 +02:00
switch (type) {
case 'blob':
2019-09-28 18:25:14 +02:00
return ActionEntry(
iconData: Octicons.settings,
onTap: () {
2019-11-02 13:54:23 +01:00
if (data != null) {
Provider.of<ThemeModel>(context).pushRoute(
context, (_) => CodeThemeScreen(data['text'], _language));
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-08-30 08:23:58 +02:00
switch (type) {
case 'tree':
2019-11-02 13:54:23 +01:00
return _buildTree(data);
2019-08-30 08:23:58 +02:00
case 'blob':
2019-11-02 13:54:23 +01:00
return _buildBlob(context, data);
2019-08-30 08:23:58 +02:00
default:
return null;
}
2019-08-30 08:08:09 +02:00
},
);
}
}