From 343c3240508ff2504d1428f0d2b4b5fdacba42ed Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Fri, 28 Feb 2025 22:45:32 -0600 Subject: [PATCH] fix sorting --- web/repertory/lib/models/mount_list.dart | 31 ++++++++++++++++-------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/web/repertory/lib/models/mount_list.dart b/web/repertory/lib/models/mount_list.dart index 1ec7fb51..d82b3f4e 100644 --- a/web/repertory/lib/models/mount_list.dart +++ b/web/repertory/lib/models/mount_list.dart @@ -11,9 +11,20 @@ class MountList with ChangeNotifier { _fetch(); } - List _items = []; + List _mountList = []; - UnmodifiableListView get items => UnmodifiableListView(_items); + UnmodifiableListView get items => UnmodifiableListView(_mountList); + + void _sort(list) { + list.sort((a, b) { + final res = a.type.compareTo(b.type); + if (res != 0) { + return res; + } + + return a.name.compareTo(b.name); + }); + } Future _fetch() async { final response = await http.get( @@ -21,16 +32,16 @@ class MountList with ChangeNotifier { ); if (response.statusCode == 200) { - List items = []; + List nextList = []; var data = jsonDecode(response.body); data.forEach((key, value) { - items.addAll( + nextList.addAll( value.map((name) => MountConfig.fromJson(key, name)).toList(), ); }); - items.sort((a, b) => a.name.compareTo(b.name)); - _items = items; + _sort(nextList); + _mountList = nextList; notifyListeners(); return; @@ -38,18 +49,18 @@ class MountList with ChangeNotifier { } void add(MountConfig config) { - var item = _items.firstWhereOrNull((cfg) => cfg.name == config.name); + var item = _mountList.firstWhereOrNull((cfg) => cfg.name == config.name); if (item != null) { throw DuplicateMountException(name: config.name); } - _items.add(config); - _items.sort((a, b) => a.name.compareTo(b.name)); + _mountList.add(config); + _sort(_mountList); notifyListeners(); } void remove(String name) { - _items.removeWhere((item) => item.name == name); + _mountList.removeWhere((item) => item.name == name); } }