Skip to content

Commit

Permalink
Merge pull request #400 from EgidioCaprino/search-filters
Browse files Browse the repository at this point in the history
add search filters with sorting option
  • Loading branch information
lamarios authored Dec 8, 2023
2 parents da35696 + 607651f commit 621bfb7
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,10 @@
"@shorts": {
"description": "Youtube shorts"
},
"searchSortBy": "Sort by",
"@searchSortBy": {
"description": "Search sorting option"
},
"searchSortRelevance": "Relevance",
"@searchSortRelevance": {
"description": "Sort search by relevance"
Expand Down
15 changes: 13 additions & 2 deletions lib/search/models/search_sort_by.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

enum SearchSortBy {
relevance,
rating,
upload_date,
view_count;
date,
views;

String getLable(AppLocalizations locals) {
return switch(this) {
(SearchSortBy.relevance) => locals.searchSortRelevance,
(SearchSortBy.rating) => locals.searchSortRating,
(SearchSortBy.date) => locals.searchSortUploadDate,
(SearchSortBy.views) => locals.searchSortViewCount,
};
}
}
74 changes: 74 additions & 0 deletions lib/search/search_filters.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:invidious/search/models/search_sort_by.dart';

const defaultSearchSortBy = SearchSortBy.relevance;

class SearchFiltersButton extends StatelessWidget {
final SearchSortBy initialSearchSortBy;
final Function(SearchSortBy) callback;

const SearchFiltersButton({
super.key,
required this.initialSearchSortBy,
required this.callback,
});

@override
Widget build(BuildContext context) {
final locals = AppLocalizations.of(context)!;
return IconButton(
onPressed: () {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
SearchSortBy selectedSearchSortBy = initialSearchSortBy;
return AlertDialog(
content: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
onChanged(SearchSortBy? newSearchSortBy) {
setState(() => selectedSearchSortBy =
newSearchSortBy ?? defaultSearchSortBy);
}

return SingleChildScrollView(
child: ListBody(children: <Widget>[
Text(locals.searchSortBy),
...SearchSortBy.values.map((value) {
return ListTile(
title: Text(value.getLable(locals)),
leading: Radio<SearchSortBy>(
value: value,
groupValue: selectedSearchSortBy,
onChanged: onChanged,
),
);
}),
]),
);
},
),
actions: <Widget>[
TextButton(
child: Text(locals.cancel),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: Text(locals.ok),
onPressed: () {
callback(selectedSearchSortBy);
Navigator.of(context).pop();
},
),
],
);
},
);
},
icon: const Icon(Icons.filter_list_alt),
);
}
}
4 changes: 4 additions & 0 deletions lib/search/states/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class SearchCubit<T extends SearchState> extends Cubit<SearchState> {
state.selectedIndex = value;
emit(state);
}

void setSearchSortBy(SearchSortBy value) {
emit(state.copyWith(sortBy: value));
}
}

abstract class Clonable<T> {
Expand Down
11 changes: 11 additions & 0 deletions lib/search/views/screens/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:invidious/globals.dart';
import 'package:invidious/playlists/views/components/playlist_list.dart';
import 'package:invidious/router.dart';
import 'package:invidious/search/models/search_type.dart';
import 'package:invidious/search/search_filters.dart';
import 'package:invidious/utils/views/components/navigation_switcher.dart';
import 'package:invidious/videos/models/video_in_list.dart';

Expand Down Expand Up @@ -72,6 +73,15 @@ class SearchScreen extends StatelessWidget {
}
},
icon: const Icon(Icons.clear)),
SearchFiltersButton(
initialSearchSortBy: _.sortBy,
callback: (newSearchSortBy) {
cubit.setSearchSortBy(newSearchSortBy);
if (_.showResults || _.queryController.text.isNotEmpty) {
cubit.search(_.queryController.text);
}
},
),
],
),
body: SafeArea(
Expand Down Expand Up @@ -145,6 +155,7 @@ class SearchScreen extends StatelessWidget {
child: NavigationSwitcher(
child: [
VideoList(
key: UniqueKey(),
paginatedVideoList: PageBasedPaginatedList<VideoInList>(
getItemsFunc: (page, maxResults) => service
.search(_.queryController.value.text,
Expand Down
8 changes: 7 additions & 1 deletion lib/service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,13 @@ class Service {

Future<SearchResults> search(String query, {SearchType? type, int? page, SearchSortBy? sortBy}) async {
String countryCode = db.getSettings(BROWSING_COUNTRY)?.value ?? 'US';
Uri uri = buildUrl(urlSearch, query: {'q': Uri.encodeQueryComponent(query), 'type': type?.name, 'page': page?.toString() ?? '1', 'sort_by': sortBy?.name, 'region': countryCode});
Uri uri = buildUrl(urlSearch, query: {
'q': Uri.encodeQueryComponent(query),
'type': type?.name,
'page': page?.toString() ?? '1',
'sort': sortBy?.name,
'region': countryCode,
});
final response = await http.get(uri);
Iterable i = handleResponse(response);
// only getting videos for now
Expand Down

0 comments on commit 621bfb7

Please sign in to comment.