mirror of
https://github.com/gabehf/Fladder.git
synced 2026-03-14 09:46:01 -07:00
Init repo
This commit is contained in:
commit
764b6034e3
566 changed files with 212335 additions and 0 deletions
42
lib/providers/search_provider.dart
Normal file
42
lib/providers/search_provider.dart
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import 'package:chopper/chopper.dart';
|
||||
import 'package:fladder/models/search_model.dart';
|
||||
import 'package:fladder/providers/api_provider.dart';
|
||||
import 'package:fladder/providers/service_provider.dart';
|
||||
import 'package:fladder/util/item_base_model/item_base_model_extensions.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
final searchProvider = StateNotifierProvider<SearchNotifier, SearchModel>((ref) {
|
||||
return SearchNotifier(ref);
|
||||
});
|
||||
|
||||
class SearchNotifier extends StateNotifier<SearchModel> {
|
||||
SearchNotifier(this.ref) : super(SearchModel());
|
||||
|
||||
final Ref ref;
|
||||
|
||||
late final JellyService api = ref.read(jellyApiProvider);
|
||||
|
||||
Future<Response?> searchQuery() async {
|
||||
if (state.searchQuery.isEmpty) return null;
|
||||
state = state.copyWith(loading: true);
|
||||
final response = await api.itemsGet(
|
||||
recursive: true,
|
||||
searchTerm: state.searchQuery,
|
||||
);
|
||||
|
||||
state = state.copyWith(
|
||||
resultCount: response.body?.totalRecordCount ?? 0,
|
||||
results: (response.body?.items)?.groupedItems,
|
||||
);
|
||||
state = state.copyWith(loading: false);
|
||||
return response;
|
||||
}
|
||||
|
||||
void setQuery(String searchQuery) {
|
||||
state = state.copyWith(searchQuery: searchQuery);
|
||||
}
|
||||
|
||||
void clear() {
|
||||
state = SearchModel();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue