refactor: 一些本地属性迁移

This commit is contained in:
HapeLee
2026-06-11 13:34:41 +08:00
parent 08f197598f
commit a94dc88a57
5 changed files with 57 additions and 24 deletions
@@ -7,8 +7,9 @@ import androidx.annotation.CallSuper
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import io.legado.app.R
import io.legado.app.data.local.preferences.LocalPreferencesKeys
import io.legado.app.data.local.preferences.LocalPreferencesRepository
import io.legado.app.help.LifecycleHelp
import io.legado.app.help.config.AppConfig
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.lib.permission.Permissions
import io.legado.app.lib.permission.PermissionsCompat
@@ -16,7 +17,10 @@ import io.legado.app.utils.LogUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Semaphore
import org.koin.java.KoinJavaComponent.get
import kotlin.coroutines.CoroutineContext
abstract class BaseService : LifecycleService() {
@@ -40,8 +44,18 @@ abstract class BaseService : LifecycleService() {
startForegroundNotification()
isForeground = true
LifecycleHelp.onServiceCreate(this)
if (!AppConfig.permissionChecked) {
AppConfig.permissionChecked = true
val localPreferencesRepository: LocalPreferencesRepository = get(LocalPreferencesRepository::class.java)
val checked: Boolean = runBlocking {
localPreferencesRepository.getPreference(
LocalPreferencesKeys.PERMISSION_CHECKED, false
).first()
}
if (!checked) {
runBlocking {
localPreferencesRepository.updatePreference(
LocalPreferencesKeys.PERMISSION_CHECKED, true
)
}
checkPermission()
}
}
@@ -249,7 +249,6 @@ object PreferKey {
const val mangaClickActionBR = "mangaClickActionBottomRight"
const val mangaLongClick = "mangaLongClick"
const val mangaBackground = "mangaBackground"
const val permissionChecked = "permissionChecked"
const val notificationsPost = "notificationsPost"
const val ignoreBatteryPermission = "ignoreBatteryPermission"
const val paddingDisplayCutouts = "paddingDisplayCutouts"
@@ -5,6 +5,8 @@ import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.longPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
val Context.localDataStore: DataStore<Preferences> by preferencesDataStore(name = "local_ui_status")
@@ -17,4 +19,8 @@ object LocalPreferencesKeys {
val EXPLORE_LAYOUT_GRID_PORTRAIT = intPreferencesKey("explore_layout_grid_portrait")
val EXPLORE_LAYOUT_GRID_LANDSCAPE = intPreferencesKey("explore_layout_grid_landscape")
val READ_URL_IN_BROWSER = booleanPreferencesKey("read_url_in_browser")
val LAST_BACKUP = longPreferencesKey("last_backup")
val PASSWORD = stringPreferencesKey("password")
val PRIVACY_POLICY_OK = booleanPreferencesKey("privacy_policy_ok")
val PERMISSION_CHECKED = booleanPreferencesKey("permission_checked")
}
@@ -99,6 +99,7 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
ReadConfig.optimizeRender = preferences.optimizeRender
ReadConfig.adaptSpecialStyle = preferences.adaptSpecialStyle
ReadConfig.useUnderline = preferences.useUnderline
ReadConfig.chineseConverterType = preferences.chineseConverterType
clickActionTL = preferences.clickActionTL
clickActionTC = preferences.clickActionTC
clickActionTR = preferences.clickActionTR
@@ -244,12 +245,6 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
}
}
var permissionChecked: Boolean
get() = appCtx.getPrefBoolean(PreferKey.permissionChecked, false)
set(value) {
appCtx.putPrefBoolean(PreferKey.permissionChecked, value)
}
val textSelectAble: Boolean
get() = appCtx.getPrefBoolean(PreferKey.textSelectAble, true)
@@ -391,11 +386,8 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
val speechRatePlay: Int get() = if (ttsFlowSys) defaultSpeechRate else ttsSpeechRate
var chineseConverterType: Int
get() = appCtx.getPrefInt(PreferKey.chineseConverterType)
set(value) {
appCtx.putPrefInt(PreferKey.chineseConverterType, value)
}
val chineseConverterType: Int
get() = ReadConfig.chineseConverterType
var systemTypefaces: Int
get() = appCtx.getPrefInt(PreferKey.systemTypefaces)
@@ -3,11 +3,16 @@ package io.legado.app.help.config
import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import io.legado.app.data.local.preferences.LocalPreferencesKeys
import io.legado.app.data.local.preferences.LocalPreferencesRepository
import io.legado.app.utils.getBoolean
import io.legado.app.utils.putBoolean
import io.legado.app.utils.putLong
import io.legado.app.utils.putString
import io.legado.app.utils.remove
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.koin.java.KoinJavaComponent.get
import splitties.init.appCtx
@Suppress("ConstPropertyName")
@@ -16,29 +21,46 @@ by appCtx.getSharedPreferences("local", Context.MODE_PRIVATE) {
private const val versionCodeKey = "appVersionCode"
private val localPreferencesRepository: LocalPreferencesRepository
by lazy { get(LocalPreferencesRepository::class.java) }
/**
* 本地密码,用来对需要备份的敏感信息加密,如 webdav 配置等
*/
var password: String?
get() = getString("password", null)
get() = runBlocking {
localPreferencesRepository.getPreference(LocalPreferencesKeys.PASSWORD, "").first()
.ifEmpty { null }
}
set(value) {
if (value != null) {
putString("password", value)
} else {
remove("password")
runBlocking {
localPreferencesRepository.updatePreference(
LocalPreferencesKeys.PASSWORD, value ?: ""
)
}
}
var lastBackup: Long
get() = getLong("lastBackup", 0)
get() = runBlocking {
localPreferencesRepository.getPreference(LocalPreferencesKeys.LAST_BACKUP, 0L).first()
}
set(value) {
putLong("lastBackup", value)
runBlocking {
localPreferencesRepository.updatePreference(LocalPreferencesKeys.LAST_BACKUP, value)
}
}
var privacyPolicyOk: Boolean
get() = getBoolean("privacyPolicyOk")
get() = runBlocking {
localPreferencesRepository.getPreference(LocalPreferencesKeys.PRIVACY_POLICY_OK, false)
.first()
}
set(value) {
putBoolean("privacyPolicyOk", value)
runBlocking {
localPreferencesRepository.updatePreference(
LocalPreferencesKeys.PRIVACY_POLICY_OK, value
)
}
}
var navExtended: Boolean