1
0
mirror of https://github.com/immich-app/immich.git synced 2024-12-23 02:06:15 +02:00
immich/mobile/lib/modules/search/providers/search_page_state.provider.dart

66 lines
1.9 KiB
Dart
Raw Normal View History

2022-02-27 20:45:12 +02:00
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/modules/search/models/search_page_state.model.dart';
2022-02-27 20:45:12 +02:00
import 'package:immich_mobile/modules/search/services/search.service.dart';
import 'package:openapi/api.dart';
2022-02-27 20:45:12 +02:00
class SearchPageStateNotifier extends StateNotifier<SearchPageState> {
SearchPageStateNotifier(this._searchService)
2022-02-27 20:45:12 +02:00
: super(
SearchPageState(
searchTerm: "",
isSearchEnabled: false,
searchSuggestion: [],
userSuggestedSearchTerms: [],
),
);
final SearchService _searchService;
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) {
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: []);
}
}
void getSuggestedSearchTerms() async {
var userSuggestedSearchTerms =
await _searchService.getUserSuggestedSearchTerms();
state = state.copyWith(userSuggestedSearchTerms: userSuggestedSearchTerms);
}
2022-02-27 20:45:12 +02:00
}
final searchPageStateProvider =
StateNotifierProvider<SearchPageStateNotifier, SearchPageState>((ref) {
return SearchPageStateNotifier(ref.watch(searchServiceProvider));
2022-02-27 20:45:12 +02:00
});
final getCuratedLocationProvider =
FutureProvider.autoDispose<List<CuratedLocationsResponseDto>>((ref) async {
final SearchService searchService = ref.watch(searchServiceProvider);
var curatedLocation = await searchService.getCuratedLocation();
return curatedLocation ?? [];
});