From 942b27241ac409ef827e1ef6391e4bea73edf664 Mon Sep 17 00:00:00 2001 From: mertalev <101130780+mertalev@users.noreply.github.com> Date: Sun, 31 Aug 2025 20:14:14 -0400 Subject: [PATCH] fix hot reload --- .../repositories/network.repository.dart | 18 ++++++++++++++++-- mobile/lib/main.dart | 8 ++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/mobile/lib/infrastructure/repositories/network.repository.dart b/mobile/lib/infrastructure/repositories/network.repository.dart index 35981f7d1e..1639f1515d 100644 --- a/mobile/lib/infrastructure/repositories/network.repository.dart +++ b/mobile/lib/infrastructure/repositories/network.repository.dart @@ -7,11 +7,20 @@ import 'package:path_provider/path_provider.dart'; class NetworkRepository { static late Directory _cachePath; + static final _clients = {}; static Future init() async { _cachePath = await getTemporaryDirectory(); } + static void reset() { + Future.microtask(init); + for (final client in _clients.values) { + client.close(); + } + _clients.clear(); + } + const NetworkRepository(); http.Client getHttpClient({ @@ -21,11 +30,16 @@ class NetworkRepository { required int maxConnections, required CacheMode cacheMode, }) { + final cachedClient = _clients[directoryName]; + if (cachedClient != null) { + return cachedClient; + } + final directory = Directory('${_cachePath.path}/$directoryName'); directory.createSync(recursive: true); if (Platform.isAndroid) { final engine = CronetEngine.build(cacheMode: cacheMode, cacheMaxSize: diskCapacity, storagePath: directory.path); - return CronetClient.fromCronetEngine(engine, closeEngine: true); + return _clients[directoryName] = CronetClient.fromCronetEngine(engine, closeEngine: true); } final config = URLSessionConfiguration.defaultSessionConfiguration() @@ -35,6 +49,6 @@ class NetworkRepository { memoryCapacity: memoryCapacity, directory: directory.uri, ); - return CupertinoClient.fromSessionConfiguration(config); + return _clients[directoryName] = CupertinoClient.fromSessionConfiguration(config); } } diff --git a/mobile/lib/main.dart b/mobile/lib/main.dart index 889a9ca359..74f8af1edc 100644 --- a/mobile/lib/main.dart +++ b/mobile/lib/main.dart @@ -228,6 +228,14 @@ class ImmichAppState extends ConsumerState with WidgetsBindingObserve super.dispose(); } + @override + void reassemble() { + if (kDebugMode) { + NetworkRepository.reset(); + } + super.reassemble(); + } + @override Widget build(BuildContext context) { final router = ref.watch(appRouterProvider);