From d38a09c3f0c74ad65b9de06f51b82e36ae8f6580 Mon Sep 17 00:00:00 2001 From: Prateek Sunal Date: Mon, 1 Sep 2025 18:50:00 +0000 Subject: [PATCH] perf: optimize video stream processing state management MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Move isCurrentlyProcessing to widget state for better performance - Only call setState when processing state actually changes - Add comprehensive processing status handling (retry, compressing, uploading) - Remove redundant service calls from build method - Clean up unnecessary early returns and duplicate logic 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../lib/services/video_preview_service.dart | 2 - .../ui/viewer/file/video_stream_change.dart | 40 ++++++++++--------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/mobile/apps/photos/lib/services/video_preview_service.dart b/mobile/apps/photos/lib/services/video_preview_service.dart index 29d263cea5..5b90ce9483 100644 --- a/mobile/apps/photos/lib/services/video_preview_service.dart +++ b/mobile/apps/photos/lib/services/video_preview_service.dart @@ -157,8 +157,6 @@ class VideoPreviewService { bool isCurrentlyProcessing(int? uploadedFileID) { if (uploadedFileID == null) return false; - // Check if file is actively being processed - if (uploadingFileId == uploadedFileID) return true; // Also check if file is in queue or other processing states final item = _items[uploadedFileID]; diff --git a/mobile/apps/photos/lib/ui/viewer/file/video_stream_change.dart b/mobile/apps/photos/lib/ui/viewer/file/video_stream_change.dart index ecca015a16..a99aaeede9 100644 --- a/mobile/apps/photos/lib/ui/viewer/file/video_stream_change.dart +++ b/mobile/apps/photos/lib/ui/viewer/file/video_stream_change.dart @@ -32,27 +32,34 @@ class VideoStreamChangeWidget extends StatefulWidget { class _VideoStreamChangeWidgetState extends State { StreamSubscription? _subscription; + bool isCurrentlyProcessing = false; + @override void initState() { super.initState(); + // Initialize processing state safely in initState + isCurrentlyProcessing = VideoPreviewService.instance + .isCurrentlyProcessing(widget.file.uploadedFileID); _subscription = Bus.instance.on().listen((event) { final fileId = event.fileId; - if (widget.file.uploadedFileID != fileId) { - return; // Not for this file - } - final status = event.status; - // Handle different states - switch (status) { - case PreviewItemStatus.inQueue: - case PreviewItemStatus.uploaded: - case PreviewItemStatus.failed: - setState(() {}); - break; - default: + // Handle different states - will be false for different files or non-processing states + final newProcessingState = widget.file.uploadedFileID == fileId && switch (status) { + PreviewItemStatus.inQueue || + PreviewItemStatus.retry || + PreviewItemStatus.compressing || + PreviewItemStatus.uploading => + true, + _ => false, + }; + + // Only update state if value changed + if (isCurrentlyProcessing != newProcessingState) { + isCurrentlyProcessing = newProcessingState; + setState(() {}); } }); } @@ -80,13 +87,10 @@ class _VideoStreamChangeWidgetState extends State { final bool isPreviewAvailable = widget.file.uploadedFileID != null && (fileDataService.previewIds.containsKey(widget.file.uploadedFileID)); - // Check if this file is currently being processed for streaming - final bool isCurrentlyProcessing = VideoPreviewService.instance - .isCurrentlyProcessing(widget.file.uploadedFileID); - // Get the current processing status for more specific messaging - final processingStatus = widget.file.uploadedFileID != null - ? VideoPreviewService.instance.getProcessingStatus(widget.file.uploadedFileID!) + final processingStatus = widget.file.uploadedFileID != null + ? VideoPreviewService.instance + .getProcessingStatus(widget.file.uploadedFileID!) : null; final colorScheme = getEnteColorScheme(context);