Fix wallpaper scheduling and effects handling bugs

Multiple fixes for scheduling logic and UI behavior:

**Scheduling Fixes:**
- Add required albums validation before scheduling WorkManager jobs
- Only schedule when all required albums are selected based on enabled screens
- Prevent scheduling with incomplete album selection (both screens enabled requires both albums)
- Add hasRequiredAlbums check in HomeViewModel, WallpaperScreen, and BootReceiver

**Race Condition Fixes:**
- Fix toggleWallpaperChanger race condition with atomic updateEnableChanger()
- Prevent phantom album selections by using atomic DataStore updates
- Fix startup race condition - only cleanup stale album IDs after albums load

**Display Settings:**
- Separate scheduling changes from display changes (ScheduleSettings.kt)
- Add hasSchedulingChanges() - intervals, screen enable/disable, separate schedules
- Add hasDisplayChanges() - scaling, effects, adaptive brightness
- Reapply wallpaper immediately when display settings change (no reschedule needed)
- Prevent unnecessary WorkManager rescheduling for display-only changes

**Effect Sliders:**
- Fix effect sliders to respect separateSchedules setting
- Show synchronized slider when individual scheduling OFF
- Show separate sliders when individual scheduling ON
- Applies to brightness/darken, blur, and vignette sliders

**AlbumRefreshWorker:**
- Fix duplicate detection to check both direct and folder wallpapers
- Fix failure counting (increment failedCount on addWallpaper errors)
- Prevent false error logs and database constraint violations

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Anthonyy232andClaude authored and Anthony La committed 2025-12-21 18:30:23 -08:00
1 parent 9079306f8f
commit 9820581723
8 files changed
+266 -82

No files matched your search

