preliminary auto-login
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
import 'dart:async';
|
||||
import 'dart:isolate';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:immich_mobile/domain/repositories/database.repository.dart';
|
||||
import 'package:immich_mobile/service_locator.dart';
|
||||
import 'package:immich_mobile/utils/immich_api_client.dart';
|
||||
@@ -12,10 +16,13 @@ class _ImApiClientData {
|
||||
const _ImApiClientData({required this.endpoint, required this.headersMap});
|
||||
}
|
||||
|
||||
// !! Should be used only from the root isolate
|
||||
class IsolateHelper {
|
||||
// Cache the ApiClient to reconstruct it later after inside the isolate
|
||||
late final _ImApiClientData? _clientData;
|
||||
|
||||
static RootIsolateToken get _rootToken => RootIsolateToken.instance!;
|
||||
|
||||
IsolateHelper();
|
||||
|
||||
void preIsolateHandling() {
|
||||
@@ -26,7 +33,7 @@ class IsolateHelper {
|
||||
);
|
||||
}
|
||||
|
||||
void postIsolateHandling({required DriftDatabaseRepository database}) {
|
||||
void postIsolateHandling() {
|
||||
assert(_clientData != null);
|
||||
// Reconstruct client from cached data
|
||||
final client = ImmichApiClient(endpoint: _clientData!.endpoint);
|
||||
@@ -36,11 +43,21 @@ class IsolateHelper {
|
||||
|
||||
// Register all services in the isolates memory
|
||||
ServiceLocator.configureServicesForIsolate(
|
||||
database: database,
|
||||
database: DriftDatabaseRepository(),
|
||||
apiClient: client,
|
||||
);
|
||||
|
||||
// Init log manager to continue listening to log events
|
||||
LogManager.I.init();
|
||||
}
|
||||
|
||||
static Future<T> run<T>(FutureOr<T> Function() computation) async {
|
||||
final helper = IsolateHelper()..preIsolateHandling();
|
||||
final token = _rootToken;
|
||||
return await Isolate.run(() async {
|
||||
BackgroundIsolateBinaryMessenger.ensureInitialized(token);
|
||||
helper.postIsolateHandling();
|
||||
return await computation();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user