1
0
mirror of https://github.com/immich-app/immich.git synced 2024-12-25 10:43:13 +02:00

feat(mobile): load search result assets from local DB (#5971)

This commit is contained in:
Fynn Petersen-Frey 2023-12-26 22:41:51 +01:00 committed by GitHub
parent c0ebc943d2
commit 733fa28aa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 29 deletions

View File

@ -48,10 +48,8 @@ class MapSerivce {
if (assets.isNotEmpty) return assets[0]; if (assets.isNotEmpty) return assets[0];
final dto = await _apiService.assetApi.getAssetById(remoteId); final dto = await _apiService.assetApi.getAssetById(remoteId);
if (dto == null) { if (dto == null) return null;
return null; return _db.assets.getByRemoteId(dto.id);
}
return Asset.remote(dto);
} catch (error, stack) { } catch (error, stack) {
_log.severe( _log.severe(
"Cannot get asset for marker ${error.toString()}", "Cannot get asset for marker ${error.toString()}",

View File

@ -2,13 +2,16 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/shared/models/asset.dart'; import 'package:immich_mobile/shared/models/asset.dart';
import 'package:immich_mobile/modules/memories/models/memory.dart'; import 'package:immich_mobile/modules/memories/models/memory.dart';
import 'package:immich_mobile/shared/providers/api.provider.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:immich_mobile/shared/services/api.service.dart';
import 'package:isar/isar.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:openapi/api.dart'; import 'package:openapi/api.dart';
final memoryServiceProvider = StateProvider<MemoryService>((ref) { final memoryServiceProvider = StateProvider<MemoryService>((ref) {
return MemoryService( return MemoryService(
ref.watch(apiServiceProvider), ref.watch(apiServiceProvider),
ref.watch(dbProvider),
); );
}); });
@ -16,8 +19,9 @@ class MemoryService {
final log = Logger("MemoryService"); final log = Logger("MemoryService");
final ApiService _apiService; final ApiService _apiService;
final Isar _db;
MemoryService(this._apiService); MemoryService(this._apiService, this._db);
Future<List<Memory>?> getMemoryLane() async { Future<List<Memory>?> getMemoryLane() async {
try { try {
@ -36,7 +40,7 @@ class MemoryService {
memories.add( memories.add(
Memory( Memory(
title: title, title: title,
assets: assets.map((a) => Asset.remote(a)).toList(), assets: await _db.assets.getAllByRemoteId(assets.map((e) => e.id)),
), ),
); );
} }

View File

@ -1,28 +1,13 @@
import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/shared/models/asset.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/providers/db.provider.dart';
import 'package:isar/isar.dart';
final allVideoAssetsProvider = FutureProvider<List<Asset>>( (ref) async { final allVideoAssetsProvider = FutureProvider<List<Asset>>((ref) async {
final search = await ref.watch(apiServiceProvider).searchApi.search( return ref
type: 'VIDEO', .watch(dbProvider)
);
if (search == null) {
return [];
}
return ref.watch(dbProvider)
.assets .assets
.getAllByRemoteId(
search.assets.items.map((e) => e.id),
);
/// This works offline, but we use the above
/*
return ref.watch(dbProvider).assets
.filter() .filter()
.durationInSecondsGreaterThan(0) .typeEqualTo(AssetType.video)
.findAll(); .findAll();
*/
}); });

View File

@ -1,6 +1,8 @@
import 'package:immich_mobile/shared/models/asset.dart'; import 'package:immich_mobile/shared/models/asset.dart';
import 'package:immich_mobile/shared/providers/api.provider.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:immich_mobile/shared/services/api.service.dart';
import 'package:isar/isar.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:openapi/api.dart'; import 'package:openapi/api.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart';
@ -9,13 +11,14 @@ part 'person.service.g.dart';
@riverpod @riverpod
PersonService personService(PersonServiceRef ref) => PersonService personService(PersonServiceRef ref) =>
PersonService(ref.read(apiServiceProvider)); PersonService(ref.read(apiServiceProvider), ref.read(dbProvider));
class PersonService { class PersonService {
final Logger _log = Logger("PersonService"); final Logger _log = Logger("PersonService");
final ApiService _apiService; final ApiService _apiService;
final Isar _db;
PersonService(this._apiService); PersonService(this._apiService, this._db);
Future<List<PersonResponseDto>> getCuratedPeople() async { Future<List<PersonResponseDto>> getCuratedPeople() async {
try { try {
@ -30,7 +33,8 @@ class PersonService {
Future<List<Asset>?> getPersonAssets(String id) async { Future<List<Asset>?> getPersonAssets(String id) async {
try { try {
final assets = await _apiService.personApi.getPersonAssets(id); final assets = await _apiService.personApi.getPersonAssets(id);
return assets?.map((e) => Asset.remote(e)).toList(); if (assets == null) return null;
return await _db.assets.getAllByRemoteId(assets.map((e) => e.id));
} catch (error, stack) { } catch (error, stack) {
_log.severe("Error while fetching person assets", error, stack); _log.severe("Error while fetching person assets", error, stack);
} }

View File

@ -472,6 +472,8 @@ extension AssetsHelper on IsarCollection<Asset> {
ids.isEmpty ? Future.value([]) : remote(ids).findAll(); ids.isEmpty ? Future.value([]) : remote(ids).findAll();
Future<List<Asset>> getAllByLocalId(Iterable<String> ids) => Future<List<Asset>> getAllByLocalId(Iterable<String> ids) =>
ids.isEmpty ? Future.value([]) : local(ids).findAll(); ids.isEmpty ? Future.value([]) : local(ids).findAll();
Future<Asset?> getByRemoteId(String id) =>
where().remoteIdEqualTo(id).findFirst();
QueryBuilder<Asset, Asset, QAfterWhereClause> remote(Iterable<String> ids) => QueryBuilder<Asset, Asset, QAfterWhereClause> remote(Iterable<String> ids) =>
where().anyOf(ids, (q, String e) => q.remoteIdEqualTo(e)); where().anyOf(ids, (q, String e) => q.remoteIdEqualTo(e));