Skip to content

Commit

Permalink
Merge pull request #522 from lamarios/fix/search-history
Browse files Browse the repository at this point in the history
Fix/search history
  • Loading branch information
lamarios authored Apr 10, 2024
2 parents fbd1fdd + ad952f2 commit 72f7b43
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 32 deletions.
21 changes: 19 additions & 2 deletions lib/search/states/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:easy_debounce/easy_debounce.dart';
import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:invidious/globals.dart';
import 'package:invidious/search/models/db/search_history_item.dart';
import 'package:invidious/search/models/search_sort_by.dart';
import 'package:invidious/search/states/search_filter.dart';

Expand All @@ -22,6 +23,7 @@ class SearchCubit<T extends SearchState> extends Cubit<SearchState> {

void onInit() {
state.queryController.addListener(getSuggestions);
getHistory();
if (state.searchNow) {
search(state.queryController.value.text);
}
Expand Down Expand Up @@ -70,18 +72,32 @@ class SearchCubit<T extends SearchState> extends Cubit<SearchState> {
}
}

List<String> getHistory() {
return settings.state.useSearchHistory ? db.getSearchHistory() : [];
getHistory() {
emit(state.copyWith(
searchHistory:
settings.state.useSearchHistory ? db.getSearchHistory() : []));
}

search(String value) async {
emit(state.copyWith(showResults: true));

final query = state.queryController.text;
if (query.isNotEmpty && settings.state.useSearchHistory) {
await db.addToSearchHistory(SearchHistoryItem(
query, (DateTime.now().millisecondsSinceEpoch / 1000).round()));
}
getHistory();
}

setSearchQuery(String e) {
state.queryController.text = e;
search(e);
}

removeFromHistory(String e) async {
await db.deleteFromSearchHistory(e);
getHistory();
}
}

@freezed
Expand All @@ -95,6 +111,7 @@ class SearchState with _$SearchState {
@Default(1) int videoPage,
@Default(1) int channelPage,
@Default(1) int playlistPage,
@Default([]) List<String> searchHistory,
@Default(SearchFiltersState()) SearchFiltersState filters}) =
_SearchState;

Expand Down
34 changes: 32 additions & 2 deletions lib/search/states/search.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mixin _$SearchState {
int get videoPage => throw _privateConstructorUsedError;
int get channelPage => throw _privateConstructorUsedError;
int get playlistPage => throw _privateConstructorUsedError;
List<String> get searchHistory => throw _privateConstructorUsedError;
SearchFiltersState get filters => throw _privateConstructorUsedError;

@JsonKey(ignore: true)
Expand All @@ -47,6 +48,7 @@ abstract class $SearchStateCopyWith<$Res> {
int videoPage,
int channelPage,
int playlistPage,
List<String> searchHistory,
SearchFiltersState filters});

