1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-28 08:40:54 +01:00
git-touch-android-ios-app/lib/screens/object.dart

185 lines
6.0 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-12-07 09:39:56 +01:00
import 'package:git_touch/graphql/github_object.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-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-12-07 09:52:54 +01:00
import 'package:photo_view/photo_view.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-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 '';
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-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-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-12-07 09:39:56 +01:00
Widget _buildIcon(GithubObjectTreeEntry item) {
switch (item.type) {
2019-08-30 08:08:09 +02:00
case 'blob':
2019-12-07 09:39:56 +01: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(
2019-12-07 09:39:56 +01:00
item.type == 'tree'
2019-09-14 18:01:15 +02:00
? Octicons.file_directory
: Octicons.file_submodule,
color: _iconDefaultColor,
size: 24,
);
2019-09-09 10:50:54 +02:00
default:
2019-12-07 09:39:56 +01:00
return null;
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-12-07 09:39:56 +01:00
return RefreshStatefulScaffold<GithubObjectGitObject>(
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
.execute(GithubObjectQuery(
variables: GithubObjectArguments(
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') {
(data as GithubObjectTree).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 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
if (data == null) return null;
switch (data.resolveType) {
case 'Blob':
final blob = data as GithubObjectBlob;
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(
2019-12-07 09:39:56 +01:00
context, (_) => CodeThemeScreen(blob.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-12-07 09:39:56 +01:00
switch (data.resolveType) {
case 'Tree':
final tree = data as GithubObjectTree;
return TableView(
hasIcon: true,
items: tree.entries.map((item) {
return TableViewItem(
leftWidget: _buildIcon(item),
text: Text(item.name),
screenBuilder: (_) {
if (item.type == 'commit') return null;
return ObjectScreen(
owner,
name,
branch,
paths: [...paths, item.name],
);
},
);
}),
);
case 'Blob':
final codeProvider = Provider.of<CodeModel>(context);
2019-12-07 09:52:54 +01:00
final theme = Provider.of<ThemeModel>(context);
2019-12-07 09:39:56 +01:00
final text = (data as GithubObjectBlob).text;
2019-12-07 09:52:54 +01:00
2019-12-07 09:39:56 +01:00
switch (_extname) {
2019-12-07 09:52:54 +01:00
// TODO: All image types
case 'png':
case 'jpg':
case 'jpeg':
case 'gif':
case 'webp':
return PhotoView(
imageProvider: NetworkImage(rawUrl),
backgroundDecoration:
BoxDecoration(color: theme.palette.background),
);
2019-12-07 09:39:56 +01:00
case 'md':
case 'markdown':
return Padding(
padding: CommonStyle.padding,
child: MarkdownView(text,
basePaths: [owner, name, branch, ...paths]),
);
case 'svg':
return SvgPicture.network(rawUrl);
default:
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: HighlightView(
text,
language: _language,
theme: themeMap[codeProvider.theme],
padding: CommonStyle.padding,
textStyle: TextStyle(
fontSize: codeProvider.fontSize.toDouble(),
fontFamily: codeProvider.fontFamilyUsed),
),
);
}
break;
2019-08-30 08:23:58 +02:00
default:
return null;
}
2019-08-30 08:08:09 +02:00
},
);
}
}