2022-02-27 13:43:29 -05:00
|
|
|
import 'package:flutter/material.dart';
|
2022-06-25 13:46:51 -05:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2023-02-04 15:42:42 -05:00
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2022-08-03 00:04:34 -05:00
|
|
|
import 'package:immich_mobile/shared/providers/api.provider.dart';
|
2023-03-03 17:38:30 -05:00
|
|
|
import 'package:immich_mobile/shared/providers/db.provider.dart';
|
2022-07-13 07:23:48 -05:00
|
|
|
import 'package:immich_mobile/shared/services/api.service.dart';
|
2023-03-03 17:38:30 -05:00
|
|
|
import 'package:isar/isar.dart';
|
2022-07-13 07:23:48 -05:00
|
|
|
import 'package:openapi/api.dart';
|
2022-02-27 13:43:29 -05:00
|
|
|
|
2022-07-13 07:23:48 -05:00
|
|
|
final searchServiceProvider = Provider(
|
|
|
|
(ref) => SearchService(
|
|
|
|
ref.watch(apiServiceProvider),
|
2023-03-03 17:38:30 -05:00
|
|
|
ref.watch(dbProvider),
|
2022-07-13 07:23:48 -05:00
|
|
|
),
|
|
|
|
);
|
2022-06-25 13:46:51 -05:00
|
|
|
|
2022-02-27 13:43:29 -05:00
|
|
|
class SearchService {
|
2022-07-13 07:23:48 -05:00
|
|
|
final ApiService _apiService;
|
2023-03-03 17:38:30 -05:00
|
|
|
final Isar _db;
|
2022-08-03 00:04:34 -05:00
|
|
|
|
2023-03-03 17:38:30 -05:00
|
|
|
SearchService(this._apiService, this._db);
|
2022-02-27 13:43:29 -05:00
|
|
|
|
|
|
|
Future<List<String>?> getUserSuggestedSearchTerms() async {
|
|
|
|
try {
|
2022-07-13 07:23:48 -05:00
|
|
|
return await _apiService.assetApi.getAssetSearchTerms();
|
2022-02-27 13:43:29 -05:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("[ERROR] [getUserSuggestedSearchTerms] ${e.toString()}");
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2022-03-02 17:44:24 -05:00
|
|
|
|
2023-02-04 15:42:42 -05:00
|
|
|
Future<List<Asset>?> searchAsset(String searchTerm) async {
|
2023-03-03 17:38:30 -05:00
|
|
|
// TODO search in local DB: 1. when offline, 2. to find local assets
|
2022-03-02 17:44:24 -05:00
|
|
|
try {
|
2023-02-04 15:42:42 -05:00
|
|
|
final List<AssetResponseDto>? results = await _apiService.assetApi
|
2022-07-13 07:23:48 -05:00
|
|
|
.searchAsset(SearchAssetDto(searchTerm: searchTerm));
|
2023-02-04 15:42:42 -05:00
|
|
|
if (results == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-03-03 17:38:30 -05:00
|
|
|
// TODO local DB might be out of date; add assets not yet in DB?
|
|
|
|
return _db.assets.getAllByRemoteId(results.map((e) => e.id));
|
2022-03-02 17:44:24 -05:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("[ERROR] [searchAsset] ${e.toString()}");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2022-03-16 10:19:31 -05:00
|
|
|
|
2022-07-13 07:23:48 -05:00
|
|
|
Future<List<CuratedLocationsResponseDto>?> getCuratedLocation() async {
|
2022-03-16 10:19:31 -05:00
|
|
|
try {
|
2022-07-13 07:23:48 -05:00
|
|
|
var locations = await _apiService.assetApi.getCuratedLocations();
|
2022-03-16 10:19:31 -05:00
|
|
|
|
2022-07-13 07:23:48 -05:00
|
|
|
return locations;
|
2022-03-16 10:19:31 -05:00
|
|
|
} catch (e) {
|
2022-07-13 07:23:48 -05:00
|
|
|
debugPrint("Error [getCuratedLocation] ${e.toString()}");
|
|
|
|
return [];
|
2022-03-16 10:19:31 -05:00
|
|
|
}
|
|
|
|
}
|
2022-03-27 14:58:54 -05:00
|
|
|
|
2022-07-13 07:23:48 -05:00
|
|
|
Future<List<CuratedObjectsResponseDto>?> getCuratedObjects() async {
|
2022-03-27 14:58:54 -05:00
|
|
|
try {
|
2022-07-13 07:23:48 -05:00
|
|
|
return await _apiService.assetApi.getCuratedObjects();
|
2022-03-27 14:58:54 -05:00
|
|
|
} catch (e) {
|
2022-07-13 07:23:48 -05:00
|
|
|
debugPrint("Error [getCuratedObjects] ${e.toString()}");
|
2023-02-04 15:42:42 -05:00
|
|
|
return [];
|
2022-03-27 14:58:54 -05:00
|
|
|
}
|
|
|
|
}
|
2022-02-27 13:43:29 -05:00
|
|
|
}
|