2024-10-30 19:27:13 +00:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:immich_mobile/models/search/search_result.model.dart';
|
2024-05-02 20:59:14 +00:00
|
|
|
import 'package:immich_mobile/providers/asset_viewer/render_list.provider.dart';
|
2024-05-07 04:04:21 +00:00
|
|
|
import 'package:immich_mobile/widgets/asset_grid/asset_grid_data_structure.dart';
|
2024-05-01 02:36:40 +00:00
|
|
|
import 'package:immich_mobile/models/search/search_filter.model.dart';
|
2024-05-02 20:59:14 +00:00
|
|
|
import 'package:immich_mobile/services/search.service.dart';
|
2024-04-01 14:45:11 +00:00
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
|
|
|
|
part 'paginated_search.provider.g.dart';
|
|
|
|
|
2024-10-30 19:27:13 +00:00
|
|
|
final paginatedSearchProvider =
|
|
|
|
StateNotifierProvider<PaginatedSearchNotifier, SearchResult>(
|
|
|
|
(ref) => PaginatedSearchNotifier(ref.watch(searchServiceProvider)),
|
|
|
|
);
|
2024-04-01 14:45:11 +00:00
|
|
|
|
2024-10-30 19:27:13 +00:00
|
|
|
class PaginatedSearchNotifier extends StateNotifier<SearchResult> {
|
|
|
|
final SearchService _searchService;
|
2024-04-01 14:45:11 +00:00
|
|
|
|
2024-10-30 19:27:13 +00:00
|
|
|
PaginatedSearchNotifier(this._searchService)
|
|
|
|
: super(SearchResult(assets: [], nextPage: 1));
|
2024-04-01 14:45:11 +00:00
|
|
|
|
2024-10-30 19:27:13 +00:00
|
|
|
search(SearchFilter filter) async {
|
|
|
|
if (state.nextPage == null) return;
|
2024-04-01 14:45:11 +00:00
|
|
|
|
2024-10-30 19:27:13 +00:00
|
|
|
final result = await _searchService.search(filter, state.nextPage!);
|
2024-04-01 14:45:11 +00:00
|
|
|
|
2024-10-30 19:27:13 +00:00
|
|
|
if (result == null) return;
|
2024-04-01 14:45:11 +00:00
|
|
|
|
2024-10-30 19:27:13 +00:00
|
|
|
state = SearchResult(
|
|
|
|
assets: [...state.assets, ...result.assets],
|
|
|
|
nextPage: result.nextPage,
|
|
|
|
);
|
2024-04-01 14:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
2024-10-30 19:27:13 +00:00
|
|
|
state = SearchResult(assets: [], nextPage: 1);
|
2024-04-01 14:45:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@riverpod
|
|
|
|
AsyncValue<RenderList> paginatedSearchRenderList(
|
|
|
|
PaginatedSearchRenderListRef ref,
|
|
|
|
) {
|
2024-10-30 19:27:13 +00:00
|
|
|
final result = ref.watch(paginatedSearchProvider);
|
2024-04-01 14:45:11 +00:00
|
|
|
|
2024-10-30 19:27:13 +00:00
|
|
|
return ref.watch(
|
|
|
|
renderListProviderWithGrouping(
|
|
|
|
(result.assets, GroupAssetsBy.none),
|
|
|
|
),
|
|
|
|
);
|
2024-04-01 14:45:11 +00:00
|
|
|
}
|