-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TW-1098 [Search] Load more in Server side Search
- Loading branch information
1 parent
c833418
commit a7fe5f9
Showing
9 changed files
with
228 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
lib/domain/model/search/server_side_search_categories.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:matrix/matrix.dart'; | ||
|
||
class ServerSideSearchCategories extends Equatable { | ||
final String searchTerm; | ||
final SearchFilter? searchFilter; | ||
|
||
const ServerSideSearchCategories({ | ||
required this.searchTerm, | ||
this.searchFilter, | ||
}); | ||
|
||
@override | ||
List<Object?> get props => [searchTerm, searchFilter]; | ||
|
||
Categories get searchCategories { | ||
return Categories( | ||
roomEvents: RoomEventsCriteria( | ||
searchTerm: searchTerm, | ||
groupings: Groupings( | ||
groupBy: [Group(key: GroupKey.roomId)], | ||
), | ||
keys: [KeyKind.contentBody], | ||
orderBy: SearchOrder.recent, | ||
filter: searchFilter, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,137 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:dartz/dartz.dart'; | ||
import 'package:fluffychat/app_state/failure.dart'; | ||
import 'package:fluffychat/app_state/success.dart'; | ||
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_search.dart'; | ||
import 'package:fluffychat/utils/scroll_controller_extension.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:fluffychat/domain/app_state/search/server_search_state.dart'; | ||
import 'package:matrix/matrix.dart'; | ||
|
||
class ServerSearchController with SearchDebouncerMixin { | ||
final _serverSearchInteractor = getIt.get<ServerSearchInteractor>(); | ||
|
||
final serverSearchNotifier = ValueNotifier<Either<Failure, Success>>( | ||
Right(ServerSearchInitial()), | ||
final searchResultsNotifier = ValueNotifier<PresentationServerSideSearch>( | ||
const PresentationServerSideSearch( | ||
searchResults: [], | ||
), | ||
); | ||
|
||
String keyword = ''; | ||
static const int _limitServerSideSearchFilter = 20; | ||
|
||
final isLoadingMoreNotifier = ValueNotifier<bool>(false); | ||
|
||
final scrollController = ScrollController(); | ||
|
||
StreamSubscription? _searchSubscription; | ||
|
||
String? _nextBatch; | ||
|
||
ServerSideSearchCategories? _searchCategories; | ||
|
||
void init() { | ||
initializeDebouncer((searchTerm) { | ||
updateSearchCategories(searchTerm); | ||
resetSearchResults(); | ||
if (searchTerm.isNotEmpty) { | ||
keyword = searchTerm; | ||
searchUnencryptedMessages(searchTerm); | ||
searchUnencryptedMessages(); | ||
} else { | ||
serverSearchNotifier.value = Right(ServerSearchInitial()); | ||
resetSearchResults(); | ||
} | ||
}); | ||
scrollController.addLoadMoreListener(loadMore); | ||
} | ||
|
||
void dispose() { | ||
super.disposeDebouncer(); | ||
scrollController.dispose(); | ||
_searchSubscription?.cancel(); | ||
isLoadingMoreNotifier.dispose(); | ||
resetNextBatch(); | ||
resetSearchResults(); | ||
_searchCategories = null; | ||
} | ||
|
||
void searchUnencryptedMessages(String searchTerm) { | ||
_serverSearchInteractor | ||
.execute( | ||
void _handleListenServerSearch(Either<Failure, Success> searchResult) { | ||
searchResult.fold( | ||
(failure) => resetNextBatch(), | ||
(success) { | ||
if (success is ServerSearchChatSuccess) { | ||
updateNextBatch(success.nextBatch); | ||
searchResultsNotifier.value = PresentationServerSideSearch( | ||
searchResults: [ | ||
...searchResultsNotifier.value.searchResults, | ||
...success.results ?? [], | ||
], | ||
); | ||
} | ||
}, | ||
); | ||
} | ||
|
||
void resetSearchResults() { | ||
searchResultsNotifier.value = const PresentationServerSideSearch( | ||
searchResults: [], | ||
); | ||
} | ||
|
||
void resetNextBatch() { | ||
_nextBatch = null; | ||
Logs().d('ServerSearchController::resetNextBatch(): $_nextBatch'); | ||
} | ||
|
||
void updateNextBatch(String? newBatch) { | ||
_nextBatch = newBatch; | ||
Logs().d('ServerSearchController::updateNextBatch(): $_nextBatch'); | ||
} | ||
|
||
void updateSearchCategories(String searchTerm) { | ||
resetNextBatch(); | ||
_searchCategories = ServerSideSearchCategories( | ||
searchTerm: searchTerm, | ||
) | ||
.listen((searchResult) { | ||
serverSearchNotifier.value = searchResult; | ||
}); | ||
searchFilter: SearchFilter( | ||
limit: _limitServerSideSearchFilter, | ||
), | ||
); | ||
} | ||
|
||
void searchUnencryptedMessages() { | ||
_searchSubscription = _serverSearchInteractor | ||
.execute( | ||
searchCategories: _searchCategories!, | ||
) | ||
.listen( | ||
(searchResult) => _handleListenServerSearch(searchResult), | ||
); | ||
} | ||
|
||
void onSearchBarChanged(String keyword) { | ||
setDebouncerValue(keyword); | ||
} | ||
|
||
void loadMore() { | ||
if (_searchCategories == null || | ||
isLoadingMoreNotifier.value || | ||
searchResultsNotifier.value.searchResults.isEmpty || | ||
_nextBatch == null) { | ||
return; | ||
} | ||
isLoadingMoreNotifier.value = true; | ||
_searchSubscription = _serverSearchInteractor | ||
.execute( | ||
searchCategories: _searchCategories!, | ||
nextBatch: _nextBatch, | ||
) | ||
.listen( | ||
(searchResult) => _handleListenServerSearch(searchResult), | ||
onDone: () { | ||
isLoadingMoreNotifier.value = false; | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
lib/presentation/model/search/presentation_server_side_search.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:matrix/matrix.dart'; | ||
|
||
class PresentationServerSideSearch extends Equatable { | ||
final List<Result> searchResults; | ||
|
||
const PresentationServerSideSearch({ | ||
required this.searchResults, | ||
}); | ||
|
||
@override | ||
List<Object> get props => [searchResults]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.