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';
|
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';
|
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
|
|
|
|
2023-03-04 00:38:30 +02:00
|
|
|
SearchService(this._apiService, this._db);
|
2022-02-27 20:43:29 +02:00
|
|
|
|
|
|
|
Future<List<String>?> getUserSuggestedSearchTerms() async {
|
|
|
|
try {
|
2022-07-13 14:23:48 +02:00
|
|
|
return await _apiService.assetApi.getAssetSearchTerms();
|
2022-02-27 20:43:29 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("[ERROR] [getUserSuggestedSearchTerms] ${e.toString()}");
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2022-03-03 00:44:24 +02:00
|
|
|
|
2023-03-28 17:34:06 +02:00
|
|
|
Future<List<Asset>?> searchAsset(
|
|
|
|
String searchTerm, {
|
|
|
|
bool clipEnable = true,
|
|
|
|
}) async {
|
2023-03-04 00:38:30 +02:00
|
|
|
// TODO search in local DB: 1. when offline, 2. to find local assets
|
2022-03-03 00:44:24 +02:00
|
|
|
try {
|
2023-03-28 17:34:06 +02:00
|
|
|
final SearchResponseDto? results = await _apiService.searchApi.search(
|
|
|
|
query: searchTerm,
|
|
|
|
clip: clipEnable,
|
|
|
|
);
|
2023-02-04 22:42:42 +02:00
|
|
|
if (results == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-03-04 00:38:30 +02:00
|
|
|
// TODO local DB might be out of date; add assets not yet in DB?
|
2023-03-23 17:08:14 +02:00
|
|
|
return _db.assets.getAllByRemoteId(results.assets.items.map((e) => e.id));
|
2022-03-03 00:44:24 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("[ERROR] [searchAsset] ${e.toString()}");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2022-03-16 17:19:31 +02:00
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
Future<List<CuratedLocationsResponseDto>?> getCuratedLocation() async {
|
2022-03-16 17:19:31 +02:00
|
|
|
try {
|
2022-07-13 14:23:48 +02:00
|
|
|
var locations = await _apiService.assetApi.getCuratedLocations();
|
2022-03-16 17:19:31 +02:00
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
return locations;
|
2022-03-16 17:19:31 +02:00
|
|
|
} catch (e) {
|
2022-07-13 14:23:48 +02:00
|
|
|
debugPrint("Error [getCuratedLocation] ${e.toString()}");
|
|
|
|
return [];
|
2022-03-16 17:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-27 21:58:54 +02:00
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
Future<List<CuratedObjectsResponseDto>?> getCuratedObjects() async {
|
2022-03-27 21:58:54 +02:00
|
|
|
try {
|
2022-07-13 14:23:48 +02:00
|
|
|
return await _apiService.assetApi.getCuratedObjects();
|
2022-03-27 21:58:54 +02:00
|
|
|
} catch (e) {
|
2022-07-13 14:23:48 +02:00
|
|
|
debugPrint("Error [getCuratedObjects] ${e.toString()}");
|
2023-02-04 22:42:42 +02:00
|
|
|
return [];
|
2022-03-27 21:58:54 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-27 20:43:29 +02:00
|
|
|
}
|