1
0
mirror of https://github.com/immich-app/immich.git synced 2024-12-21 01:39:59 +02:00
immich/mobile/lib/modules/search/services/search.service.dart
Fynn Petersen-Frey 8708867c1c
feature(mobile): sync assets, albums & users to local database on device (#1759)
* feature(mobile): sync assets, albums & users to local database on device

* try to fix tests

* move DB sync operations to new SyncService

* clear db on user logout

* fix reason for endless loading timeline

* fix error when deleting album

* fix thumbnail of device albums

* add a few comments

* fix Hive box not open in album service when loading local assets

* adjust tests to int IDs

* fix bug: show all albums when Recent is selected

* update generated api

* reworked Recents album isAll handling

* guard against wrongly interleaved sync operations

* fix: timeline asset ordering (sort asset state by created at)

* fix: sort assets in albums by created at
2023-03-03 16:38:30 -06:00

68 lines
2.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/shared/models/asset.dart';
import 'package:immich_mobile/shared/providers/api.provider.dart';
import 'package:immich_mobile/shared/providers/db.provider.dart';
import 'package:immich_mobile/shared/services/api.service.dart';
import 'package:isar/isar.dart';
import 'package:openapi/api.dart';
final searchServiceProvider = Provider(
(ref) => SearchService(
ref.watch(apiServiceProvider),
ref.watch(dbProvider),
),
);
class SearchService {
final ApiService _apiService;
final Isar _db;
SearchService(this._apiService, this._db);
Future<List<String>?> getUserSuggestedSearchTerms() async {
try {
return await _apiService.assetApi.getAssetSearchTerms();
} catch (e) {
debugPrint("[ERROR] [getUserSuggestedSearchTerms] ${e.toString()}");
return [];
}
}
Future<List<Asset>?> searchAsset(String searchTerm) async {
// TODO search in local DB: 1. when offline, 2. to find local assets
try {
final List<AssetResponseDto>? results = await _apiService.assetApi
.searchAsset(SearchAssetDto(searchTerm: searchTerm));
if (results == null) {
return null;
}
// TODO local DB might be out of date; add assets not yet in DB?
return _db.assets.getAllByRemoteId(results.map((e) => e.id));
} catch (e) {
debugPrint("[ERROR] [searchAsset] ${e.toString()}");
return null;
}
}
Future<List<CuratedLocationsResponseDto>?> getCuratedLocation() async {
try {
var locations = await _apiService.assetApi.getCuratedLocations();
return locations;
} catch (e) {
debugPrint("Error [getCuratedLocation] ${e.toString()}");
return [];
}
}
Future<List<CuratedObjectsResponseDto>?> getCuratedObjects() async {
try {
return await _apiService.assetApi.getCuratedObjects();
} catch (e) {
debugPrint("Error [getCuratedObjects] ${e.toString()}");
return [];
}
}
}