From 49fecf56affc91cdd692ffb9c2b85f4c624ab146 Mon Sep 17 00:00:00 2001 From: Anthonyy232 Date: Wed, 19 Nov 2025 19:41:59 -0800 Subject: [PATCH] Fix queue handling when running out of wallpapers during validation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Improve robustness of wallpaper queue management: **Problem:** When the queue runs out DURING the validation loop (all remaining wallpapers are invalid), the code returned an error without trying to rebuild the queue. This could happen when: - Album has both valid and invalid wallpapers - Queue contains only invalid wallpapers after valid ones are used - Validation loop dequeues all invalid wallpapers - Queue becomes empty but is never rebuilt **Solution:** When getAndDequeueWallpaper returns null during validation loop: 1. Try rebuilding the queue (up to 2 attempts) 2. Continue validation loop to get wallpaper from rebuilt queue 3. Only return "no wallpapers" error after 2 rebuild attempts fail **Edge Cases Handled:** - Queue runs out due to invalid wallpapers being removed: rebuild and retry - Album truly has no valid wallpapers: error after 2 rebuild attempts - Shuffle mode: queue rebuilt with correct shuffle order - Sequential mode: queue rebuilt with correct display order This ensures the wallpaper changer continues working even when encountering invalid wallpapers, and properly cycles through the album whether shuffle is enabled or not. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../domain/usecase/ChangeWallpaperUseCase.kt | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/anthonyla/paperize/domain/usecase/ChangeWallpaperUseCase.kt b/app/src/main/java/com/anthonyla/paperize/domain/usecase/ChangeWallpaperUseCase.kt index 1c63a9b..a0f6649 100644 --- a/app/src/main/java/com/anthonyla/paperize/domain/usecase/ChangeWallpaperUseCase.kt +++ b/app/src/main/java/com/anthonyla/paperize/domain/usecase/ChangeWallpaperUseCase.kt @@ -56,14 +56,31 @@ class ChangeWallpaperUseCase @Inject constructor( // This prevents race conditions when multiple wallpaper changes happen simultaneously var wallpaper: Wallpaper? = null var maxRetries = MAX_VALIDATION_RETRIES // Prevent infinite loop + var queueRebuildAttempts = 0 while (wallpaper == null && maxRetries > 0) { // Atomically get and remove from queue in a single transaction val candidate = wallpaperRepository.getAndDequeueWallpaper(albumId, screenType) - // If still null after building queue, no wallpapers exist in album + // If queue is empty (candidate is null), try rebuilding queue if (candidate == null) { - return Result.Error(Exception(context.getString(R.string.no_wallpapers_in_album))) + queueRebuildAttempts++ + + // Allow up to 2 rebuild attempts + // First attempt: queue ran out due to invalid wallpapers being removed + // Second attempt: confirms album truly has no valid wallpapers + if (queueRebuildAttempts > 2) { + return Result.Error(Exception(context.getString(R.string.no_wallpapers_in_album))) + } + + // Try rebuilding queue + val rebuildResult = wallpaperRepository.buildWallpaperQueue(albumId, screenType, settings.shuffleEnabled) + if (rebuildResult is Result.Error) { + return Result.Error(Exception(context.getString(R.string.error_failed_to_build_queue))) + } + + // Continue loop to try getting wallpaper from rebuilt queue + continue } // Validate URI