2022-03-16 17:19:31 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2023-03-25 05:44:53 +02:00
|
|
|
import 'package:immich_mobile/modules/asset_viewer/providers/render_list.provider.dart';
|
2024-01-05 23:23:58 +02:00
|
|
|
import 'package:immich_mobile/modules/home/ui/asset_grid/asset_grid_data_structure.dart';
|
2022-03-16 17:19:31 +02:00
|
|
|
import 'package:immich_mobile/modules/search/models/search_result_page_state.model.dart';
|
|
|
|
|
|
|
|
import 'package:immich_mobile/modules/search/services/search.service.dart';
|
2022-11-08 19:00:24 +02:00
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2022-03-16 17:19:31 +02:00
|
|
|
|
|
|
|
class SearchResultPageNotifier extends StateNotifier<SearchResultPageState> {
|
2022-06-25 20:46:51 +02:00
|
|
|
SearchResultPageNotifier(this._searchService)
|
|
|
|
: super(
|
|
|
|
SearchResultPageState(
|
|
|
|
searchResult: [],
|
|
|
|
isError: false,
|
|
|
|
isLoading: true,
|
|
|
|
isSuccess: false,
|
2024-01-05 23:23:58 +02:00
|
|
|
isClip: false,
|
2022-06-25 20:46:51 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
final SearchService _searchService;
|
2022-03-16 17:19:31 +02:00
|
|
|
|
2024-01-05 23:23:58 +02:00
|
|
|
Future<void> search(String searchTerm, {bool clipEnable = true}) async {
|
2022-06-25 20:46:51 +02:00
|
|
|
state = state.copyWith(
|
2022-07-13 14:23:48 +02:00
|
|
|
searchResult: [],
|
|
|
|
isError: false,
|
|
|
|
isLoading: true,
|
|
|
|
isSuccess: false,
|
|
|
|
);
|
2022-03-16 17:19:31 +02:00
|
|
|
|
2023-03-28 17:34:06 +02:00
|
|
|
List<Asset>? assets = await _searchService.searchAsset(
|
|
|
|
searchTerm,
|
|
|
|
clipEnable: clipEnable,
|
|
|
|
);
|
2022-03-16 17:19:31 +02:00
|
|
|
|
|
|
|
if (assets != null) {
|
2022-06-25 20:46:51 +02:00
|
|
|
state = state.copyWith(
|
2022-07-13 14:23:48 +02:00
|
|
|
searchResult: assets,
|
|
|
|
isError: false,
|
|
|
|
isLoading: false,
|
|
|
|
isSuccess: true,
|
2024-01-05 23:23:58 +02:00
|
|
|
isClip: clipEnable,
|
2022-07-13 14:23:48 +02:00
|
|
|
);
|
2022-03-16 17:19:31 +02:00
|
|
|
} else {
|
2022-06-25 20:46:51 +02:00
|
|
|
state = state.copyWith(
|
2022-07-13 14:23:48 +02:00
|
|
|
searchResult: [],
|
|
|
|
isError: true,
|
|
|
|
isLoading: false,
|
|
|
|
isSuccess: false,
|
2024-01-05 23:23:58 +02:00
|
|
|
isClip: clipEnable,
|
2022-07-13 14:23:48 +02:00
|
|
|
);
|
2022-03-16 17:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-25 20:46:51 +02:00
|
|
|
final searchResultPageProvider =
|
|
|
|
StateNotifierProvider<SearchResultPageNotifier, SearchResultPageState>(
|
|
|
|
(ref) {
|
|
|
|
return SearchResultPageNotifier(ref.watch(searchServiceProvider));
|
2022-03-16 17:19:31 +02:00
|
|
|
});
|
|
|
|
|
2024-01-05 23:23:58 +02:00
|
|
|
final searchRenderListProvider = Provider((ref) {
|
|
|
|
final result = ref.watch(searchResultPageProvider);
|
|
|
|
return ref.watch(
|
|
|
|
renderListProviderWithGrouping(
|
|
|
|
(result.searchResult, result.isClip ? GroupAssetsBy.none : null),
|
|
|
|
),
|
|
|
|
);
|
2022-09-30 11:05:54 +02:00
|
|
|
});
|