sort construct list on construct list update, don't repeat sort operation

pull/1490/head
ggurdin 1 year ago
parent f1a782eac5
commit 4a518e053b
No known key found for this signature in database
GPG Key ID: A01CB41737CBB478

@ -52,16 +52,21 @@ class ConstructListModel {
_updateMetrics(); _updateMetrics();
} }
int _sortConstructs(ConstructUses a, ConstructUses b) {
final comp = b.points.compareTo(a.points);
if (comp != 0) return comp;
return a.lemma.compareTo(b.lemma);
}
/// A map of lemmas to ConstructUses, each of which contains a lemma /// A map of lemmas to ConstructUses, each of which contains a lemma
/// key = lemmma + constructType.string, value = ConstructUses /// key = lemmma + constructType.string, value = ConstructUses
void _updateConstructMap(final List<OneConstructUse> newUses) { void _updateConstructMap(final List<OneConstructUse> newUses) {
for (final use in newUses) { for (final use in newUses) {
if (use.lemma == null) continue;
final currentUses = _constructMap[use.identifier.string] ?? final currentUses = _constructMap[use.identifier.string] ??
ConstructUses( ConstructUses(
uses: [], uses: [],
constructType: use.constructType, constructType: use.constructType,
lemma: use.lemma!, lemma: use.lemma,
category: use.category, category: use.category,
); );
currentUses.uses.add(use); currentUses.uses.add(use);
@ -75,11 +80,7 @@ class ConstructListModel {
void _updateConstructList() { void _updateConstructList() {
// TODO check how expensive this is // TODO check how expensive this is
_constructList = _constructMap.values.toList(); _constructList = _constructMap.values.toList();
_constructList.sort((a, b) { _constructList.sort(_sortConstructs);
final comp = b.uses.length.compareTo(a.uses.length);
if (comp != 0) return comp;
return a.lemma.compareTo(b.lemma);
});
} }
void _updateCategoriesToUses() { void _updateCategoriesToUses() {

@ -1,4 +1,3 @@
import 'package:collection/collection.dart';
import 'package:fluffychat/pangea/enum/construct_type_enum.dart'; import 'package:fluffychat/pangea/enum/construct_type_enum.dart';
import 'package:fluffychat/pangea/enum/progress_indicators_enum.dart'; import 'package:fluffychat/pangea/enum/progress_indicators_enum.dart';
import 'package:fluffychat/pangea/models/analytics/construct_list_model.dart'; import 'package:fluffychat/pangea/models/analytics/construct_list_model.dart';
@ -94,11 +93,7 @@ class AnalyticsPopupState extends State<AnalyticsPopup> {
dialogContent = Center(child: Text(L10n.of(context)!.noDataFound)); dialogContent = Center(child: Text(L10n.of(context)!.noDataFound));
} else if (hasNoCategories || !widget.showGroups) { } else if (hasNoCategories || !widget.showGroups) {
dialogContent = ConstructsTileList( dialogContent = ConstructsTileList(
_constructsModel.constructList(type: widget.type).sorted((a, b) { _constructsModel.constructList(type: widget.type),
final comp = b.points.compareTo(a.points);
if (comp != 0) return comp;
return a.lemma.compareTo(b.lemma);
}),
); );
} else { } else {
dialogContent = ListView.builder( dialogContent = ListView.builder(
@ -156,8 +151,6 @@ class ConstructsTileList extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// sort list by xp
constructs.sort((a, b) => b.points.compareTo(a.points));
return ListView.builder( return ListView.builder(
itemCount: constructs.length, itemCount: constructs.length,
itemBuilder: (context, index) => ConstructUsesXPTile(constructs[index]), itemBuilder: (context, index) => ConstructUsesXPTile(constructs[index]),

Loading…
Cancel
Save