From 146f731c95141e3cecf134315b339988569d94bf Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Thu, 30 Jan 2020 19:24:59 +0800 Subject: [PATCH] improvement: sort tree items --- lib/screens/gitea_object.dart | 16 +++++++++------- lib/screens/object.dart | 10 +--------- lib/utils/utils.dart | 6 ++++++ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/screens/gitea_object.dart b/lib/screens/gitea_object.dart index 60039d2..1c7c98e 100644 --- a/lib/screens/gitea_object.dart +++ b/lib/screens/gitea_object.dart @@ -50,17 +50,19 @@ class GiteaObjectScreen extends StatelessWidget { }, bodyBuilder: (p, _) { if (p is List) { - return ObjectTree( - items: p.map((t) { - final v = GiteaTree.fromJson(t); - return ObjectTreeItem( + final items = p.map((t) => GiteaTree.fromJson(t)).toList(); + items.sort((a, b) { + return sortByKey('dir', a.type, b.type); + }); + return ObjectTree(items: [ + for (var v in items) + ObjectTreeItem( name: v.name, type: v.type, size: v.type == 'file' ? v.size : null, url: '/gitea/$owner/$name/blob?path=${v.path.urlencode}', - ); - }), - ); + ), + ]); } else { final v = GiteaBlob.fromJson(p); return BlobView(v.name, base64Text: v.content); diff --git a/lib/screens/object.dart b/lib/screens/object.dart index 4346dfc..b765642 100644 --- a/lib/screens/object.dart +++ b/lib/screens/object.dart @@ -57,19 +57,11 @@ class ObjectScreen extends StatelessWidget { ), )); final data = res.data.repository.object; - if (data.resolveType == 'Tree') { (data as GhObjectTree).entries.sort((a, b) { - if (a.type == 'tree' && b.type == 'blob') { - return -1; - } - if (a.type == 'blob' && b.type == 'tree') { - return 1; - } - return 0; + return sortByKey('tree', a.type, b.type); }); } - return data; }, actionBuilder: (data, _) { diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index e6ccedb..b1afe92 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -194,3 +194,9 @@ String convertBase64ToString(String input) { final bits = base64.decode(input.replaceAll('\n', '')); return utf8.decode(bits); } + +int sortByKey(T key, T a, T b) { + if (a == key && b != key) return -1; + if (a != key && b == key) return 1; + return 0; +}