Fix queue handling when running out of wallpapers during validation
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
ca59cd8155
commit
49fecf56af
1 file changed
+19
-2
@@ -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
|
||||
|
||||
Reference in new issue
Block a user