mirror of
https://github.com/git-touch/git-touch
synced 2025-01-16 09:31:14 +01:00
feat(gitlab): blob screen
This commit is contained in:
parent
edd3a8d02e
commit
d91ae0a213
@ -132,3 +132,13 @@ class GitlabTreeItem {
|
||||
factory GitlabTreeItem.fromJson(Map<String, dynamic> json) =>
|
||||
_$GitlabTreeItemFromJson(json);
|
||||
}
|
||||
|
||||
@JsonSerializable(fieldRename: FieldRename.snake)
|
||||
class GitlabBlob {
|
||||
String content;
|
||||
|
||||
GitlabBlob();
|
||||
|
||||
factory GitlabBlob.fromJson(Map<String, dynamic> json) =>
|
||||
_$GitlabBlobFromJson(json);
|
||||
}
|
||||
|
@ -178,3 +178,12 @@ Map<String, dynamic> _$GitlabTreeItemToJson(GitlabTreeItem instance) =>
|
||||
'type': instance.type,
|
||||
'path': instance.path,
|
||||
};
|
||||
|
||||
GitlabBlob _$GitlabBlobFromJson(Map<String, dynamic> json) {
|
||||
return GitlabBlob()..content = json['content'] as String;
|
||||
}
|
||||
|
||||
Map<String, dynamic> _$GitlabBlobToJson(GitlabBlob instance) =>
|
||||
<String, dynamic>{
|
||||
'content': instance.content,
|
||||
};
|
||||
|
31
lib/screens/gitlab_blob.dart
Normal file
31
lib/screens/gitlab_blob.dart
Normal file
@ -0,0 +1,31 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:git_touch/models/auth.dart';
|
||||
import 'package:git_touch/models/gitlab.dart';
|
||||
import 'package:git_touch/scaffolds/refresh_stateful.dart';
|
||||
import 'package:git_touch/widgets/app_bar_title.dart';
|
||||
import 'package:git_touch/widgets/blob_view.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class GitlabBlobScreen extends StatelessWidget {
|
||||
final int id;
|
||||
final String path;
|
||||
|
||||
GitlabBlobScreen(this.id, this.path);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return RefreshStatefulScaffold<GitlabBlob>(
|
||||
title: AppBarTitle(path),
|
||||
fetchData: () async {
|
||||
final res = await Provider.of<AuthModel>(context).fetchGitlab(
|
||||
'/projects/$id/repository/files/$path?ref=master'); // TODO:
|
||||
return GitlabBlob.fromJson(res);
|
||||
},
|
||||
bodyBuilder: (data, _) {
|
||||
return BlobView(path, utf8.decode(base64.decode(data.content)));
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:git_touch/models/gitlab.dart';
|
||||
import 'package:git_touch/scaffolds/refresh_stateful.dart';
|
||||
import 'package:git_touch/screens/gitlab_blob.dart';
|
||||
import 'package:git_touch/widgets/app_bar_title.dart';
|
||||
import 'package:git_touch/widgets/table_view.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@ -58,11 +59,16 @@ class GitlabTreeScreen extends StatelessWidget {
|
||||
leftWidget: _buildIcon(item),
|
||||
text: Text(item.path),
|
||||
screenBuilder: (_) {
|
||||
if (item.type == 'commit') return null;
|
||||
return GitlabTreeScreen(
|
||||
id,
|
||||
paths: [...paths, item.path],
|
||||
);
|
||||
switch (item.type) {
|
||||
case 'tree':
|
||||
return GitlabTreeScreen(
|
||||
id,
|
||||
paths: [...paths, item.path],
|
||||
);
|
||||
case 'blob':
|
||||
return GitlabBlobScreen(
|
||||
id, [...paths, item.path].join('/'));
|
||||
}
|
||||
},
|
||||
);
|
||||
}),
|
||||
|
67
lib/widgets/blob_view.dart
Normal file
67
lib/widgets/blob_view.dart
Normal file
@ -0,0 +1,67 @@
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter_highlight/theme_map.dart';
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
import 'package:git_touch/models/code.dart';
|
||||
import 'package:git_touch/models/theme.dart';
|
||||
import 'package:git_touch/widgets/markdown_view.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_highlight/flutter_highlight.dart';
|
||||
import 'package:photo_view/photo_view.dart';
|
||||
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);
|
||||
|
||||
String get _extname {
|
||||
var dotext = p.extension(path);
|
||||
if (dotext.isEmpty) return '';
|
||||
return dotext.substring(1);
|
||||
}
|
||||
|
||||
String get _language => _extname.isEmpty ? 'plaintext' : _extname;
|
||||
|
||||
@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':
|
||||
case 'jpg':
|
||||
case 'jpeg':
|
||||
case 'gif':
|
||||
case 'webp':
|
||||
return PhotoView(
|
||||
imageProvider: NetworkImage(payload),
|
||||
backgroundDecoration: BoxDecoration(color: theme.palette.background),
|
||||
);
|
||||
case 'md':
|
||||
case 'markdown':
|
||||
return Padding(
|
||||
padding: CommonStyle.padding,
|
||||
child: MarkdownView(payload), // TODO: basePath
|
||||
);
|
||||
case 'svg':
|
||||
return SvgPicture.network(payload);
|
||||
default:
|
||||
return SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: HighlightView(
|
||||
payload,
|
||||
language: _language,
|
||||
theme: themeMap[codeProvider.theme],
|
||||
padding: CommonStyle.padding,
|
||||
textStyle: TextStyle(
|
||||
fontSize: codeProvider.fontSize.toDouble(),
|
||||
fontFamily: codeProvider.fontFamilyUsed),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user