Skip to content

Commit

Permalink
feat: Remember toggle states (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenjacobs authored Jan 7, 2024
1 parent 6dd60e2 commit 2aa9a63
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<Boolean> =
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<Boolean> =
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")
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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() {

Expand All @@ -58,14 +59,12 @@ class MainScreenViewModel(
}

private val text = MutableStateFlow<String?>(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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2aa9a63

Please sign in to comment.