From 22d426a209deb4115be4eaae4cf5c60205fe4d19 Mon Sep 17 00:00:00 2001 From: caro Date: Wed, 2 Nov 2022 09:43:44 +0100 Subject: [PATCH] Issue #20 Fluffychat: react on click on citation in search result --- lib/pages/chat_search/chat_search.dart | 38 ++++++++++++++------- lib/pages/chat_search/chat_search_view.dart | 1 + 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/pages/chat_search/chat_search.dart b/lib/pages/chat_search/chat_search.dart index 91d98a19bb..f0728cbcac 100644 --- a/lib/pages/chat_search/chat_search.dart +++ b/lib/pages/chat_search/chat_search.dart @@ -26,10 +26,12 @@ class ChatSearchController extends State { Stream>? searchResultStream; final TextEditingController searchController = TextEditingController(); String? searchError; - String lastSearchTerm = ""; SearchState searchState = SearchState.noResult; bool searchResultsFound = false; - String searchTerm = ""; + + String _searchTerm = ""; + String _lastSearchTerm = ""; + List _foundMessages = []; final AutoScrollController scrollController = AutoScrollController(); bool showScrollToTopButton = false; @@ -69,27 +71,35 @@ class ChatSearchController extends State { } bool searchFunction(Event event) { - if (event.type == EventTypes.Message) { - return event.body.toLowerCase().contains(searchTerm.toLowerCase()); - } else { - return false; + // use _foundMessages to filter out messages which have already be found + if (event.type == EventTypes.Message && !_foundMessages.contains(event.eventId)) { + bool found = event.body.toLowerCase().contains(_searchTerm.toLowerCase()); + if(found) { + _foundMessages.add(event.eventId); + return found; + } } + + return false; } void search() async { try { - searchTerm = searchController.text; + _searchTerm = searchController.text; // start search only if a new search term was entered - if (searchTerm != lastSearchTerm) { - lastSearchTerm = searchTerm; + if (_searchTerm != _lastSearchTerm) { + + _lastSearchTerm = _searchTerm; + _foundMessages.clear(); + searchError = null; searchResultsFound = false; - if (searchTerm.isNotEmpty) { + if (_searchTerm.isNotEmpty) { searchResultStream = timeline ?.searchEvent( - searchTerm: searchTerm, + searchTerm: _searchTerm, requestHistoryCount: 30, maxHistoryRequests: 30, searchFunc: searchFunction) @@ -126,10 +136,14 @@ class ChatSearchController extends State { void unfold(String eventId) {} void onSelectMessage(Event event) { + scrollToEventId(event.eventId); + } + + void scrollToEventId(String eventId) { VRouter.of(context).path.startsWith('/spaces/') ? VRouter.of(context).pop() : VRouter.of(context).toSegments(['rooms', roomId!], - queryParameters: {'event': event.eventId}); + queryParameters: {'event': eventId}); } void scrollToTop() { diff --git a/lib/pages/chat_search/chat_search_view.dart b/lib/pages/chat_search/chat_search_view.dart index b24eb89d2f..80973f22b9 100644 --- a/lib/pages/chat_search/chat_search_view.dart +++ b/lib/pages/chat_search/chat_search_view.dart @@ -147,6 +147,7 @@ class ChatSearchView extends StatelessWidget { onSwipe: (direction) => {}, unfold: controller.unfold, onSelect: controller.onSelectMessage, + scrollToEventId: controller.scrollToEventId, timeline: controller.timeline!), if(i == snapshot.data?.length && controller.searchState == SearchState.searching) Center(