From 39c360f28118991a68ea629e41377fc6c19046de Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Thu, 30 Jan 2020 15:31:46 +0800 Subject: [PATCH] feat: object tree item size --- lib/graphql/gh.dart | 153 +++++++++++++++++++++------------- lib/graphql/gh.g.dart | 48 ++++++----- lib/graphql/gh_object.graphql | 13 ++- lib/models/gitea.dart | 1 + lib/models/gitea.g.dart | 6 +- lib/screens/gitea_object.dart | 1 + lib/screens/object.dart | 11 ++- lib/widgets/object_tree.dart | 5 ++ 8 files changed, 155 insertions(+), 83 deletions(-) diff --git a/lib/graphql/gh.dart b/lib/graphql/gh.dart index 8cb67d9..6b694be 100644 --- a/lib/graphql/gh.dart +++ b/lib/graphql/gh.dart @@ -7308,10 +7308,10 @@ class GhObjectGitObject with EquatableMixin { factory GhObjectGitObject.fromJson(Map json) { switch (json['__typename'].toString()) { - case 'Tree': - return GhObjectTree.fromJson(json); case 'Blob': return GhObjectBlob.fromJson(json); + case 'Tree': + return GhObjectTree.fromJson(json); default: } return _$GhObjectGitObjectFromJson(json); @@ -7324,16 +7324,53 @@ class GhObjectGitObject with EquatableMixin { List get props => [resolveType]; Map toJson() { switch (resolveType) { - case 'Tree': - return (this as GhObjectTree).toJson(); case 'Blob': return (this as GhObjectBlob).toJson(); + case 'Tree': + return (this as GhObjectTree).toJson(); default: } return _$GhObjectGitObjectToJson(this); } } +@JsonSerializable(explicitToJson: true) +class GhObjectBlob + with EquatableMixin + implements GhObjectNode, GhObjectGitObject { + GhObjectBlob({this.text, this.byteSize}); + + factory GhObjectBlob.fromJson(Map json) => + _$GhObjectBlobFromJson(json); + + String text; + + int byteSize; + + @override + @JsonKey(name: '__typename') + String resolveType; + + @override + List get props => [text, byteSize, resolveType]; + Map toJson() => _$GhObjectBlobToJson(this); +} + +@JsonSerializable(explicitToJson: true) +class GhObjectNode with EquatableMixin { + GhObjectNode(); + + factory GhObjectNode.fromJson(Map json) => + _$GhObjectNodeFromJson(json); + + @JsonKey(name: '__typename') + String resolveType; + + @override + List get props => [resolveType]; + Map toJson() => _$GhObjectNodeToJson(this); +} + @JsonSerializable(explicitToJson: true) class GhObjectTree with EquatableMixin @@ -7356,7 +7393,7 @@ class GhObjectTree @JsonSerializable(explicitToJson: true) class GhObjectTreeEntry with EquatableMixin { - GhObjectTreeEntry({this.type, this.name}); + GhObjectTreeEntry({this.type, this.name, this.object}); factory GhObjectTreeEntry.fromJson(Map json) => _$GhObjectTreeEntryFromJson(json); @@ -7365,46 +7402,13 @@ class GhObjectTreeEntry with EquatableMixin { String name; + GhObjectGitObject object; + @override - List get props => [type, name]; + List get props => [type, name, object]; Map toJson() => _$GhObjectTreeEntryToJson(this); } -@JsonSerializable(explicitToJson: true) -class GhObjectNode with EquatableMixin { - GhObjectNode(); - - factory GhObjectNode.fromJson(Map json) => - _$GhObjectNodeFromJson(json); - - @JsonKey(name: '__typename') - String resolveType; - - @override - List get props => [resolveType]; - Map toJson() => _$GhObjectNodeToJson(this); -} - -@JsonSerializable(explicitToJson: true) -class GhObjectBlob - with EquatableMixin - implements GhObjectNode, GhObjectGitObject { - GhObjectBlob({this.text}); - - factory GhObjectBlob.fromJson(Map json) => - _$GhObjectBlobFromJson(json); - - String text; - - @override - @JsonKey(name: '__typename') - String resolveType; - - @override - List get props => [text, resolveType]; - Map toJson() => _$GhObjectBlobToJson(this); -} - @JsonSerializable(explicitToJson: true) class GhObjectPinnableItem with EquatableMixin { GhObjectPinnableItem(); @@ -7603,6 +7607,26 @@ class GhObjectQuery extends GraphQLQuery { arguments: [], directives: [], selectionSet: null), + InlineFragmentNode( + typeCondition: TypeConditionNode( + on: NamedTypeNode( + name: NameNode(value: 'Blob'), + isNonNull: false)), + directives: [], + selectionSet: SelectionSetNode(selections: [ + FieldNode( + name: NameNode(value: 'text'), + alias: null, + arguments: [], + directives: [], + selectionSet: null), + FieldNode( + name: NameNode(value: 'byteSize'), + alias: null, + arguments: [], + directives: [], + selectionSet: null) + ])), InlineFragmentNode( typeCondition: TypeConditionNode( on: NamedTypeNode( @@ -7627,22 +7651,39 @@ class GhObjectQuery extends GraphQLQuery { alias: null, arguments: [], directives: [], - selectionSet: null) + selectionSet: null), + FieldNode( + name: NameNode(value: 'object'), + alias: null, + arguments: [], + directives: [], + selectionSet: + SelectionSetNode(selections: [ + FieldNode( + name: NameNode(value: '__typename'), + alias: null, + arguments: [], + directives: [], + selectionSet: null), + InlineFragmentNode( + typeCondition: TypeConditionNode( + on: NamedTypeNode( + name: + NameNode(value: 'Blob'), + isNonNull: false)), + directives: [], + selectionSet: + SelectionSetNode(selections: [ + FieldNode( + name: NameNode( + value: 'byteSize'), + alias: null, + arguments: [], + directives: [], + selectionSet: null) + ])) + ])) ])) - ])), - InlineFragmentNode( - typeCondition: TypeConditionNode( - on: NamedTypeNode( - name: NameNode(value: 'Blob'), - isNonNull: false)), - directives: [], - selectionSet: SelectionSetNode(selections: [ - FieldNode( - name: NameNode(value: 'text'), - alias: null, - arguments: [], - directives: [], - selectionSet: null) ])) ])) ])) diff --git a/lib/graphql/gh.g.dart b/lib/graphql/gh.g.dart index e73cf2c..c658d83 100644 --- a/lib/graphql/gh.g.dart +++ b/lib/graphql/gh.g.dart @@ -2947,6 +2947,29 @@ Map _$GhObjectGitObjectToJson(GhObjectGitObject instance) => '__typename': instance.resolveType, }; +GhObjectBlob _$GhObjectBlobFromJson(Map json) { + return GhObjectBlob( + text: json['text'] as String, + byteSize: json['byteSize'] as int, + )..resolveType = json['__typename'] as String; +} + +Map _$GhObjectBlobToJson(GhObjectBlob instance) => + { + 'text': instance.text, + 'byteSize': instance.byteSize, + '__typename': instance.resolveType, + }; + +GhObjectNode _$GhObjectNodeFromJson(Map json) { + return GhObjectNode()..resolveType = json['__typename'] as String; +} + +Map _$GhObjectNodeToJson(GhObjectNode instance) => + { + '__typename': instance.resolveType, + }; + GhObjectTree _$GhObjectTreeFromJson(Map json) { return GhObjectTree( entries: (json['entries'] as List) @@ -2967,6 +2990,9 @@ GhObjectTreeEntry _$GhObjectTreeEntryFromJson(Map json) { return GhObjectTreeEntry( type: json['type'] as String, name: json['name'] as String, + object: json['object'] == null + ? null + : GhObjectGitObject.fromJson(json['object'] as Map), ); } @@ -2974,27 +3000,7 @@ Map _$GhObjectTreeEntryToJson(GhObjectTreeEntry instance) => { 'type': instance.type, 'name': instance.name, - }; - -GhObjectNode _$GhObjectNodeFromJson(Map json) { - return GhObjectNode()..resolveType = json['__typename'] as String; -} - -Map _$GhObjectNodeToJson(GhObjectNode instance) => - { - '__typename': instance.resolveType, - }; - -GhObjectBlob _$GhObjectBlobFromJson(Map json) { - return GhObjectBlob( - text: json['text'] as String, - )..resolveType = json['__typename'] as String; -} - -Map _$GhObjectBlobToJson(GhObjectBlob instance) => - { - 'text': instance.text, - '__typename': instance.resolveType, + 'object': instance.object?.toJson(), }; GhObjectPinnableItem _$GhObjectPinnableItemFromJson(Map json) { diff --git a/lib/graphql/gh_object.graphql b/lib/graphql/gh_object.graphql index 4088733..0bd97f1 100644 --- a/lib/graphql/gh_object.graphql +++ b/lib/graphql/gh_object.graphql @@ -2,15 +2,22 @@ query GhObject($owner: String!, $name: String!, $expression: String!) { repository(owner: $owner, name: $name) { object(expression: $expression) { __typename + ... on Blob { + text + byteSize # x + } ... on Tree { entries { type name + object { + __typename + ... on Blob { + byteSize + } + } } } - ... on Blob { - text - } } } } diff --git a/lib/models/gitea.dart b/lib/models/gitea.dart index 9a3fe3f..ca5be31 100644 --- a/lib/models/gitea.dart +++ b/lib/models/gitea.dart @@ -37,6 +37,7 @@ class GiteaTree { String type; String name; String path; + int size; GiteaTree(); factory GiteaTree.fromJson(Map json) => _$GiteaTreeFromJson(json); diff --git a/lib/models/gitea.g.dart b/lib/models/gitea.g.dart index 0011b00..3269e2c 100644 --- a/lib/models/gitea.g.dart +++ b/lib/models/gitea.g.dart @@ -63,13 +63,15 @@ GiteaTree _$GiteaTreeFromJson(Map json) { return GiteaTree() ..type = json['type'] as String ..name = json['name'] as String - ..path = json['path'] as String; + ..path = json['path'] as String + ..size = json['size'] as int; } Map _$GiteaTreeToJson(GiteaTree instance) => { 'type': instance.type, 'name': instance.name, 'path': instance.path, + 'size': instance.size, }; GiteaBlob _$GiteaBlobFromJson(Map json) { @@ -77,6 +79,7 @@ GiteaBlob _$GiteaBlobFromJson(Map json) { ..type = json['type'] as String ..name = json['name'] as String ..path = json['path'] as String + ..size = json['size'] as int ..content = json['content'] as String; } @@ -84,5 +87,6 @@ Map _$GiteaBlobToJson(GiteaBlob instance) => { 'type': instance.type, 'name': instance.name, 'path': instance.path, + 'size': instance.size, 'content': instance.content, }; diff --git a/lib/screens/gitea_object.dart b/lib/screens/gitea_object.dart index 9463ed1..60039d2 100644 --- a/lib/screens/gitea_object.dart +++ b/lib/screens/gitea_object.dart @@ -56,6 +56,7 @@ class GiteaObjectScreen extends StatelessWidget { return ObjectTreeItem( name: v.name, type: v.type, + size: v.type == 'file' ? v.size : null, url: '/gitea/$owner/$name/blob?path=${v.path.urlencode}', ); }), diff --git a/lib/screens/object.dart b/lib/screens/object.dart index fbaf09e..4346dfc 100644 --- a/lib/screens/object.dart +++ b/lib/screens/object.dart @@ -94,8 +94,15 @@ class ObjectScreen extends StatelessWidget { items: (data as GhObjectTree).entries.map((v) { // if (item.type == 'commit') return null; final p = [...paths, v.name].join('/').urlencode; - final url = '/$owner/$name/blob/$branch?path=$p'; - return ObjectTreeItem(name: v.name, type: v.type, url: url); + + return ObjectTreeItem( + name: v.name, + type: v.type, + url: '/$owner/$name/blob/$branch?path=$p', + size: v.object.resolveType == 'Blob' + ? (v.object as GhObjectBlob).byteSize + : null, + ); }), ); case 'Blob': diff --git a/lib/widgets/object_tree.dart b/lib/widgets/object_tree.dart index 984adea..949b59a 100644 --- a/lib/widgets/object_tree.dart +++ b/lib/widgets/object_tree.dart @@ -1,3 +1,4 @@ +import 'package:filesize/filesize.dart'; import 'package:flutter/material.dart'; import 'package:git_touch/utils/utils.dart'; import 'package:git_touch/widgets/table_view.dart'; @@ -8,10 +9,12 @@ class ObjectTreeItem { final String url; final String name; final String type; + final int size; ObjectTreeItem({ @required this.name, @required this.url, @required this.type, + this.size, }); } @@ -51,7 +54,9 @@ class ObjectTree extends StatelessWidget { TableViewItem( leftWidget: _buildIcon(item), text: Text(item.name), + rightWidget: item.size == null ? null : Text(filesize(item.size)), url: item.url, + hideRightChevron: item.size != null, ) ], );