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