From 2aa9a63f5a1c2bc30afe872250f82d9cf1f3f81c Mon Sep 17 00:00:00 2001 From: Sven Jacobs Date: Sun, 7 Jan 2024 13:16:57 +0100 Subject: [PATCH] feat: Remember toggle states (#351) --- .../app/leon/datastore/AppDataStoreManager.kt | 27 ++++++++++++++++++- .../screens/main/model/MainScreenViewModel.kt | 19 +++++++------ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/com/svenjacobs/app/leon/datastore/AppDataStoreManager.kt b/app/src/main/kotlin/com/svenjacobs/app/leon/datastore/AppDataStoreManager.kt index aec56fa0..75d5df44 100644 --- a/app/src/main/kotlin/com/svenjacobs/app/leon/datastore/AppDataStoreManager.kt +++ b/app/src/main/kotlin/com/svenjacobs/app/leon/datastore/AppDataStoreManager.kt @@ -1,6 +1,6 @@ /* * Léon - The URL Cleaner - * Copyright (C) 2023 Sven Jacobs + * Copyright (C) 2024 Sven Jacobs * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,6 +20,7 @@ package com.svenjacobs.app.leon.datastore import android.content.Context import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.booleanPreferencesKey import androidx.datastore.preferences.core.edit import androidx.datastore.preferences.core.intPreferencesKey import androidx.datastore.preferences.core.stringPreferencesKey @@ -54,8 +55,32 @@ class AppDataStoreManager(private val context: Context = AppContext) { }.getOrNull() } + suspend fun setUrlDecodeEnabled(enabled: Boolean) { + context.dataStore.edit { + it[KEY_URL_DECODE] = enabled + } + } + + val urlDecodeEnabled: Flow = + context.dataStore.data.map { preferences -> + preferences[KEY_URL_DECODE] ?: false + } + + suspend fun setExtractUrlEnabled(enabled: Boolean) { + context.dataStore.edit { + it[KEY_EXTRACT_URL] = enabled + } + } + + val extractUrlEnabled: Flow = + context.dataStore.data.map { preferences -> + preferences[KEY_EXTRACT_URL] ?: false + } + private companion object { private val KEY_VERSION_CODE = intPreferencesKey("version_code") private val KEY_ACTION_AFTER_CLEAN = stringPreferencesKey("action_after_clean") + private val KEY_URL_DECODE = booleanPreferencesKey("url_decode") + private val KEY_EXTRACT_URL = booleanPreferencesKey("extract_url") } } diff --git a/app/src/main/kotlin/com/svenjacobs/app/leon/ui/screens/main/model/MainScreenViewModel.kt b/app/src/main/kotlin/com/svenjacobs/app/leon/ui/screens/main/model/MainScreenViewModel.kt index 776899fa..92e3771b 100644 --- a/app/src/main/kotlin/com/svenjacobs/app/leon/ui/screens/main/model/MainScreenViewModel.kt +++ b/app/src/main/kotlin/com/svenjacobs/app/leon/ui/screens/main/model/MainScreenViewModel.kt @@ -1,6 +1,6 @@ /* * Léon - The URL Cleaner - * Copyright (C) 2023 Sven Jacobs + * Copyright (C) 2024 Sven Jacobs * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,9 +30,10 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.launch class MainScreenViewModel( - appDataStoreManager: AppDataStoreManager = AppDataStoreManager, + private val appDataStoreManager: AppDataStoreManager = AppDataStoreManager, private val cleanerService: CleanerService = CleanerService(), ) : ViewModel() { @@ -58,14 +59,12 @@ class MainScreenViewModel( } private val text = MutableStateFlow(null) - private val urlDecodeEnabled = MutableStateFlow(false) - private val extractUrlEnabled = MutableStateFlow(false) val uiState = combine( text, - urlDecodeEnabled, - extractUrlEnabled, + appDataStoreManager.urlDecodeEnabled, + appDataStoreManager.extractUrlEnabled, appDataStoreManager.actionAfterClean, ) { text, urlDecodeEnabled, extractUrlEnabled, actionAfterClean -> val result = text?.let { @@ -99,11 +98,15 @@ class MainScreenViewModel( } fun onUrlDecodeCheckedChange(enabled: Boolean) { - urlDecodeEnabled.value = enabled + viewModelScope.launch { + appDataStoreManager.setUrlDecodeEnabled(enabled) + } } fun onExtractUrlCheckedChange(enabled: Boolean) { - extractUrlEnabled.value = enabled + viewModelScope.launch { + appDataStoreManager.setExtractUrlEnabled(enabled) + } } private suspend fun clean(text: String, decodeUrl: Boolean, extractUrl: Boolean): Result = try {