2022-03-16 08:19:31 -07:00
|
|
|
import 'package:collection/collection.dart';
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-10-01 01:33:06 -07:00
|
|
|
import 'package:immich_mobile/modules/home/ui/asset_grid/asset_grid_data_structure.dart';
|
2022-03-16 08:19:31 -07: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-09-30 02:05:54 -07:00
|
|
|
import 'package:immich_mobile/modules/settings/providers/app_settings.provider.dart';
|
|
|
|
import 'package:immich_mobile/modules/settings/services/app_settings.service.dart';
|
2022-03-16 08:19:31 -07:00
|
|
|
import 'package:intl/intl.dart';
|
2022-07-13 05:23:48 -07:00
|
|
|
import 'package:openapi/api.dart';
|
2022-03-16 08:19:31 -07:00
|
|
|
|
|
|
|
class SearchResultPageNotifier extends StateNotifier<SearchResultPageState> {
|
2022-06-25 11:46:51 -07:00
|
|
|
SearchResultPageNotifier(this._searchService)
|
|
|
|
: super(
|
|
|
|
SearchResultPageState(
|
|
|
|
searchResult: [],
|
|
|
|
isError: false,
|
|
|
|
isLoading: true,
|
|
|
|
isSuccess: false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
final SearchService _searchService;
|
2022-03-16 08:19:31 -07:00
|
|
|
|
|
|
|
void search(String searchTerm) async {
|
2022-06-25 11:46:51 -07:00
|
|
|
state = state.copyWith(
|
2022-07-13 05:23:48 -07:00
|
|
|
searchResult: [],
|
|
|
|
isError: false,
|
|
|
|
isLoading: true,
|
|
|
|
isSuccess: false,
|
|
|
|
);
|
2022-03-16 08:19:31 -07:00
|
|
|
|
2022-07-13 05:23:48 -07:00
|
|
|
List<AssetResponseDto>? assets =
|
|
|
|
await _searchService.searchAsset(searchTerm);
|
2022-03-16 08:19:31 -07:00
|
|
|
|
|
|
|
if (assets != null) {
|
2022-06-25 11:46:51 -07:00
|
|
|
state = state.copyWith(
|
2022-07-13 05:23:48 -07:00
|
|
|
searchResult: assets,
|
|
|
|
isError: false,
|
|
|
|
isLoading: false,
|
|
|
|
isSuccess: true,
|
|
|
|
);
|
2022-03-16 08:19:31 -07:00
|
|
|
} else {
|
2022-06-25 11:46:51 -07:00
|
|
|
state = state.copyWith(
|
2022-07-13 05:23:48 -07:00
|
|
|
searchResult: [],
|
|
|
|
isError: true,
|
|
|
|
isLoading: false,
|
|
|
|
isSuccess: false,
|
|
|
|
);
|
2022-03-16 08:19:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-25 11:46:51 -07:00
|
|
|
final searchResultPageProvider =
|
|
|
|
StateNotifierProvider<SearchResultPageNotifier, SearchResultPageState>(
|
|
|
|
(ref) {
|
|
|
|
return SearchResultPageNotifier(ref.watch(searchServiceProvider));
|
2022-03-16 08:19:31 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
final searchResultGroupByDateTimeProvider = StateProvider((ref) {
|
|
|
|
var assets = ref.watch(searchResultPageProvider).searchResult;
|
|
|
|
|
2022-06-25 11:46:51 -07:00
|
|
|
assets.sortByCompare<DateTime>(
|
2022-07-13 05:23:48 -07:00
|
|
|
(e) => DateTime.parse(e.createdAt),
|
|
|
|
(a, b) => b.compareTo(a),
|
|
|
|
);
|
|
|
|
return assets.groupListsBy(
|
2022-09-22 13:58:17 -07:00
|
|
|
(element) => DateFormat('y-MM-dd')
|
|
|
|
.format(DateTime.parse(element.createdAt).toLocal()),
|
2022-07-13 05:23:48 -07:00
|
|
|
);
|
2022-03-16 08:19:31 -07:00
|
|
|
});
|
2022-09-30 02:05:54 -07:00
|
|
|
|
|
|
|
final searchRenderListProvider = StateProvider((ref) {
|
|
|
|
var assetGroups = ref.watch(searchResultGroupByDateTimeProvider);
|
|
|
|
|
|
|
|
var settings = ref.watch(appSettingsServiceProvider);
|
|
|
|
final assetsPerRow = settings.getSetting(AppSettingsEnum.tilesPerRow);
|
|
|
|
|
|
|
|
return assetGroupsToRenderList(assetGroups, assetsPerRow);
|
|
|
|
});
|