replace full text match with fuzzy match + sort according to best match

This commit is contained in:
shilangyu 2020-09-16 00:26:36 +02:00
parent 896337bf2f
commit 65f71eef1a
3 changed files with 25 additions and 4 deletions

View File

@ -3,6 +3,7 @@ import 'dart:async';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:fuzzy/fuzzy.dart';
import 'package:lemmy_api_client/lemmy_api_client.dart';
import 'package:provider/provider.dart';
@ -91,10 +92,15 @@ class CommunitiesTab extends HookWidget {
);
}();
filterCommunities(List<CommunityFollowerView> comm) =>
comm.where((e) => e.communityName
.toLowerCase()
.contains(filterController.text.toLowerCase()));
filterCommunities(List<CommunityFollowerView> comm) {
var matches = Fuzzy(
comm.map((e) => e.communityName).toList(),
options: FuzzyOptions(threshold: 0.5),
).search(filterController.text).map((e) => e.item);
return matches
.map((match) => comm.firstWhere((e) => e.communityName == match));
}
toggleCollapse(int i) => isCollapsed.value =
isCollapsed.value.mapWithIndex((e, j) => j == i ? !e : e).toList();

View File

@ -268,6 +268,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
fuzzy:
dependency: "direct main"
description:
name: fuzzy
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.3"
glob:
dependency: transitive
description:
@ -338,6 +345,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
latinize:
dependency: transitive
description:
name: latinize
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.2"
lemmy_api_client:
dependency: "direct main"
description:

View File

@ -29,6 +29,7 @@ dependencies:
cached_network_image: ^2.2.0+1
timeago: ^2.0.27
lemmy_api_client: ^0.4.0
fuzzy: <1.0.0
mobx: ^1.2.1
flutter_mobx: ^1.1.0