$SearchFiltersStateCopyWith<$Res> get filters;
Expand All @@ -73,6 +75,7 @@ class _$SearchStateCopyWithImpl<$Res, $Val extends SearchState>
Object? videoPage = null,
Object? channelPage = null,
Object? playlistPage = null,
Object? searchHistory = null,
Object? filters = null,
}) {
return _then(_value.copyWith(
Expand Down Expand Up @@ -108,6 +111,10 @@ class _$SearchStateCopyWithImpl<$Res, $Val extends SearchState>
? _value.playlistPage
: playlistPage // ignore: cast_nullable_to_non_nullable
as int,
searchHistory: null == searchHistory
? _value.searchHistory
: searchHistory // ignore: cast_nullable_to_non_nullable
as List<String>,
filters: null == filters
? _value.filters
: filters // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -141,6 +148,7 @@ abstract class _$$SearchStateImplCopyWith<$Res>
int videoPage,
int channelPage,
int playlistPage,
List<String> searchHistory,
SearchFiltersState filters});

@override
Expand All @@ -166,6 +174,7 @@ class __$$SearchStateImplCopyWithImpl<$Res>
Object? videoPage = null,
Object? channelPage = null,
Object? playlistPage = null,
Object? searchHistory = null,
Object? filters = null,
}) {
return _then(_$SearchStateImpl(
Expand Down Expand Up @@ -201,6 +210,10 @@ class __$$SearchStateImplCopyWithImpl<$Res>
? _value.playlistPage
: playlistPage // ignore: cast_nullable_to_non_nullable
as int,
searchHistory: null == searchHistory
? _value._searchHistory
: searchHistory // ignore: cast_nullable_to_non_nullable
as List<String>,
filters: null == filters
? _value.filters
: filters // ignore: cast_nullable_to_non_nullable
Expand All @@ -221,8 +234,10 @@ class _$SearchStateImpl implements _SearchState {
this.videoPage = 1,
this.channelPage = 1,
this.playlistPage = 1,
final List<String> searchHistory = const [],
this.filters = const SearchFiltersState()})
: _suggestions = suggestions;
: _suggestions = suggestions,
_searchHistory = searchHistory;

@override
final TextEditingController queryController;
Expand Down Expand Up @@ -253,13 +268,22 @@ class _$SearchStateImpl implements _SearchState {
@override
@JsonKey()
final int playlistPage;
final List<String> _searchHistory;
@override
@JsonKey()
List<String> get searchHistory {
if (_searchHistory is EqualUnmodifiableListView) return _searchHistory;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(_searchHistory);
}

@override
@JsonKey()
final SearchFiltersState filters;

@override
String toString() {
return 'SearchState(queryController: $queryController, searchNow: $searchNow, suggestions: $suggestions, sortBy: $sortBy, showResults: $showResults, videoPage: $videoPage, channelPage: $channelPage, playlistPage: $playlistPage, filters: $filters)';
return 'SearchState(queryController: $queryController, searchNow: $searchNow, suggestions: $suggestions, sortBy: $sortBy, showResults: $showResults, videoPage: $videoPage, channelPage: $channelPage, playlistPage: $playlistPage, searchHistory: $searchHistory, filters: $filters)';
}

@override
Expand All @@ -282,6 +306,8 @@ class _$SearchStateImpl implements _SearchState {
other.channelPage == channelPage) &&
(identical(other.playlistPage, playlistPage) ||
other.playlistPage == playlistPage) &&
const DeepCollectionEquality()
.equals(other._searchHistory, _searchHistory) &&
(identical(other.filters, filters) || other.filters == filters));
}

Expand All @@ -296,6 +322,7 @@ class _$SearchStateImpl implements _SearchState {
videoPage,
channelPage,
playlistPage,
const DeepCollectionEquality().hash(_searchHistory),
filters);

@JsonKey(ignore: true)
Expand All @@ -315,6 +342,7 @@ abstract class _SearchState implements SearchState {
final int videoPage,
final int channelPage,
final int playlistPage,
final List<String> searchHistory,
final SearchFiltersState filters}) = _$SearchStateImpl;

