Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more search options #845

Merged
merged 5 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Support new scaled and controversial sort types - contribution from @micahmo
- Added support to open Lemmy links in app. Android only. - contribution from @ggichure
- Added support for receiving share intents. Android only. - contribution from @ggichure
- Add more search options - contribution from @micahmo

### Changed
- Collapsed comments are easier to expand - contribution from @micahmo
Expand Down
12 changes: 12 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@
"removeInstance": "Remove instance",
"@removeInstance": {},
"searchCommunitiesFederatedWith": "Search for communities federated with {instance}",
"searchUsersFederatedWith": "Search for users federated with {instance}",
"noCommunitiesFound": "No communities found",
"noUsersFound": "No users found",
"allPosts": "All Posts",
"clearSearch": "Clear Search",
"selectSearchType": "Select Search Type",
"communities": "Communities",
"users": "Users",
"localPosts": "Local Posts",
"empty": "Empty",
"countSubscribers": "{count} subscribers",
"selectFeedType": "Select Feed Type",
"@searchCommunitiesFederatedWith": {},
"searchInstance": "Search {instance}",
"@searchInstance": {},
Expand Down
32 changes: 27 additions & 5 deletions lib/search/bloc/search_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ class SearchBloc extends Bloc<SearchEvent, SearchState> {
page: 1,
limit: 15,
sort: event.sortType,
listingType: event.listingType,
type: event.searchType,
));

// If there are no search results, see if this is an exact search
if (searchResponse.communities.isEmpty) {
if (event.searchType == SearchType.communities && searchResponse.communities.isEmpty) {
// Note: We could jump straight to GetCommunity here.
// However, getLemmyCommunity has a nice instance check that can short-circuit things
// if the instance is not valid to start.
Expand All @@ -90,7 +92,26 @@ class SearchBloc extends Bloc<SearchEvent, SearchState> {
}
}

return emit(state.copyWith(status: SearchStatus.success, communities: searchResponse.communities, page: 2));
// Check for exact user search
if (event.searchType == SearchType.users && searchResponse.users.isEmpty) {
String? userName = await getLemmyUser(event.query);
if (userName != null) {
try {
Account? account = await fetchActiveProfileAccount();

final getCommunityResponse = await LemmyClient.instance.lemmyApiV3.run(GetPersonDetails(
username: userName,
auth: account?.jwt,
));

searchResponse = searchResponse.copyWith(users: [getCommunityResponse.personView]);
} catch (e) {
// Ignore any exceptions here and return an empty response below
}
}
}

return emit(state.copyWith(status: SearchStatus.success, communities: searchResponse.communities, users: searchResponse.users, page: 2));
} catch (e) {
return emit(state.copyWith(status: SearchStatus.failure, errorMessage: e.toString()));
}
Expand All @@ -104,7 +125,7 @@ class SearchBloc extends Bloc<SearchEvent, SearchState> {

while (attemptCount < 2) {
try {
emit(state.copyWith(status: SearchStatus.refreshing, communities: state.communities));
emit(state.copyWith(status: SearchStatus.refreshing, communities: state.communities, users: state.users));

Account? account = await fetchActiveProfileAccount();
LemmyApiV3 lemmy = LemmyClient.instance.lemmyApiV3;
Expand All @@ -117,14 +138,15 @@ class SearchBloc extends Bloc<SearchEvent, SearchState> {
sort: event.sortType,
));

if (searchResponse.communities.isEmpty) {
if ((event.searchType == SearchType.communities && searchResponse.communities.isEmpty) || event.searchType == SearchType.users && searchResponse.users.isEmpty) {
return emit(state.copyWith(status: SearchStatus.done));
}

// Append the search results
state.communities = [...state.communities ?? [], ...searchResponse.communities];
state.users = [...state.users ?? [], ...searchResponse.users];

return emit(state.copyWith(status: SearchStatus.success, communities: state.communities, page: state.page + 1));
return emit(state.copyWith(status: SearchStatus.success, communities: state.communities, users: state.users, page: state.page + 1));
} catch (e) {
exception = e;
attemptCount++;
Expand Down
7 changes: 5 additions & 2 deletions lib/search/bloc/search_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ abstract class SearchEvent extends Equatable {
class StartSearchEvent extends SearchEvent {
final String query;
final SortType sortType;
final ListingType listingType;
final SearchType searchType;

const StartSearchEvent({required this.query, required this.sortType});
const StartSearchEvent({required this.query, required this.sortType, required this.listingType, required this.searchType});
}

class ChangeCommunitySubsciptionStatusEvent extends SearchEvent {
Expand All @@ -27,8 +29,9 @@ class ResetSearch extends SearchEvent {}
class ContinueSearchEvent extends SearchEvent {
final String query;
final SortType sortType;
final SearchType searchType;

const ContinueSearchEvent({required this.query, required this.sortType});
const ContinueSearchEvent({required this.query, required this.sortType, required this.searchType});
}

class FocusSearchEvent extends SearchEvent {}
Expand Down
6 changes: 5 additions & 1 deletion lib/search/bloc/search_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class SearchState extends Equatable {
this.status = SearchStatus.initial,
this.communities,
this.trendingCommunities,
this.users,
this.errorMessage,
this.page = 1,
this.sortType,
Expand All @@ -16,6 +17,7 @@ class SearchState extends Equatable {
final SearchStatus status;
List<CommunityView>? communities;
List<CommunityView>? trendingCommunities;
List<PersonView>? users;

final String? errorMessage;

Expand All @@ -28,6 +30,7 @@ class SearchState extends Equatable {
SearchStatus? status,
List<CommunityView>? communities,
List<CommunityView>? trendingCommunities,
List<PersonView>? users,
String? errorMessage,
int? page,
SortType? sortType,
Expand All @@ -37,6 +40,7 @@ class SearchState extends Equatable {
status: status ?? this.status,
communities: communities ?? this.communities,
trendingCommunities: trendingCommunities ?? this.trendingCommunities,
users: users ?? this.users,
errorMessage: errorMessage,
page: page ?? this.page,
sortType: sortType ?? this.sortType,
Expand All @@ -45,5 +49,5 @@ class SearchState extends Equatable {
}

@override
List<Object?> get props => [status, communities, trendingCommunities, errorMessage, page, focusSearchId];
List<Object?> get props => [status, communities, trendingCommunities, users, errorMessage, page, focusSearchId];
}
Loading
Loading