From 107ac79eafc7032bd1ef50654d248a6d8f6d7e8c Mon Sep 17 00:00:00 2001 From: HuyNguyen Date: Wed, 10 Jan 2024 16:20:38 +0700 Subject: [PATCH] TW-1302: Apply for search in unencrypted chat --- lib/pages/chat_search/chat_search_view.dart | 45 ++++++++++++------- lib/pages/search/search_view.dart | 9 ++++ .../search/server_search_controller.dart | 19 ++++---- lib/pages/search/server_search_view.dart | 10 ++--- .../search/presentation_server_side.dart | 6 --- ...presentation_server_side_empty_search.dart | 4 +- .../presentation_server_side_search.dart | 4 +- .../presentation_server_side_state.dart | 8 ++++ 8 files changed, 66 insertions(+), 39 deletions(-) delete mode 100644 lib/presentation/model/search/presentation_server_side.dart create mode 100644 lib/presentation/model/search/presentation_server_side_state.dart diff --git a/lib/pages/chat_search/chat_search_view.dart b/lib/pages/chat_search/chat_search_view.dart index ce81c0e810..fb95e9bf63 100644 --- a/lib/pages/chat_search/chat_search_view.dart +++ b/lib/pages/chat_search/chat_search_view.dart @@ -8,6 +8,7 @@ import 'package:fluffychat/pages/chat_list/chat_list_header_style.dart'; import 'package:fluffychat/pages/chat_search/chat_search.dart'; import 'package:fluffychat/pages/chat_search/chat_search_style.dart'; import 'package:fluffychat/pages/search/server_search_controller.dart'; +import 'package:fluffychat/presentation/model/search/presentation_server_side_empty_search.dart'; import 'package:fluffychat/presentation/model/search/presentation_server_side_search.dart'; import 'package:fluffychat/presentation/same_type_events_builder/same_type_events_builder.dart'; import 'package:fluffychat/presentation/same_type_events_builder/same_type_events_controller.dart'; @@ -84,23 +85,35 @@ class _ServerSearchView extends StatelessWidget { ValueListenableBuilder( valueListenable: serverSearchController.searchResultsNotifier, builder: (context, searchResults, child) { - final events = (searchResults as PresentationServerSideSearch) - .searchResults - .map((result) => result.getEvent(context)) - .whereNotNull() - .toList(); - return SliverList.builder( - itemCount: events.length, - itemBuilder: (context, index) { - final event = events[index]; - return _SearchItem( - event: event, - searchWord: controller.serverSearchController.debouncerValue, - onTap: controller.onEventTap, - ); - }, - ); + if (searchResults is PresentationServerSideSearch) { + final events = searchResults.searchResults + .map((result) => result.getEvent(context)) + .whereNotNull() + .toList(); + return SliverList.builder( + itemCount: events.length, + itemBuilder: (context, index) { + final event = events[index]; + return _SearchItem( + event: event, + searchWord: + controller.serverSearchController.debouncerValue, + onTap: controller.onEventTap, + ); + }, + ); + } + + if (searchResults is PresentationServerSideEmptySearch) { + return const SliverToBoxAdapter( + child: EmptySearchWidget(), + ); + } + return child!; }, + child: const SliverToBoxAdapter( + child: SizedBox(), + ), ), ValueListenableBuilder( valueListenable: serverSearchController.isLoadingMoreNotifier, diff --git a/lib/pages/search/search_view.dart b/lib/pages/search/search_view.dart index e3b541239d..38224a199b 100644 --- a/lib/pages/search/search_view.dart +++ b/lib/pages/search/search_view.dart @@ -5,6 +5,7 @@ import 'package:fluffychat/pages/search/recent_item_widget.dart'; import 'package:fluffychat/pages/search/search.dart'; import 'package:fluffychat/pages/search/search_view_style.dart'; import 'package:fluffychat/pages/search/server_search_view.dart'; +import 'package:fluffychat/presentation/model/search/presentation_server_side_empty_search.dart'; import 'package:fluffychat/presentation/model/search/presentation_server_side_search.dart'; import 'package:fluffychat/widgets/twake_components/twake_icon_button.dart'; import 'package:fluffychat/widgets/twake_components/twake_loading/center_loading_indicator.dart'; @@ -74,6 +75,14 @@ class SearchView extends StatelessWidget { valueListenable: searchController.serverSearchController.searchResultsNotifier, builder: ((context, searchResults, child) { + if (searchResults is PresentationServerSideEmptySearch) { + return _SearchHeader( + header: L10n.of(context)!.messages, + searchController: searchController, + needShowMore: false, + ); + } + if (searchResults is PresentationServerSideSearch) { if (searchResults.searchResults.isEmpty) { return child!; diff --git a/lib/pages/search/server_search_controller.dart b/lib/pages/search/server_search_controller.dart index 848d3615bd..36872a9cbd 100644 --- a/lib/pages/search/server_search_controller.dart +++ b/lib/pages/search/server_search_controller.dart @@ -7,7 +7,7 @@ import 'package:fluffychat/di/global/get_it_initializer.dart'; import 'package:fluffychat/domain/model/search/server_side_search_categories.dart'; import 'package:fluffychat/pages/search/search_debouncer_mixin.dart'; import 'package:fluffychat/domain/usecase/search/server_search_interactor.dart'; -import 'package:fluffychat/presentation/model/search/presentation_server_side.dart'; +import 'package:fluffychat/presentation/model/search/presentation_server_side_state.dart'; import 'package:fluffychat/presentation/model/search/presentation_server_side_empty_search.dart'; import 'package:fluffychat/presentation/model/search/presentation_server_side_search.dart'; import 'package:flutter/material.dart'; @@ -23,8 +23,8 @@ class ServerSearchController with SearchDebouncerMixin { final _serverSearchInteractor = getIt.get(); - final searchResultsNotifier = ValueNotifier( - PresentationServerSide(), + final searchResultsNotifier = ValueNotifier( + PresentationServerSideInitial(), ); static const int _limitServerSideSearchFilter = 20; @@ -79,8 +79,10 @@ class ServerSearchController with SearchDebouncerMixin { } else { searchResultsNotifier.value = PresentationServerSideSearch( searchResults: [ - ...(searchResultsNotifier.value as PresentationServerSideSearch) - .searchResults, + if (searchResultsNotifier.value is PresentationServerSideSearch) + ...(searchResultsNotifier.value + as PresentationServerSideSearch) + .searchResults, ...success.results ?? [], ], ); @@ -134,9 +136,10 @@ class ServerSearchController with SearchDebouncerMixin { void loadMore() { if (_searchCategories == null || isLoadingMoreNotifier.value || - (searchResultsNotifier.value as PresentationServerSideSearch) - .searchResults - .isEmpty || + ((searchResultsNotifier.value is PresentationServerSideSearch) && + (searchResultsNotifier.value as PresentationServerSideSearch) + .searchResults + .isEmpty) || _nextBatch == null) { return; } diff --git a/lib/pages/search/server_search_view.dart b/lib/pages/search/server_search_view.dart index e5d429e1cc..753ed99c63 100644 --- a/lib/pages/search/server_search_view.dart +++ b/lib/pages/search/server_search_view.dart @@ -26,20 +26,20 @@ class ServerSearchMessagesList extends StatelessWidget { child: ValueListenableBuilder( valueListenable: searchController.serverSearchController.searchResultsNotifier, - builder: (context, severSearchNotifier, child) { - if (severSearchNotifier is PresentationServerSideEmptySearch) { + builder: (context, serverSearchNotifier, child) { + if (serverSearchNotifier is PresentationServerSideEmptySearch) { return child!; } - if (severSearchNotifier is PresentationServerSideSearch) { + if (serverSearchNotifier is PresentationServerSideSearch) { return ListView.builder( shrinkWrap: true, physics: const ClampingScrollPhysics(), - itemCount: severSearchNotifier.searchResults.length, + itemCount: serverSearchNotifier.searchResults.length, padding: ServerSearchViewStyle.paddingListItem, itemBuilder: ((context, index) { final searchResult = - severSearchNotifier.searchResults[index].result; + serverSearchNotifier.searchResults[index].result; final room = Matrix.of(context).client.getRoomById( searchResult?.roomId ?? '', ); diff --git a/lib/presentation/model/search/presentation_server_side.dart b/lib/presentation/model/search/presentation_server_side.dart deleted file mode 100644 index 8a88dc8edc..0000000000 --- a/lib/presentation/model/search/presentation_server_side.dart +++ /dev/null @@ -1,6 +0,0 @@ -import 'package:equatable/equatable.dart'; - -class PresentationServerSide with EquatableMixin { - @override - List get props => []; -} diff --git a/lib/presentation/model/search/presentation_server_side_empty_search.dart b/lib/presentation/model/search/presentation_server_side_empty_search.dart index 2f02702c13..5f5a074baa 100644 --- a/lib/presentation/model/search/presentation_server_side_empty_search.dart +++ b/lib/presentation/model/search/presentation_server_side_empty_search.dart @@ -1,6 +1,6 @@ -import 'package:fluffychat/presentation/model/search/presentation_server_side.dart'; +import 'package:fluffychat/presentation/model/search/presentation_server_side_state.dart'; -class PresentationServerSideEmptySearch extends PresentationServerSide { +class PresentationServerSideEmptySearch extends PresentationServerSideUIState { @override List get props => []; } diff --git a/lib/presentation/model/search/presentation_server_side_search.dart b/lib/presentation/model/search/presentation_server_side_search.dart index f21ebdf2de..7a28e476f5 100644 --- a/lib/presentation/model/search/presentation_server_side_search.dart +++ b/lib/presentation/model/search/presentation_server_side_search.dart @@ -1,7 +1,7 @@ -import 'package:fluffychat/presentation/model/search/presentation_server_side.dart'; +import 'package:fluffychat/presentation/model/search/presentation_server_side_state.dart'; import 'package:matrix/matrix.dart'; -class PresentationServerSideSearch extends PresentationServerSide { +class PresentationServerSideSearch extends PresentationServerSideUIState { final List searchResults; PresentationServerSideSearch({ diff --git a/lib/presentation/model/search/presentation_server_side_state.dart b/lib/presentation/model/search/presentation_server_side_state.dart new file mode 100644 index 0000000000..839fec4df7 --- /dev/null +++ b/lib/presentation/model/search/presentation_server_side_state.dart @@ -0,0 +1,8 @@ +import 'package:equatable/equatable.dart'; + +abstract class PresentationServerSideUIState with EquatableMixin { + @override + List get props => []; +} + +class PresentationServerSideInitial extends PresentationServerSideUIState {}