@override
Expand All @@ -334,6 +362,8 @@ abstract class _SearchState implements SearchState {
@override
int get playlistPage;
@override
List<String> get searchHistory;
@override
SearchFiltersState get filters;
@override
@JsonKey(ignore: true)
Expand Down
49 changes: 31 additions & 18 deletions lib/search/views/screens/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SearchScreen extends StatelessWidget {
create: (context) => SearchCubit<SearchState>(
SearchState.init(query: query, searchNow: searchNow), settings),
child: BlocBuilder<SearchCubit, SearchState>(
builder: (context, _) {
builder: (context, state) {
var cubit = context.read<SearchCubit>();
return AutoTabsRouter.tabBar(
routes: const [
Expand All @@ -44,7 +44,7 @@ class SearchScreen extends StatelessWidget {
final tabsController = AutoTabsRouter.of(context);
return Scaffold(
bottomNavigationBar:
_.showResults && deviceType == DeviceType.phone
state.showResults && deviceType == DeviceType.phone
? NavigationBar(
selectedIndex: tabsController.activeIndex,
onDestinationSelected:
Expand All @@ -65,25 +65,25 @@ class SearchScreen extends StatelessWidget {
appBar: AppBar(
title: TextField(
autofocus: query == null,
controller: _.queryController,
controller: state.queryController,
textInputAction: TextInputAction.search,
onSubmitted: cubit.search,
),
actions: [
IconButton(
onPressed: () {
if (cubit.searchCleared()) {
AutoRouter.of(context).pop();
AutoRouter.of(context).maybePop();
}
},
icon: const Icon(Icons.clear)),
SearchFiltersButton(
initialFilters: _.filters,
initialFilters: state.filters,
onChanged: (newFilters) {
cubit.onFiltersChanged(newFilters);
if (_.showResults ||
_.queryController.text.isNotEmpty) {
cubit.search(_.queryController.text);
if (state.showResults ||
state.queryController.text.isNotEmpty) {
cubit.search(state.queryController.text);
}
},
),
Expand All @@ -93,11 +93,11 @@ class SearchScreen extends StatelessWidget {
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: innerHorizontalPadding),
child: !_.showResults
child: !state.showResults
? ListView(
children: _.queryController.value.text.isEmpty
? cubit
.getHistory()
children: state
.queryController.value.text.isEmpty
? state.searchHistory
.map((e) => InkWell(
onTap: () =>
cubit.setSearchQuery(e),
Expand All @@ -106,16 +106,29 @@ class SearchScreen extends StatelessWidget {
const EdgeInsets.all(8.0),
child: Row(children: [
const Icon(Icons.history),
Padding(
padding:
const EdgeInsets
.only(left: 8),
child: Text(e))
Expanded(
child: Padding(
padding:
const EdgeInsets
.only(
left: 8),
child: Text(e)),
),
IconButton(
visualDensity:
VisualDensity
.compact,
onPressed: () => cubit
.removeFromHistory(
e),
iconSize: 17,
icon: const Icon(
Icons.delete))
]),
),
))
.toList()
: _.suggestions
: state.suggestions
.map((e) => InkWell(
onTap: () =>
cubit.setSearchQuery(e),
Expand Down
7 changes: 0 additions & 7 deletions lib/service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:invidious/channels/models/channel_sort_by.dart';
import 'package:invidious/extensions.dart';
import 'package:invidious/globals.dart';
import 'package:invidious/playlists/models/playlist.dart';
import 'package:invidious/search/models/db/search_history_item.dart';
import 'package:invidious/search/models/search_date.dart';
import 'package:invidious/search/models/search_duration.dart';
import 'package:invidious/search/models/search_results.dart';
Expand Down Expand Up @@ -293,12 +292,6 @@ class Service {
}
log.info(results);

if (query.isNotEmpty &&
db.getSettings(useSearchHistorySettingName)?.value == 'true') {
await db.addToSearchHistory(SearchHistoryItem(
query, (DateTime.now().millisecondsSinceEpoch / 1000).round()));
}

results.videos = (await postProcessVideos(results.videos)).cast();
return results;
}
Expand Down
6 changes: 6 additions & 0 deletions lib/utils/file_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,12 @@ class FileDB extends IDbClient {
upsertServer(Server server) async {
await super.upsertServer(server);
}

@override
Future<void> deleteFromSearchHistory(String search) {
// TODO: implement deleteFromSearchHistory
throw UnimplementedError();
}
}

@JsonSerializable()
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/interfaces/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,6 @@ abstract class IDbClient {
DeArrowCache? getDeArrowCache(String videoId);

Future<void> upsertDeArrowCache(DeArrowCache cache);

Future<void> deleteFromSearchHistory(String search);
}
11 changes: 9 additions & 2 deletions lib/utils/sembast_sqflite_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class SembastSqfDb extends IDbClient {
await clearExcessSearchHistory();
}

@override
Future<void> deleteFromSearchHistory(String item) async {
await searchHistoryStore.record(item).delete(db);
}

@override
Future<void> cleanOldLogs() async {
// TODO: implement cleanOldLogs
Expand Down Expand Up @@ -209,10 +214,12 @@ class SembastSqfDb extends IDbClient {

@override
List<String> getSearchHistory() {
return searchHistoryStore
var list = searchHistoryStore
.findSync(db)
.map((e) => SearchHistoryItem.fromJson(e.value).search)
.map((e) => SearchHistoryItem.fromJson(e.value))
.toList();
list.sort((a, b) => b.time.compareTo(a.time));
return list.map((e) => e.search).toList();
}

@override
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: invidious
version: 1.19.4+4049
version: 1.19.5+4050
publish_to: none
description: A new Flutter project.
environment:
Expand Down

0 comments on commit 72f7b43

Please sign in to comment.