diff --git a/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeScreen.kt b/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeScreen.kt index bbaf23a..8b6fc7f 100644 --- a/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeScreen.kt +++ b/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeScreen.kt @@ -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) } ) } } diff --git a/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeViewModel.kt b/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeViewModel.kt index b171a2b..11e52eb 100644 --- a/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeViewModel.kt +++ b/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeViewModel.kt @@ -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 diff --git a/app/src/main/java/com/anthonyla/paperize/presentation/screens/wallpaper/WallpaperScreen.kt b/app/src/main/java/com/anthonyla/paperize/presentation/screens/wallpaper/WallpaperScreen.kt index 4f69667..203c991 100644 --- a/app/src/main/java/com/anthonyla/paperize/presentation/screens/wallpaper/WallpaperScreen.kt +++ b/app/src/main/java/com/anthonyla/paperize/presentation/screens/wallpaper/WallpaperScreen.kt @@ -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(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) }