Skip to content

Commit

Permalink
Issue krille-chan#20 Fluffychat: react on click on citation in search…
Browse files Browse the repository at this point in the history
… result
  • Loading branch information
carowebtec authored and Matthias committed Dec 2, 2022
1 parent 4923b16 commit 22d426a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
38 changes: 26 additions & 12 deletions lib/pages/chat_search/chat_search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ class ChatSearchController extends State<ChatSearch> {
Stream<List<Event>>? searchResultStream;
final TextEditingController searchController = TextEditingController();
String? searchError;
String lastSearchTerm = "";
SearchState searchState = SearchState.noResult;
bool searchResultsFound = false;
String searchTerm = "";

String _searchTerm = "";
String _lastSearchTerm = "";
List<String> _foundMessages = <String>[];

final AutoScrollController scrollController = AutoScrollController();
bool showScrollToTopButton = false;
Expand Down Expand Up @@ -69,27 +71,35 @@ class ChatSearchController extends State<ChatSearch> {
}

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)
Expand Down Expand Up @@ -126,10 +136,14 @@ class ChatSearchController extends State<ChatSearch> {
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() {
Expand Down
1 change: 1 addition & 0 deletions lib/pages/chat_search/chat_search_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 22d426a

Please sign in to comment.