mirror of
https://github.com/immich-app/immich.git
synced 2025-07-09 03:04:16 -04:00
use asynccache
This commit is contained in:
parent
8450c8cc4f
commit
0e8b19e269
@ -11,13 +11,8 @@ class UserApiRepository with LogMixin implements IUserApiRepository {
|
||||
@override
|
||||
Future<model.User?> getMyUser() async {
|
||||
try {
|
||||
final [
|
||||
userDto as UserAdminResponseDto?,
|
||||
preferencesDto as UserPreferencesResponseDto?
|
||||
] = await Future.wait([
|
||||
_usersApi.getMyUser(),
|
||||
_usersApi.getMyPreferences(),
|
||||
]);
|
||||
final (userDto, preferencesDto) =
|
||||
await (_usersApi.getMyUser(), _usersApi.getMyPreferences()).wait;
|
||||
|
||||
if (userDto == null) {
|
||||
log.e("Cannot fetch my user.");
|
||||
|
@ -1,3 +1,4 @@
|
||||
import 'package:async/async.dart';
|
||||
import 'package:immich_mobile/domain/interfaces/album.interface.dart';
|
||||
import 'package:immich_mobile/domain/interfaces/album_asset.interface.dart';
|
||||
import 'package:immich_mobile/domain/interfaces/album_etag.interface.dart';
|
||||
@ -14,10 +15,13 @@ import 'package:immich_mobile/utils/isolate_helper.dart';
|
||||
import 'package:immich_mobile/utils/mixins/log.mixin.dart';
|
||||
|
||||
class AlbumSyncService with LogMixin {
|
||||
const AlbumSyncService();
|
||||
AlbumSyncService();
|
||||
|
||||
final _fullDeviceSyncCache = AsyncCache<bool>.ephemeral();
|
||||
|
||||
Future<bool> performFullDeviceSyncIsolate() async {
|
||||
return await IsolateHelper.run(performFullDeviceSync);
|
||||
return await _fullDeviceSyncCache
|
||||
.fetch(() async => await IsolateHelper.run(performFullDeviceSync));
|
||||
}
|
||||
|
||||
Future<bool> performFullDeviceSync() async {
|
||||
|
@ -1,5 +1,6 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:async/async.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:immich_mobile/domain/interfaces/api/sync_api.interface.dart';
|
||||
import 'package:immich_mobile/domain/interfaces/asset.interface.dart';
|
||||
@ -13,20 +14,24 @@ import 'package:immich_mobile/utils/isolate_helper.dart';
|
||||
import 'package:immich_mobile/utils/mixins/log.mixin.dart';
|
||||
|
||||
class AssetSyncService with LogMixin {
|
||||
const AssetSyncService();
|
||||
AssetSyncService();
|
||||
|
||||
final _fullRemoteSyncCache = AsyncCache<bool>.ephemeral();
|
||||
|
||||
Future<bool> performFullRemoteSyncIsolate(
|
||||
User user, {
|
||||
DateTime? updatedUtil,
|
||||
int? limit,
|
||||
}) async {
|
||||
return await IsolateHelper.run(() async {
|
||||
return await _fullRemoteSyncCache.fetch(
|
||||
() async => await IsolateHelper.run(() async {
|
||||
return await performFullRemoteSync(
|
||||
user,
|
||||
updatedUtil: updatedUtil,
|
||||
limit: limit,
|
||||
);
|
||||
});
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> performFullRemoteSync(
|
||||
|
@ -1,8 +1,6 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_web_auth_2/flutter_web_auth_2.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:immich_mobile/domain/interfaces/album.interface.dart';
|
||||
@ -22,6 +20,7 @@ import 'package:immich_mobile/presentation/states/gallery_permission.state.dart'
|
||||
import 'package:immich_mobile/presentation/states/server_info.state.dart';
|
||||
import 'package:immich_mobile/service_locator.dart';
|
||||
import 'package:immich_mobile/utils/immich_api_client.dart';
|
||||
import 'package:immich_mobile/utils/isolate_helper.dart';
|
||||
import 'package:immich_mobile/utils/mixins/log.mixin.dart';
|
||||
|
||||
// Cannot add dependency repos to constructor as this requires the newly registered API client from login
|
||||
@ -59,7 +58,7 @@ class LoginService with LogMixin {
|
||||
);
|
||||
|
||||
if (res.statusCode == HttpStatus.ok) {
|
||||
final data = await compute(jsonDecode, res.body);
|
||||
final data = await IsolateHelper.decodeJson(res.bodyBytes);
|
||||
final endpoint = data['api']['endpoint'].toString();
|
||||
|
||||
// Full URL is relative to base
|
||||
|
@ -12,7 +12,7 @@ class ServerInfoProvider extends ValueNotifier<ServerInfo> {
|
||||
super(const ServerInfo.initial());
|
||||
|
||||
Future<void> fetchFeatures() async =>
|
||||
await Future.wait([_getFeatures(), _getConfig(), _getVersion()]);
|
||||
await (_getFeatures(), _getConfig(), _getVersion()).wait;
|
||||
|
||||
Future<void> _getFeatures() async {
|
||||
final features = await _serverApiRepository.getServerFeatures();
|
||||
|
@ -138,8 +138,8 @@ abstract final class ServiceLocator {
|
||||
static void _registerServices() {
|
||||
/// Special services. So they are initiated as singletons
|
||||
_registerSingleton(ImHostService());
|
||||
_registerSingleton(const AlbumSyncService());
|
||||
_registerSingleton(const AssetSyncService());
|
||||
_registerSingleton(AlbumSyncService());
|
||||
_registerSingleton(AssetSyncService());
|
||||
|
||||
///
|
||||
_registerFactory<LoginService>(() => const LoginService());
|
||||
|
@ -1,4 +1,5 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:isolate';
|
||||
import 'dart:ui';
|
||||
|
||||
@ -82,4 +83,11 @@ class IsolateHelper {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static Future<Map<String, dynamic>> decodeJson(Uint8List json) async {
|
||||
return await Isolate.run(
|
||||
() => const Utf8Decoder().fuse(const JsonDecoder()).convert(json)
|
||||
as Map<String, dynamic>,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ dependencies:
|
||||
logging: ^1.3.0
|
||||
# Collection Utils
|
||||
collection: ^1.18.0
|
||||
async: ^2.12.0
|
||||
# service_locator
|
||||
get_it: ^8.0.0
|
||||
# Photo Manager
|
||||
|
Loading…
x
Reference in New Issue
Block a user