1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-24 06:42:07 +01:00

fix(gitlab): binary blob view

This commit is contained in:
Rongjian Zhang 2020-01-30 14:06:25 +08:00
parent e6401d65d5
commit d17b6a02a1
3 changed files with 23 additions and 20 deletions

View File

@ -137,9 +137,7 @@ class GitlabTreeItem {
String type; String type;
String path; String path;
String name; String name;
GitlabTreeItem(); GitlabTreeItem();
factory GitlabTreeItem.fromJson(Map<String, dynamic> json) => factory GitlabTreeItem.fromJson(Map<String, dynamic> json) =>
_$GitlabTreeItemFromJson(json); _$GitlabTreeItemFromJson(json);
} }
@ -147,9 +145,7 @@ class GitlabTreeItem {
@JsonSerializable(fieldRename: FieldRename.snake) @JsonSerializable(fieldRename: FieldRename.snake)
class GitlabBlob { class GitlabBlob {
String content; String content;
GitlabBlob(); GitlabBlob();
factory GitlabBlob.fromJson(Map<String, dynamic> json) => factory GitlabBlob.fromJson(Map<String, dynamic> json) =>
_$GitlabBlobFromJson(json); _$GitlabBlobFromJson(json);
} }

View File

@ -1,4 +1,3 @@
import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:git_touch/models/auth.dart'; import 'package:git_touch/models/auth.dart';
import 'package:git_touch/models/gitlab.dart'; import 'package:git_touch/models/gitlab.dart';
@ -30,7 +29,7 @@ class GitlabBlobScreen extends StatelessWidget {
return GitlabBlob.fromJson(res); return GitlabBlob.fromJson(res);
}, },
bodyBuilder: (data, _) { bodyBuilder: (data, _) {
return BlobView(path, utf8.decode(base64.decode(data.content))); return BlobView(path, base64Text: data.content);
}, },
); );
} }

View File

@ -1,3 +1,4 @@
import 'dart:convert';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter_highlight/theme_map.dart'; import 'package:flutter_highlight/theme_map.dart';
@ -12,24 +13,26 @@ import 'package:provider/provider.dart';
import 'package:git_touch/utils/utils.dart'; import 'package:git_touch/utils/utils.dart';
class BlobView extends StatelessWidget { class BlobView extends StatelessWidget {
final String path; final String name;
final String payload; final String text;
final String base64Text;
BlobView(this.path, this.payload); final String networkUrl;
BlobView(this.name, {this.text, this.base64Text, this.networkUrl})
: assert(text == null || base64Text == null);
String get _extname { String get _extname {
var dotext = p.extension(path); var dotext = p.extension(name);
if (dotext.isEmpty) return ''; if (dotext.isEmpty) return '';
return dotext.substring(1); return dotext.substring(1);
} }
String get _language => _extname.isEmpty ? 'plaintext' : _extname; String get _language => _extname.isEmpty ? 'plaintext' : _extname;
String get _text => text ?? utf8.decode(base64.decode(base64Text));
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final codeProvider = Provider.of<CodeModel>(context); final codeProvider = Provider.of<CodeModel>(context);
final theme = Provider.of<ThemeModel>(context); final theme = Provider.of<ThemeModel>(context);
switch (_extname) { switch (_extname) {
// TODO: All image types // TODO: All image types
case 'png': case 'png':
@ -37,23 +40,28 @@ class BlobView extends StatelessWidget {
case 'jpeg': case 'jpeg':
case 'gif': case 'gif':
case 'webp': case 'webp':
return PhotoView( // return PhotoView(
imageProvider: NetworkImage(payload), // imageProvider: MemoryImage(Uint8List.fromList(bits)),
backgroundDecoration: BoxDecoration(color: theme.palette.background), // backgroundDecoration: BoxDecoration(color: theme.palette.background),
); // );
return base64Text == null
? Image.network(networkUrl)
: Image.memory(base64.decode(base64Text));
case 'svg':
return base64Text == null
? SvgPicture.network(networkUrl)
: SvgPicture.memory(base64.decode(base64Text));
case 'md': case 'md':
case 'markdown': case 'markdown':
return Padding( return Padding(
padding: CommonStyle.padding, padding: CommonStyle.padding,
child: MarkdownView(payload), // TODO: basePath child: MarkdownView(_text), // TODO: basePath
); );
case 'svg':
return SvgPicture.network(payload);
default: default:
return SingleChildScrollView( return SingleChildScrollView(
scrollDirection: Axis.horizontal, scrollDirection: Axis.horizontal,
child: HighlightView( child: HighlightView(
payload, _text,
language: _language, language: _language,
theme: themeMap[theme.brightness == Brightness.dark theme: themeMap[theme.brightness == Brightness.dark
? codeProvider.themeDark ? codeProvider.themeDark