2022-02-27 20:43:29 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-06-25 20:46:51 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2024-04-01 16:45:11 +02:00
|
|
|
import 'package:immich_mobile/modules/search/models/search_filter.dart';
|
2023-02-04 22:42:42 +02:00
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2022-08-03 07:04:34 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/api.provider.dart';
|
2023-03-04 00:38:30 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/db.provider.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:immich_mobile/shared/services/api.service.dart';
|
2023-03-04 00:38:30 +02:00
|
|
|
import 'package:isar/isar.dart';
|
2024-04-26 07:49:31 +02:00
|
|
|
import 'package:logging/logging.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:openapi/api.dart';
|
2022-02-27 20:43:29 +02:00
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
final searchServiceProvider = Provider(
|
|
|
|
(ref) => SearchService(
|
|
|
|
ref.watch(apiServiceProvider),
|
2023-03-04 00:38:30 +02:00
|
|
|
ref.watch(dbProvider),
|
2022-07-13 14:23:48 +02:00
|
|
|
),
|
|
|
|
);
|
2022-06-25 20:46:51 +02:00
|
|
|
|
2022-02-27 20:43:29 +02:00
|
|
|
class SearchService {
|
2022-07-13 14:23:48 +02:00
|
|
|
final ApiService _apiService;
|
2023-03-04 00:38:30 +02:00
|
|
|
final Isar _db;
|
2022-08-03 07:04:34 +02:00
|
|
|
|
2024-04-26 07:49:31 +02:00
|
|
|
final _log = Logger("SearchService");
|
2023-03-04 00:38:30 +02:00
|
|
|
SearchService(this._apiService, this._db);
|
2022-02-27 20:43:29 +02:00
|
|
|
|
2024-04-01 16:45:11 +02:00
|
|
|
Future<List<String>?> getSearchSuggestions(
|
|
|
|
SearchSuggestionType type, {
|
|
|
|
String? country,
|
|
|
|
String? state,
|
|
|
|
String? make,
|
|
|
|
String? model,
|
2023-03-28 17:34:06 +02:00
|
|
|
}) async {
|
2022-03-03 00:44:24 +02:00
|
|
|
try {
|
2024-04-01 16:45:11 +02:00
|
|
|
return await _apiService.searchApi.getSearchSuggestions(
|
|
|
|
type,
|
|
|
|
country: country,
|
|
|
|
state: state,
|
|
|
|
make: make,
|
|
|
|
model: model,
|
2023-03-28 17:34:06 +02:00
|
|
|
);
|
2024-04-01 16:45:11 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("[ERROR] [getSearchSuggestions] ${e.toString()}");
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<Asset>?> search(SearchFilter filter, int page) async {
|
|
|
|
try {
|
|
|
|
SearchResponseDto? response;
|
|
|
|
AssetTypeEnum? type;
|
|
|
|
if (filter.mediaType == AssetType.image) {
|
|
|
|
type = AssetTypeEnum.IMAGE;
|
|
|
|
} else if (filter.mediaType == AssetType.video) {
|
|
|
|
type = AssetTypeEnum.VIDEO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filter.context != null && filter.context!.isNotEmpty) {
|
|
|
|
response = await _apiService.searchApi.searchSmart(
|
|
|
|
SmartSearchDto(
|
|
|
|
query: filter.context!,
|
|
|
|
country: filter.location.country,
|
|
|
|
state: filter.location.state,
|
|
|
|
city: filter.location.city,
|
|
|
|
make: filter.camera.make,
|
|
|
|
model: filter.camera.model,
|
|
|
|
takenAfter: filter.date.takenAfter,
|
|
|
|
takenBefore: filter.date.takenBefore,
|
|
|
|
isArchived: filter.display.isArchive,
|
|
|
|
isFavorite: filter.display.isFavorite,
|
|
|
|
isNotInAlbum: filter.display.isNotInAlbum,
|
|
|
|
personIds: filter.people.map((e) => e.id).toList(),
|
|
|
|
type: type,
|
|
|
|
page: page,
|
|
|
|
size: 1000,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
response = await _apiService.searchApi.searchMetadata(
|
|
|
|
MetadataSearchDto(
|
|
|
|
originalFileName:
|
|
|
|
filter.filename != null && filter.filename!.isNotEmpty
|
|
|
|
? filter.filename
|
|
|
|
: null,
|
|
|
|
country: filter.location.country,
|
|
|
|
state: filter.location.state,
|
|
|
|
city: filter.location.city,
|
|
|
|
make: filter.camera.make,
|
|
|
|
model: filter.camera.model,
|
|
|
|
takenAfter: filter.date.takenAfter,
|
|
|
|
takenBefore: filter.date.takenBefore,
|
|
|
|
isArchived: filter.display.isArchive,
|
|
|
|
isFavorite: filter.display.isFavorite,
|
|
|
|
isNotInAlbum: filter.display.isNotInAlbum,
|
|
|
|
personIds: filter.people.map((e) => e.id).toList(),
|
|
|
|
type: type,
|
|
|
|
page: page,
|
|
|
|
size: 1000,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response == null) {
|
2023-02-04 22:42:42 +02:00
|
|
|
return null;
|
|
|
|
}
|
2024-04-01 16:45:11 +02:00
|
|
|
|
|
|
|
return _db.assets
|
|
|
|
.getAllByRemoteId(response.assets.items.map((e) => e.id));
|
2024-04-26 07:49:31 +02:00
|
|
|
} catch (error, stackTrace) {
|
|
|
|
_log.severe("Failed to search for assets", error, stackTrace);
|
2022-03-03 00:44:24 +02:00
|
|
|
}
|
2024-04-01 16:45:11 +02:00
|
|
|
return null;
|
2022-03-03 00:44:24 +02:00
|
|
|
}
|
2022-03-16 17:19:31 +02:00
|
|
|
|
2024-04-26 07:49:31 +02:00
|
|
|
Future<List<SearchExploreResponseDto>?> getExploreData() async {
|
2022-03-27 21:58:54 +02:00
|
|
|
try {
|
2024-04-26 07:49:31 +02:00
|
|
|
return await _apiService.searchApi.getExploreData();
|
|
|
|
} catch (error, stackTrace) {
|
|
|
|
_log.severe("Failed to getExploreData", error, stackTrace);
|
2022-03-27 21:58:54 +02:00
|
|
|
}
|
2024-04-26 07:49:31 +02:00
|
|
|
return null;
|
2022-03-27 21:58:54 +02:00
|
|
|
}
|
2022-02-27 20:43:29 +02:00
|
|
|
}
|