From 65f71eef1a528b8bfbf36d21fdc1c5a50ae7e30b Mon Sep 17 00:00:00 2001 From: shilangyu Date: Wed, 16 Sep 2020 00:26:36 +0200 Subject: [PATCH] replace full text match with fuzzy match + sort according to best match --- lib/pages/communities_tab.dart | 14 ++++++++++---- pubspec.lock | 14 ++++++++++++++ pubspec.yaml | 1 + 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/pages/communities_tab.dart b/lib/pages/communities_tab.dart index 219c719..06fa9f6 100644 --- a/lib/pages/communities_tab.dart +++ b/lib/pages/communities_tab.dart @@ -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 comm) => - comm.where((e) => e.communityName - .toLowerCase() - .contains(filterController.text.toLowerCase())); + filterCommunities(List 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(); diff --git a/pubspec.lock b/pubspec.lock index 1b60dcf..0528917 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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: diff --git a/pubspec.yaml b/pubspec.yaml index e22a9d3..da1e0c4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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