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

99 lines
3.3 KiB
Dart
Raw Normal View History

2019-09-15 11:36:09 +02:00
import 'package:flutter/cupertino.dart';
import 'package:git_touch/models/github.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';
2020-01-30 07:43:58 +01:00
import 'package:git_touch/widgets/blob_view.dart';
2020-01-30 05:55:55 +01:00
import 'package:git_touch/widgets/object_tree.dart';
2019-08-30 08:08:09 +02:00
import 'package:flutter/material.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';
2020-01-31 04:50:41 +01:00
import 'package:path/path.dart' as p;
2019-08-30 08:08:09 +02:00
import 'package:git_touch/utils/utils.dart';
final objectRouter = RouterScreen('/:owner/:name/blob/:ref', (context, params) {
return ObjectScreen(
2019-12-13 06:13:45 +01:00
params['owner'].first,
params['name'].first,
params['ref'].first,
2020-01-31 04:50:41 +01:00
path: params['path']?.first,
);
});
2019-12-13 06:13:45 +01:00
2019-08-30 08:08:09 +02:00
class ObjectScreen extends StatelessWidget {
final String owner;
final String name;
final String branch;
2020-01-31 04:50:41 +01:00
final String path;
ObjectScreen(this.owner, this.name, this.branch, {this.path});
2019-08-30 08:08:09 +02:00
@override
Widget build(BuildContext context) {
return RefreshStatefulScaffold(
2020-01-31 04:50:41 +01:00
// canRefresh: !_isImage, // TODO:
title: AppBarTitle(path == null ? 'Files' : path),
2019-09-30 11:13:12 +02:00
fetchData: () async {
final suffix = path == null ? '' : '/$path';
final res = await Provider.of<AuthModel>(context).getWithCredentials(
'/repos/$owner/$name/contents$suffix?ref=$branch');
return res;
2019-08-30 08:08:09 +02:00
},
2019-11-02 13:54:23 +01:00
actionBuilder: (data, _) {
if (data is Map) {
final theme = Provider.of<ThemeModel>(context);
return ActionEntry(
iconData: Icons.settings,
onTap: () {
theme.push(context, '/choose-code-theme');
},
);
2019-09-15 11:36:09 +02:00
}
},
2019-11-02 13:54:23 +01:00
bodyBuilder: (data, _) {
if (data is List) {
final items = data.map((t) => GithubTreeItem.fromJson(t)).toList();
items.sort((a, b) {
return sortByKey('dir', a.type, b.type);
});
return ObjectTree(
items: items.map((v) {
// if (item.type == 'commit') return null;
String url;
var ext = p.extension(v.name);
if (ext.startsWith('.')) ext = ext.substring(1);
if (['pdf', 'docx', 'doc', 'pptx', 'ppt', 'xlsx', 'xls']
.contains(ext)) {
// Let system browser handle these files
//
// TODO:
// Unhandled Exception: PlatformException(Error, Error while launching
// https://github.com/flutter/flutter/issues/49162
url = v.downloadUrl;
} else {
url = '/$owner/$name/blob/$branch?path=${v.path.urlencode}';
}
2020-01-31 06:50:09 +01:00
return ObjectTreeItem(
name: v.name,
type: v.type,
url: url,
size: v.type == 'file' ? v.size : null,
);
}),
);
} else {
// TODO: Markdown base path
// basePaths: [owner, name, branch, ...paths]
final v = GithubTreeItem.fromJson(data);
return BlobView(
path,
2020-01-31 07:31:24 +01:00
base64Text: v.content?.dropLineBreak,
networkUrl: v.downloadUrl,
);
2019-08-30 08:23:58 +02:00
}
2019-08-30 08:08:09 +02:00
},
);
}
}