1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-18 19:22:54 +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 path;
String name;
GitlabTreeItem();
factory GitlabTreeItem.fromJson(Map<String, dynamic> json) =>
_$GitlabTreeItemFromJson(json);
}
@ -147,9 +145,7 @@ class GitlabTreeItem {
@JsonSerializable(fieldRename: FieldRename.snake)
class GitlabBlob {
String content;
GitlabBlob();
factory GitlabBlob.fromJson(Map<String, dynamic> json) =>
_$GitlabBlobFromJson(json);
}

View File

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