fix sorting

This commit is contained in:
Scott E. Graves 2025-02-28 22:45:32 -06:00
parent e446757f7e
commit 343c324050

View File

@ -11,9 +11,20 @@ class MountList with ChangeNotifier {
_fetch(); _fetch();
} }
List<MountConfig> _items = []; List<MountConfig> _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<void> _fetch() async { Future<void> _fetch() async {
final response = await http.get( final response = await http.get(
@ -21,16 +32,16 @@ class MountList with ChangeNotifier {
); );
if (response.statusCode == 200) { if (response.statusCode == 200) {
List<MountConfig> items = []; List<MountConfig> nextList = [];
var data = jsonDecode(response.body); var data = jsonDecode(response.body);
data.forEach((key, value) { data.forEach((key, value) {
items.addAll( nextList.addAll(
value.map((name) => MountConfig.fromJson(key, name)).toList(), value.map((name) => MountConfig.fromJson(key, name)).toList(),
); );
}); });
items.sort((a, b) => a.name.compareTo(b.name)); _sort(nextList);
_items = items; _mountList = nextList;
notifyListeners(); notifyListeners();
return; return;
@ -38,18 +49,18 @@ class MountList with ChangeNotifier {
} }
void add(MountConfig config) { 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) { if (item != null) {
throw DuplicateMountException(name: config.name); throw DuplicateMountException(name: config.name);
} }
_items.add(config); _mountList.add(config);
_items.sort((a, b) => a.name.compareTo(b.name)); _sort(_mountList);
notifyListeners(); notifyListeners();
} }
void remove(String name) { void remove(String name) {
_items.removeWhere((item) => item.name == name); _mountList.removeWhere((item) => item.name == name);
} }
} }