fix: move settings debounce from composable-local state to HomeViewModel
pendingSettings was stored in WallpaperScreen's remember{} scope — navigating
away (e.g. to AlbumView) cancelled the LaunchedEffect and silently discarded
in-flight slider/interval changes. Moved the debounce Job to viewModelScope so
pending updates survive composition changes.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
8f357f8767
commit
456fea3c17
3 files changed
+16
-24
No files matched your search
@@ -121,7 +121,8 @@ fun HomeScreen(
|
||||
onSelectHomeAlbum = { album -> viewModel.selectHomeAlbum(album) },
|
||||
onSelectLockAlbum = { album -> viewModel.selectLockAlbum(album) },
|
||||
onSelectLiveAlbum = { album -> viewModel.selectLiveAlbum(album) },
|
||||
onUpdateScheduleSettings = { viewModel.updateScheduleSettings(it) }
|
||||
onUpdateScheduleSettings = { viewModel.updateScheduleSettings(it) },
|
||||
onUpdateScheduleSettingsDebounced = { viewModel.updateScheduleSettingsDebounced(it) }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ import com.anthonyla.paperize.service.wallpaper.WallpaperChangeService
|
||||
import com.anthonyla.paperize.service.worker.WallpaperScheduler
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
@@ -418,6 +420,16 @@ class HomeViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private var pendingSettingsJob: Job? = null
|
||||
|
||||
fun updateScheduleSettingsDebounced(settings: ScheduleSettings) {
|
||||
pendingSettingsJob?.cancel()
|
||||
pendingSettingsJob = viewModelScope.launch {
|
||||
delay(Constants.SETTINGS_DEBOUNCE_MS)
|
||||
updateScheduleSettings(settings)
|
||||
}
|
||||
}
|
||||
|
||||
fun changeWallpaperNow(screenType: ScreenType) {
|
||||
val intent = Intent(context, WallpaperChangeService::class.java).apply {
|
||||
action = WallpaperChangeService.ACTION_CHANGE_WALLPAPER
|
||||
|
||||
+2
-23
@@ -31,7 +31,6 @@ import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.derivedStateOf
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableIntStateOf
|
||||
import androidx.compose.runtime.mutableLongStateOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
@@ -54,7 +53,6 @@ import com.anthonyla.paperize.presentation.screens.wallpaper.components.SettingS
|
||||
import com.anthonyla.paperize.presentation.screens.wallpaper.components.SettingSwitchWithSlider
|
||||
import com.anthonyla.paperize.presentation.screens.wallpaper.components.TimeIntervalPicker
|
||||
import com.anthonyla.paperize.presentation.theme.AppSpacing
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
enum class AlbumSelectionContext {
|
||||
HOME, LOCK, BOTH, LIVE
|
||||
@@ -71,37 +69,18 @@ fun WallpaperScreen(
|
||||
onSelectLockAlbum: (AlbumSummary?) -> Unit,
|
||||
onSelectLiveAlbum: (AlbumSummary?) -> Unit,
|
||||
onUpdateScheduleSettings: (ScheduleSettings) -> Unit,
|
||||
onUpdateScheduleSettingsDebounced: (ScheduleSettings) -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
var showAlbumSelectionSheet by rememberSaveable { mutableStateOf(false) }
|
||||
var albumSelectionContext by rememberSaveable { mutableStateOf(AlbumSelectionContext.BOTH) }
|
||||
var showEmptyAlbumWarning by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
// Settings debouncing
|
||||
var pendingSettings by remember { mutableStateOf<ScheduleSettings?>(null) }
|
||||
var lastSettingsChangeTimestamp by remember { mutableLongStateOf(0L) }
|
||||
|
||||
// Debounce settings updates to avoid excessive database writes and rescheduling
|
||||
LaunchedEffect(lastSettingsChangeTimestamp) {
|
||||
if (lastSettingsChangeTimestamp > 0 && pendingSettings != null) {
|
||||
delay(Constants.SETTINGS_DEBOUNCE_MS)
|
||||
pendingSettings?.let { onUpdateScheduleSettings(it) }
|
||||
pendingSettings = null
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to update settings with debouncing
|
||||
fun updateSettingsDebounced(newSettings: ScheduleSettings) {
|
||||
pendingSettings = newSettings
|
||||
lastSettingsChangeTimestamp = System.currentTimeMillis()
|
||||
onUpdateScheduleSettingsDebounced(newSettings)
|
||||
}
|
||||
|
||||
// Wrapper for immediate updates that cancels pending debounced updates
|
||||
fun updateSettingsImmediate(newSettings: ScheduleSettings) {
|
||||
// Cancel any pending debounced updates
|
||||
pendingSettings = null
|
||||
lastSettingsChangeTimestamp = 0L
|
||||
// Apply the update immediately
|
||||
onUpdateScheduleSettings(newSettings)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user