From d7236e7bbf5d034769becb6fcbf316aaae6cf817 Mon Sep 17 00:00:00 2001 From: Damontecres Date: Sat, 24 Jan 2026 22:20:38 -0500 Subject: [PATCH] Ensure home settings are saved --- .../wholphin/ui/main/settings/HomeSettingsViewModel.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/github/damontecres/wholphin/ui/main/settings/HomeSettingsViewModel.kt b/app/src/main/java/com/github/damontecres/wholphin/ui/main/settings/HomeSettingsViewModel.kt index 9bb22f7e..61c91875 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/ui/main/settings/HomeSettingsViewModel.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/ui/main/settings/HomeSettingsViewModel.kt @@ -17,6 +17,7 @@ import com.github.damontecres.wholphin.data.model.HomeRowViewOptions import com.github.damontecres.wholphin.services.BackdropService import com.github.damontecres.wholphin.services.HomeSettingsService import com.github.damontecres.wholphin.services.UserPreferencesService +import com.github.damontecres.wholphin.services.hilt.IoCoroutineScope import com.github.damontecres.wholphin.ui.launchIO import com.github.damontecres.wholphin.ui.nav.ServerNavDrawerItem import com.github.damontecres.wholphin.ui.showToast @@ -24,6 +25,7 @@ import com.github.damontecres.wholphin.util.HomeRowLoadingState import com.github.damontecres.wholphin.util.LoadingState import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.qualifiers.ApplicationContext +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.first @@ -43,11 +45,13 @@ class HomeSettingsViewModel private val userPreferencesService: UserPreferencesService, private val navDrawerItemRepository: NavDrawerItemRepository, private val backdropService: BackdropService, + @param:IoCoroutineScope private val ioScope: CoroutineScope, ) : ViewModel() { private val _state = MutableStateFlow(HomePageSettingsState.EMPTY) val state: StateFlow = _state init { + addCloseable { saveToLocal() } viewModelScope.launchIO { val navDrawerItems = navDrawerItemRepository @@ -305,11 +309,13 @@ class HomeSettingsViewModel } fun saveToLocal() { - viewModelScope.launchIO { + // This uses injected ioScope so that it will still run when the page is closing + ioScope.launchIO { serverRepository.currentUser.value?.let { user -> val rows = state.value.rows.map { it.config } val settings = HomePageSettings(rows = rows) try { + Timber.d("saveToLocal") homeSettingsService.saveToLocal(user.id, settings) showToast(context, context.getString(R.string.save), Toast.LENGTH_SHORT) } catch (ex: Exception) {