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

190 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';
2020-01-07 08:07:57 +01:00
import 'package:git_touch/graphql/gh.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/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-12-13 06:13:45 +01:00
import 'package:path/path.dart' as p;
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
2019-12-13 06:13:45 +01:00
final objectRouter = RouterScreen(
'/:owner/:name/blob/:ref',
(context, params) => ObjectScreen(
params['owner'].first,
params['name'].first,
params['ref'].first,
paths: params['path']?.first?.urldecode?.split('/') ?? [],
),
);
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 '';
2019-12-13 06:13:45 +01:00
var dotext = p.extension(paths.last);
2019-08-31 16:44:27 +02:00
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;
2020-01-07 08:07:57 +01:00
Widget _buildIcon(GhObjectTreeEntry item) {
2019-12-07 09:39:56 +01:00
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) {
2020-01-07 08:07:57 +01:00
return RefreshStatefulScaffold<GhObjectGitObject>(
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
2020-01-07 08:07:57 +01:00
.execute(GhObjectQuery(
variables: GhObjectArguments(
2019-12-07 09:39:56 +01:00
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') {
2020-01-07 08:07:57 +01:00
(data as GhObjectTree).entries.sort((a, b) {
2019-12-07 10:00:23 +01:00
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
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-07 08:07:57 +01:00
final tree = data as GhObjectTree;
2019-12-12 14:06:12 +01:00
2019-12-07 09:39:56 +01:00
return TableView(
hasIcon: true,
items: tree.entries.map((item) {
2019-12-12 14:06:12 +01:00
// if (item.type == 'commit') return null;
final p = [...paths, item.name].join('/').urlencode;
final url = '/$owner/$name/blob/$branch?path=$p';
2019-12-07 09:39:56 +01:00
return TableViewItem(
leftWidget: _buildIcon(item),
text: Text(item.name),
2019-12-12 14:06:12 +01:00
url: url,
2019-12-07 09:39:56 +01:00
);
}),
);
case 'Blob':
final codeProvider = Provider.of<CodeModel>(context);
2019-12-07 09:52:54 +01:00
final theme = Provider.of<ThemeModel>(context);
2020-01-07 08:07:57 +01:00
final text = (data as GhObjectBlob).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:
2020-01-27 08:11:51 +01:00
BoxDecoration(color: theme.palette.background),
2019-12-07 09:52:54 +01:00
);
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,
2020-01-27 07:43:10 +01:00
theme: themeMap[theme.brightness == Brightness.dark
? codeProvider.themeDark
: codeProvider.theme],
2019-12-07 09:39:56 +01:00
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
},
);
}
}