@@ -307,6 +307,17 @@ class PreferencesManager @Inject constructor(
}
}
// ============ Atomic ScheduleSettings Operations ============
/**
* Atomically update enableChanger setting without race conditions
*/
suspend fun updateEnableChanger(enabled: Boolean) {
dataStore.edit { prefs ->
prefs[booleanPreferencesKey(PreferenceKeys.ENABLE_CHANGER)] = enabled
}
}
// ============ Individual Preference Operations ============
suspend fun <T> setValue(key: String, value: T) {
@@ -62,4 +62,9 @@ class SettingsRepositoryImpl @Inject constructor(
override suspend fun updateFirstLaunch(isFirstLaunch: Boolean) =
preferencesManager.updateFirstLaunch(isFirstLaunch)
// ============ Atomic ScheduleSettings Operations ============
override suspend fun updateEnableChanger(enabled: Boolean) =
preferencesManager.updateEnableChanger(enabled)
}
@@ -32,6 +32,39 @@ data class ScheduleSettings(
lockEffects = lockEffects.validate()
)
/**
* Check if scheduling-related settings changed (not just display settings)
*
* Returns true if any of these changed:
* - Screen enable/disable
* - Interval timing
* - Separate schedules toggle
* - Shuffle (handled separately with queue clearing)
*
* Display-only settings (scaling, effects, adaptive brightness) don't affect scheduling
*/
fun hasSchedulingChanges(other: ScheduleSettings): Boolean {
return homeEnabled != other.homeEnabled ||
lockEnabled != other.lockEnabled ||
homeIntervalMinutes != other.homeIntervalMinutes ||
lockIntervalMinutes != other.lockIntervalMinutes ||
separateSchedules != other.separateSchedules
}
/**
* Check if display settings changed (scaling, effects, adaptive brightness)
*
* When these change, we should reapply the current wallpaper immediately
* to show the user the effect, but we don't need to reschedule WorkManager
*/
fun hasDisplayChanges(other: ScheduleSettings): Boolean {
return homeScalingType != other.homeScalingType ||
lockScalingType != other.lockScalingType ||
homeEffects != other.homeEffects ||
lockEffects != other.lockEffects ||
adaptiveBrightness != other.adaptiveBrightness
}
companion object {
fun default() = ScheduleSettings()
}
@@ -69,4 +69,12 @@ interface SettingsRepository {
suspend fun updateDynamicTheming(enabled: Boolean)
suspend fun updateAnimate(enabled: Boolean)
suspend fun updateFirstLaunch(isFirstLaunch: Boolean)
// ============ Atomic ScheduleSettings Operations ============
/**
* Atomically update enableChanger to prevent race conditions
* Use this instead of updateScheduleSettings when only toggling changer
*/
suspend fun updateEnableChanger(enabled: Boolean)
}
@@ -102,26 +102,52 @@ class HomeViewModel @Inject constructor(
if (album == null && updated.lockAlbumId == null) {
toggleWallpaperChanger(false)
} else if (album != null && updated.enableChanger) {
// Immediately change wallpaper when selecting album
val screenType = if (
updated.homeAlbumId == updated.lockAlbumId &&
updated.homeAlbumId != null &&
!updated.separateSchedules &&
updated.homeEnabled &&
updated.lockEnabled
) {
// Same album for both screens and not separately scheduled - use BOTH
ScreenType.BOTH
} else {
ScreenType.HOME
// Check if we have all required albums before triggering wallpaper change
val homeActive = updated.homeEnabled && updated.homeAlbumId != null
val lockActive = updated.lockEnabled && updated.lockAlbumId != null
val hasRequiredAlbums = when {
updated.homeEnabled && updated.lockEnabled -> homeActive && lockActive
updated.homeEnabled -> homeActive
updated.lockEnabled -> lockActive
else -> false
}
changeWallpaperNow(screenType)
// Then schedule future alarms
scheduleAlarms(updated)
// Only change wallpaper and schedule if all required albums are selected
if (hasRequiredAlbums) {
val screenType = if (
updated.homeAlbumId == updated.lockAlbumId &&
updated.homeAlbumId != null &&
!updated.separateSchedules &&
updated.homeEnabled &&
updated.lockEnabled
) {
// Same album for both screens and not separately scheduled - use BOTH
ScreenType.BOTH
} else {
ScreenType.HOME
}
changeWallpaperNow(screenType)
scheduleAlarms(updated)
} else {
// Not all required albums selected - cancel any existing schedules
wallpaperScheduler.cancelAllWallpaperChanges()
}
} else if (updated.enableChanger) {
// Just reschedule if changer is enabled but album was unselected
scheduleAlarms(updated)
// Album was unselected - check if we still have all required albums
val homeActive = updated.homeEnabled && updated.homeAlbumId != null
val lockActive = updated.lockEnabled && updated.lockAlbumId != null
val hasRequiredAlbums = when {
updated.homeEnabled && updated.lockEnabled -> homeActive && lockActive
updated.homeEnabled -> homeActive
updated.lockEnabled -> lockActive
else -> false
}
if (hasRequiredAlbums) {
scheduleAlarms(updated)
} else {
wallpaperScheduler.cancelAllWallpaperChanges()
}
}
}
}
@@ -138,42 +164,77 @@ class HomeViewModel @Inject constructor(
if (album == null && updated.homeAlbumId == null) {
toggleWallpaperChanger(false)
} else if (album != null && updated.enableChanger) {
// Immediately change wallpaper when selecting album
val screenType = if (
updated.homeAlbumId == updated.lockAlbumId &&
updated.lockAlbumId != null &&
!updated.separateSchedules &&
updated.homeEnabled &&
updated.lockEnabled
) {
// Same album for both screens and not separately scheduled - use BOTH
ScreenType.BOTH
} else {
ScreenType.LOCK
// Check if we have all required albums before triggering wallpaper change
val homeActive = updated.homeEnabled && updated.homeAlbumId != null
val lockActive = updated.lockEnabled && updated.lockAlbumId != null
val hasRequiredAlbums = when {
updated.homeEnabled && updated.lockEnabled -> homeActive && lockActive
updated.homeEnabled -> homeActive
updated.lockEnabled -> lockActive
else -> false
}
changeWallpaperNow(screenType)
// Then schedule future alarms
scheduleAlarms(updated)
// Only change wallpaper and schedule if all required albums are selected
if (hasRequiredAlbums) {
val screenType = if (
updated.homeAlbumId == updated.lockAlbumId &&
updated.lockAlbumId != null &&
!updated.separateSchedules &&
updated.homeEnabled &&
updated.lockEnabled
) {
// Same album for both screens and not separately scheduled - use BOTH
ScreenType.BOTH
} else {
ScreenType.LOCK
}
changeWallpaperNow(screenType)
scheduleAlarms(updated)
} else {
// Not all required albums selected - cancel any existing schedules
wallpaperScheduler.cancelAllWallpaperChanges()
}
} else if (updated.enableChanger) {
// Just reschedule if changer is enabled but album was unselected
scheduleAlarms(updated)
// Album was unselected - check if we still have all required albums
val homeActive = updated.homeEnabled && updated.homeAlbumId != null
val lockActive = updated.lockEnabled && updated.lockAlbumId != null
val hasRequiredAlbums = when {
updated.homeEnabled && updated.lockEnabled -> homeActive && lockActive
updated.homeEnabled -> homeActive
updated.lockEnabled -> lockActive
else -> false
}
if (hasRequiredAlbums) {
scheduleAlarms(updated)
} else {
wallpaperScheduler.cancelAllWallpaperChanges()
}
}
}
}
fun toggleWallpaperChanger(enabled: Boolean) {
viewModelScope.launch {
val current = scheduleSettings.value
val updated = current.copy(enableChanger = enabled)
settingsRepository.updateScheduleSettings(updated)
// Use atomic update to prevent race conditions with album selection updates
settingsRepository.updateEnableChanger(enabled)
// Read updated settings after atomic write
val updated = settingsRepository.getScheduleSettings()
if (enabled) {
// Immediately change wallpaper when enabling changer
// Check if we have all required albums before changing wallpaper
val homeActive = updated.homeEnabled && updated.homeAlbumId != null
val lockActive = updated.lockEnabled && updated.lockAlbumId != null
val hasRequiredAlbums = when {
updated.homeEnabled && updated.lockEnabled -> homeActive && lockActive
updated.homeEnabled -> homeActive
updated.lockEnabled -> lockActive
else -> false
}
if (homeActive || lockActive) {
// Only change wallpaper and schedule if all required albums are selected
if (hasRequiredAlbums) {
val screenType = when {
homeActive && lockActive && updated.homeAlbumId == updated.lockAlbumId && !updated.separateSchedules ->
ScreenType.BOTH
@@ -185,10 +246,11 @@ class HomeViewModel @Inject constructor(
}
screenType?.let { changeWallpaperNow(it) }
scheduleAlarms(updated)
} else {
// Not all required albums selected - cancel any existing schedules
wallpaperScheduler.cancelAllWallpaperChanges()
}
// Then schedule future alarms
scheduleAlarms(updated)
} else {
wallpaperScheduler.cancelAllWallpaperChanges()
}
@@ -202,6 +264,11 @@ class HomeViewModel @Inject constructor(
val shuffleChanged = currentSettings.shuffleEnabled != settings.shuffleEnabled
val validated = settings.validate()
// Check what changed
val schedulingChanged = validated.hasSchedulingChanges(currentSettings)
val displayChanged = validated.hasDisplayChanges(currentSettings)
settingsRepository.updateScheduleSettings(validated)
// If shuffle setting changed, clear all queues to force rebuild with new mode
@@ -209,16 +276,39 @@ class HomeViewModel @Inject constructor(
wallpaperRepository.clearAllQueues()
}
// Only reschedule if changer is enabled AND at least one album is selected
val homeActive = validated.homeEnabled && validated.homeAlbumId != null
val lockActive = validated.lockEnabled && validated.lockAlbumId != null
if (validated.enableChanger && (homeActive || lockActive)) {
// Determine if we have the required albums selected
val hasRequiredAlbums = when {
validated.homeEnabled && validated.lockEnabled -> homeActive && lockActive
validated.homeEnabled -> homeActive
validated.lockEnabled -> lockActive
else -> false
}
// Handle scheduling changes (interval, screen enable/disable, etc.)
if (validated.enableChanger && hasRequiredAlbums && schedulingChanged) {
scheduleAlarms(validated)
} else if (validated.enableChanger) {
// Changer enabled but no albums selected - cancel existing schedules
} else if (validated.enableChanger && !hasRequiredAlbums) {
// Changer enabled but required albums not selected - cancel existing schedules
wallpaperScheduler.cancelAllWallpaperChanges()
}
// Handle display changes (scaling, effects, adaptive brightness)
// Reapply current wallpaper immediately to show the effect
if (validated.enableChanger && hasRequiredAlbums && displayChanged) {
val screenType = when {
homeActive && lockActive && validated.homeAlbumId == validated.lockAlbumId && !validated.separateSchedules ->
ScreenType.BOTH
homeActive && lockActive ->
ScreenType.BOTH // Change both even if different albums
homeActive -> ScreenType.HOME
lockActive -> ScreenType.LOCK
else -> null
}
screenType?.let { changeWallpaperNow(it) }
}
}
}
@@ -113,19 +113,35 @@ fun WallpaperScreen(
}
// Auto-toggle wallpaper changer based on album selection state
LaunchedEffect(scheduleSettings.homeAlbumId, scheduleSettings.lockAlbumId, homeEnabled, lockEnabled) {
// IMPORTANT: Check if actual album OBJECTS exist, not just IDs
// IDs might be stale (album deleted) which would cause invalid state
LaunchedEffect(homeAlbum, lockAlbum, homeEnabled, lockEnabled, scheduleSettings.homeAlbumId, scheduleSettings.lockAlbumId, albums) {
// Clean up stale album IDs (ID exists but album doesn't)
// IMPORTANT: Only cleanup if albums list has loaded (not empty)
// Otherwise we might clear valid IDs during initial load race condition
if (albums.isNotEmpty()) {
if (homeEnabled && scheduleSettings.homeAlbumId != null && homeAlbum == null) {
// Home album ID is set but album doesn't exist in loaded list - clear it
onSelectHomeAlbum(null)
}
if (lockEnabled && scheduleSettings.lockAlbumId != null && lockAlbum == null) {
// Lock album ID is set but album doesn't exist in loaded list - clear it
onSelectLockAlbum(null)
}
}
val allRequiredAlbumsSet = when {
homeEnabled && lockEnabled -> {
// Both are enabled, require both albums to be set
scheduleSettings.homeAlbumId != null && scheduleSettings.lockAlbumId != null
// Both are enabled, require both album OBJECTS to exist
homeAlbum != null && lockAlbum != null
}
homeEnabled -> {
// Only home is enabled, require only home album
scheduleSettings.homeAlbumId != null
// Only home is enabled, require home album OBJECT to exist
homeAlbum != null
}
lockEnabled -> {
// Only lock is enabled, require only lock album
scheduleSettings.lockAlbumId != null
// Only lock is enabled, require lock album OBJECT to exist
lockAlbum != null
}
else -> false // Neither enabled, should be disabled
}
@@ -550,7 +566,8 @@ fun WallpaperScreen(
)
)
},
bothEnabled = homeEnabled && lockEnabled,
// Show separate sliders only when both enabled AND separate schedules is on
bothEnabled = homeEnabled && lockEnabled && scheduleSettings.separateSchedules,
homePercentage = scheduleSettings.homeEffects.darkenPercentage,
lockPercentage = scheduleSettings.lockEffects.darkenPercentage,
onPercentageChange = { homePercent, lockPercent ->
@@ -580,7 +597,8 @@ fun WallpaperScreen(
)
)
},
bothEnabled = homeEnabled && lockEnabled,
// Show separate sliders only when both enabled AND separate schedules is on
bothEnabled = homeEnabled && lockEnabled && scheduleSettings.separateSchedules,
homePercentage = scheduleSettings.homeEffects.blurPercentage,
lockPercentage = scheduleSettings.lockEffects.blurPercentage,
onPercentageChange = { homePercent, lockPercent ->
@@ -610,7 +628,8 @@ fun WallpaperScreen(
)
)
},
bothEnabled = homeEnabled && lockEnabled,
// Show separate sliders only when both enabled AND separate schedules is on
bothEnabled = homeEnabled && lockEnabled && scheduleSettings.separateSchedules,
homePercentage = scheduleSettings.homeEffects.vignettePercentage,
lockPercentage = scheduleSettings.lockEffects.vignettePercentage,
onPercentageChange = { homePercent, lockPercent ->
@@ -48,34 +48,48 @@ class BootReceiver : BroadcastReceiver() {
val settings = settingsRepository.getScheduleSettings()
if (settings.enableChanger) {
// Reschedule wallpaper changes with WorkManager
val homeInterval: Int
val lockInterval: Int
if (settings.separateSchedules) {
// Separate schedules for home and lock
homeInterval = if (settings.homeEnabled) settings.homeIntervalMinutes else 0
lockInterval = if (settings.lockEnabled) settings.lockIntervalMinutes else 0
} else {
// Same interval for both (enabled screens)
val interval = settings.homeIntervalMinutes
homeInterval = if (settings.homeEnabled) interval else 0
lockInterval = if (settings.lockEnabled) interval else 0
// Check if we have all required albums before scheduling
val homeActive = settings.homeEnabled && settings.homeAlbumId != null
val lockActive = settings.lockEnabled && settings.lockAlbumId != null
val hasRequiredAlbums = when {
settings.homeEnabled && settings.lockEnabled -> homeActive && lockActive
settings.homeEnabled -> homeActive
settings.lockEnabled -> lockActive
else -> false
}
// Determine if screens should be synchronized
val shouldSync = settings.homeEnabled && settings.lockEnabled &&
settings.homeAlbumId != null &&
settings.homeAlbumId == settings.lockAlbumId &&
!settings.separateSchedules
if (hasRequiredAlbums) {
// Reschedule wallpaper changes with WorkManager
val homeInterval: Int
val lockInterval: Int
wallpaperScheduler.scheduleWallpaperChanges(
homeIntervalMinutes = homeInterval,
lockIntervalMinutes = lockInterval,
synchronized = shouldSync
)
if (settings.separateSchedules) {
// Separate schedules for home and lock
homeInterval = if (settings.homeEnabled) settings.homeIntervalMinutes else 0
lockInterval = if (settings.lockEnabled) settings.lockIntervalMinutes else 0
} else {
// Same interval for both (enabled screens)
val interval = settings.homeIntervalMinutes
homeInterval = if (settings.homeEnabled) interval else 0
lockInterval = if (settings.lockEnabled) interval else 0
}
Log.d(TAG, "Wallpaper changes rescheduled successfully")
// Determine if screens should be synchronized
val shouldSync = settings.homeEnabled && settings.lockEnabled &&
settings.homeAlbumId != null &&
settings.homeAlbumId == settings.lockAlbumId &&
!settings.separateSchedules
wallpaperScheduler.scheduleWallpaperChanges(
homeIntervalMinutes = homeInterval,
lockIntervalMinutes = lockInterval,
synchronized = shouldSync
)
Log.d(TAG, "Wallpaper changes rescheduled successfully")
} else {
Log.d(TAG, "Wallpaper changer enabled but required albums not selected, not scheduling")
}
} else {
Log.d(TAG, "Wallpaper changer disabled, not scheduling")
}
@@ -73,9 +73,12 @@ class AlbumRefreshWorker @AssistedInject constructor(
when (val scanResult = wallpaperRepository.scanFolderForWallpapers(android.net.Uri.parse(folder.uri))) {
is CoreResult.Success -> {
val scannedWallpapers = scanResult.data
val existingUris = album.wallpapers.map { it.uri }.toSet()
// Find new wallpapers not already in album
// Get ALL existing URIs (direct wallpapers + folder wallpapers)
val existingUris = (album.wallpapers.map { it.uri } +
album.folders.flatMap { it.wallpapers.map { w -> w.uri } }).toSet()
// Find new wallpapers not already in album (anywhere)
val newWallpapers = scannedWallpapers.filter { it.uri !in existingUris }
// Add new wallpapers to album
@@ -87,6 +90,7 @@ class AlbumRefreshWorker @AssistedInject constructor(
}
is CoreResult.Error -> {
Log.e(TAG, "Error adding wallpaper to album '${album.name}'")
failedCount++
}
is CoreResult.Loading -> {
/* Loading state not used */