From d17b6a02a16c92ca3a78da9b955bc2160be533ac Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Thu, 30 Jan 2020 14:06:25 +0800 Subject: [PATCH] fix(gitlab): binary blob view --- lib/models/gitlab.dart | 4 ---- lib/screens/gitlab_blob.dart | 3 +-- lib/widgets/blob_view.dart | 36 ++++++++++++++++++++++-------------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/lib/models/gitlab.dart b/lib/models/gitlab.dart index 89c1008..f460350 100644 --- a/lib/models/gitlab.dart +++ b/lib/models/gitlab.dart @@ -137,9 +137,7 @@ class GitlabTreeItem { String type; String path; String name; - GitlabTreeItem(); - factory GitlabTreeItem.fromJson(Map json) => _$GitlabTreeItemFromJson(json); } @@ -147,9 +145,7 @@ class GitlabTreeItem { @JsonSerializable(fieldRename: FieldRename.snake) class GitlabBlob { String content; - GitlabBlob(); - factory GitlabBlob.fromJson(Map json) => _$GitlabBlobFromJson(json); } diff --git a/lib/screens/gitlab_blob.dart b/lib/screens/gitlab_blob.dart index ebc73a7..ccf36f5 100644 --- a/lib/screens/gitlab_blob.dart +++ b/lib/screens/gitlab_blob.dart @@ -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); }, ); } diff --git a/lib/widgets/blob_view.dart b/lib/widgets/blob_view.dart index af3042e..d25560d 100644 --- a/lib/widgets/blob_view.dart +++ b/lib/widgets/blob_view.dart @@ -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(context); final theme = Provider.of(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