2022-02-27 20:45:12 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-03-16 17:19:31 +02:00
|
|
|
import 'package:immich_mobile/modules/search/models/search_page_state.model.dart';
|
2022-02-27 20:45:12 +02:00
|
|
|
|
2022-02-27 20:43:29 +02:00
|
|
|
import 'package:immich_mobile/modules/search/services/search.service.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:openapi/api.dart';
|
2022-02-27 20:43:29 +02:00
|
|
|
|
2022-02-27 20:45:12 +02:00
|
|
|
class SearchPageStateNotifier extends StateNotifier<SearchPageState> {
|
2022-06-25 20:46:51 +02:00
|
|
|
SearchPageStateNotifier(this._searchService)
|
2022-02-27 20:45:12 +02:00
|
|
|
: super(
|
|
|
|
SearchPageState(
|
|
|
|
searchTerm: "",
|
|
|
|
isSearchEnabled: false,
|
|
|
|
searchSuggestion: [],
|
2022-02-27 20:43:29 +02:00
|
|
|
userSuggestedSearchTerms: [],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2022-06-25 20:46:51 +02:00
|
|
|
final SearchService _searchService;
|
2022-02-27 20:43:29 +02:00
|
|
|
|
2022-02-27 20:45:12 +02:00
|
|
|
void enableSearch() {
|
|
|
|
state = state.copyWith(isSearchEnabled: true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void disableSearch() {
|
|
|
|
state = state.copyWith(isSearchEnabled: false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setSearchTerm(String value) {
|
|
|
|
state = state.copyWith(searchTerm: value);
|
|
|
|
|
|
|
|
_getSearchSuggestion(state.searchTerm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _getSearchSuggestion(String searchTerm) {
|
2022-02-27 20:43:29 +02:00
|
|
|
var searchList = state.userSuggestedSearchTerms;
|
2022-02-27 20:45:12 +02:00
|
|
|
|
|
|
|
var newList = searchList.where((e) => e.toLowerCase().contains(searchTerm));
|
|
|
|
|
|
|
|
state = state.copyWith(searchSuggestion: [...newList]);
|
|
|
|
|
|
|
|
if (searchTerm.isEmpty) {
|
|
|
|
state = state.copyWith(searchSuggestion: []);
|
|
|
|
}
|
|
|
|
}
|
2022-02-27 20:43:29 +02:00
|
|
|
|
|
|
|
void getSuggestedSearchTerms() async {
|
2022-06-22 07:23:35 +02:00
|
|
|
var userSuggestedSearchTerms =
|
|
|
|
await _searchService.getUserSuggestedSearchTerms();
|
2022-02-27 20:43:29 +02:00
|
|
|
|
|
|
|
state = state.copyWith(userSuggestedSearchTerms: userSuggestedSearchTerms);
|
|
|
|
}
|
2022-02-27 20:45:12 +02:00
|
|
|
}
|
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
final searchPageStateProvider =
|
|
|
|
StateNotifierProvider<SearchPageStateNotifier, SearchPageState>((ref) {
|
2022-06-25 20:46:51 +02:00
|
|
|
return SearchPageStateNotifier(ref.watch(searchServiceProvider));
|
2022-02-27 20:45:12 +02:00
|
|
|
});
|
2022-03-16 17:19:31 +02:00
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
final getCuratedLocationProvider =
|
2022-07-13 14:23:48 +02:00
|
|
|
FutureProvider.autoDispose<List<CuratedLocationsResponseDto>>((ref) async {
|
2022-06-25 20:46:51 +02:00
|
|
|
final SearchService searchService = ref.watch(searchServiceProvider);
|
2022-03-16 17:19:31 +02:00
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
var curatedLocation = await searchService.getCuratedLocation();
|
2022-07-01 03:08:49 +02:00
|
|
|
return curatedLocation ?? [];
|
2022-03-16 17:19:31 +02:00
|
|
|
});
|
2022-03-27 21:58:54 +02:00
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
final getCuratedObjectProvider =
|
2022-07-13 14:23:48 +02:00
|
|
|
FutureProvider.autoDispose<List<CuratedObjectsResponseDto>>((ref) async {
|
2022-06-25 20:46:51 +02:00
|
|
|
final SearchService searchService = ref.watch(searchServiceProvider);
|
2022-03-27 21:58:54 +02:00
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
var curatedObject = await searchService.getCuratedObjects();
|
2022-07-01 03:08:49 +02:00
|
|
|
|
|
|
|
return curatedObject ?? [];
|
2022-03-27 21:58:54 +02:00
|
|
|
});
|