From 644a2c1e3ad5c3a68e5efd612e730d55f1c58a8c Mon Sep 17 00:00:00 2001 From: otsmr Date: Mon, 20 Jul 2026 13:24:46 +0200 Subject: [PATCH] fix performance issue --- .../services/memories/memories.service.dart | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/src/services/memories/memories.service.dart b/lib/src/services/memories/memories.service.dart index 868ebcde..035d49e8 100644 --- a/lib/src/services/memories/memories.service.dart +++ b/lib/src/services/memories/memories.service.dart @@ -4,6 +4,7 @@ import 'dart:collection'; import 'package:blurhash_dart/blurhash_dart.dart' as bh; import 'package:clock/clock.dart'; import 'package:drift/drift.dart' show Value; +import 'package:flutter/foundation.dart'; import 'package:image/image.dart' as img; import 'package:intl/intl.dart'; import 'package:twonly/locator.dart'; @@ -259,7 +260,12 @@ class MemoriesService { // Phase 2: Background — hash, crop analysis, size calculation. // Each DB write here fires the stream subscription above, keeping // the gallery state fresh without a separate notification step. - await _backgroundProcessPendingFiles(pendingFiles); + _dbSubscription?.pause(); + try { + await _backgroundProcessPendingFiles(pendingFiles); + } finally { + _dbSubscription?.resume(); + } } catch (e) { Log.error('Error in background migration queue: $e'); } @@ -291,9 +297,8 @@ class MemoriesService { : mediaService.originalPath; if (imageFile.existsSync()) { final bytes = await imageFile.readAsBytes(); - final image = img.decodeImage(bytes); - if (image != null) { - final blurhash = bh.BlurHash.encode(image).hash; + final blurhash = await compute(_calculateBlurhash, bytes); + if (blurhash != null) { await twonlyDB.mediaFilesDao.updateMedia( mediaFile.mediaId, MediaFilesCompanion(blurhash: Value(blurhash)), @@ -384,3 +389,13 @@ class MemoriesService { _stateController.close(); } } + +String? _calculateBlurhash(Uint8List bytes) { + try { + final image = img.decodeImage(bytes); + if (image != null) { + return bh.BlurHash.encode(image).hash; + } + } catch (_) {} + return null; +}