|
@@ -3,7 +3,10 @@ package io.legado.app.ui.config.readConfig
|
|
|
import androidx.lifecycle.ViewModel
|
|
import androidx.lifecycle.ViewModel
|
|
|
import androidx.lifecycle.viewModelScope
|
|
import androidx.lifecycle.viewModelScope
|
|
|
import io.legado.app.domain.gateway.ReadSettingsGateway
|
|
import io.legado.app.domain.gateway.ReadSettingsGateway
|
|
|
|
|
+import io.legado.app.domain.gateway.ThemeSettingsGateway
|
|
|
import io.legado.app.domain.model.settings.ReadSettings
|
|
import io.legado.app.domain.model.settings.ReadSettings
|
|
|
|
|
+import io.legado.app.domain.model.settings.ThemeSettings
|
|
|
|
|
+import io.legado.app.ui.book.read.EyeProtectionUiState
|
|
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
|
import kotlinx.coroutines.flow.asSharedFlow
|
|
import kotlinx.coroutines.flow.asSharedFlow
|
|
@@ -13,11 +16,15 @@ import kotlinx.coroutines.launch
|
|
|
|
|
|
|
|
class ReadConfigViewModel(
|
|
class ReadConfigViewModel(
|
|
|
private val settingsGateway: ReadSettingsGateway,
|
|
private val settingsGateway: ReadSettingsGateway,
|
|
|
|
|
+ private val themeSettingsGateway: ThemeSettingsGateway,
|
|
|
private val applyReadSetting: ApplyReadSettingUseCase,
|
|
private val applyReadSetting: ApplyReadSettingUseCase,
|
|
|
) : ViewModel() {
|
|
) : ViewModel() {
|
|
|
|
|
|
|
|
private val _uiState = MutableStateFlow(
|
|
private val _uiState = MutableStateFlow(
|
|
|
- settingsGateway.currentSettings.toUiState(activeSheet = null)
|
|
|
|
|
|
|
+ settingsGateway.currentSettings.toUiState(
|
|
|
|
|
+ activeSheet = null,
|
|
|
|
|
+ eyeProtection = themeSettingsGateway.currentSettings.toEyeProtectionUiState(),
|
|
|
|
|
+ )
|
|
|
)
|
|
)
|
|
|
val uiState = _uiState.asStateFlow()
|
|
val uiState = _uiState.asStateFlow()
|
|
|
|
|
|
|
@@ -27,7 +34,17 @@ class ReadConfigViewModel(
|
|
|
init {
|
|
init {
|
|
|
viewModelScope.launch {
|
|
viewModelScope.launch {
|
|
|
settingsGateway.settings.collect { settings ->
|
|
settingsGateway.settings.collect { settings ->
|
|
|
- _uiState.update { settings.toUiState(activeSheet = it.activeSheet) }
|
|
|
|
|
|
|
+ _uiState.update {
|
|
|
|
|
+ settings.toUiState(
|
|
|
|
|
+ activeSheet = it.activeSheet,
|
|
|
|
|
+ eyeProtection = it.eyeProtection,
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ viewModelScope.launch {
|
|
|
|
|
+ themeSettingsGateway.settings.collect { settings ->
|
|
|
|
|
+ _uiState.update { it.copy(eyeProtection = settings.toEyeProtectionUiState()) }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -36,11 +53,34 @@ class ReadConfigViewModel(
|
|
|
when (intent) {
|
|
when (intent) {
|
|
|
ReadConfigIntent.OpenPageKeys -> setSheet(ReadConfigSheet.PageKeys)
|
|
ReadConfigIntent.OpenPageKeys -> setSheet(ReadConfigSheet.PageKeys)
|
|
|
ReadConfigIntent.OpenClickActions -> setSheet(ReadConfigSheet.ClickActions)
|
|
ReadConfigIntent.OpenClickActions -> setSheet(ReadConfigSheet.ClickActions)
|
|
|
|
|
+ ReadConfigIntent.OpenEyeProtection -> setSheet(ReadConfigSheet.EyeProtection)
|
|
|
ReadConfigIntent.DismissSheet -> setSheet(null)
|
|
ReadConfigIntent.DismissSheet -> setSheet(null)
|
|
|
|
|
+ is ReadConfigIntent.EyeProtectionEnabledChanged -> updateEyeProtection {
|
|
|
|
|
+ it.copy(eyeProtectionEnabled = intent.value)
|
|
|
|
|
+ }
|
|
|
|
|
+ is ReadConfigIntent.EyeProtectionIntensityChanged -> updateEyeProtection {
|
|
|
|
|
+ it.copy(colorTemperature = intent.value.coerceIn(0, 100))
|
|
|
|
|
+ }
|
|
|
|
|
+ is ReadConfigIntent.EyeProtectionAutoNightChanged -> updateEyeProtection {
|
|
|
|
|
+ it.copy(eyeProtectionAutoNight = intent.value)
|
|
|
|
|
+ }
|
|
|
|
|
+ is ReadConfigIntent.EyeProtectionScheduleChanged -> updateEyeProtection {
|
|
|
|
|
+ it.copy(eyeProtectionSchedule = intent.value)
|
|
|
|
|
+ }
|
|
|
|
|
+ is ReadConfigIntent.EyeProtectionStartTimeChanged -> updateEyeProtection {
|
|
|
|
|
+ it.copy(eyeProtectionStartTime = intent.value)
|
|
|
|
|
+ }
|
|
|
|
|
+ is ReadConfigIntent.EyeProtectionEndTimeChanged -> updateEyeProtection {
|
|
|
|
|
+ it.copy(eyeProtectionEndTime = intent.value)
|
|
|
|
|
+ }
|
|
|
else -> updateSetting(intent)
|
|
else -> updateSetting(intent)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private fun updateEyeProtection(transform: (ThemeSettings) -> ThemeSettings) {
|
|
|
|
|
+ viewModelScope.launch { themeSettingsGateway.update(transform) }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private fun setSheet(sheet: ReadConfigSheet?) {
|
|
private fun setSheet(sheet: ReadConfigSheet?) {
|
|
|
_uiState.update { it.copy(activeSheet = sheet) }
|
|
_uiState.update { it.copy(activeSheet = sheet) }
|
|
|
}
|
|
}
|
|
@@ -102,22 +142,32 @@ private fun ReadConfigIntent.toSettingsTransform(): (ReadSettings) -> ReadSettin
|
|
|
is ReadConfigIntent.PageKeysChanged -> { settings ->
|
|
is ReadConfigIntent.PageKeysChanged -> { settings ->
|
|
|
settings.copy(prevKeys = prevKeys, nextKeys = nextKeys)
|
|
settings.copy(prevKeys = prevKeys, nextKeys = nextKeys)
|
|
|
}
|
|
}
|
|
|
- is ReadConfigIntent.EyeProtectionEnabledChanged -> { settings ->
|
|
|
|
|
- settings.copy(eyeProtectionEnabled = value)
|
|
|
|
|
- }
|
|
|
|
|
- is ReadConfigIntent.EyeProtectionIntensityChanged -> { settings ->
|
|
|
|
|
- settings.copy(eyeProtectionIntensity = value)
|
|
|
|
|
- }
|
|
|
|
|
- is ReadConfigIntent.EyeProtectionAutoNightChanged -> { settings ->
|
|
|
|
|
- settings.copy(eyeProtectionAutoNight = value)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ is ReadConfigIntent.EyeProtectionEnabledChanged,
|
|
|
|
|
+ is ReadConfigIntent.EyeProtectionIntensityChanged,
|
|
|
|
|
+ is ReadConfigIntent.EyeProtectionAutoNightChanged,
|
|
|
|
|
+ is ReadConfigIntent.EyeProtectionScheduleChanged,
|
|
|
|
|
+ is ReadConfigIntent.EyeProtectionStartTimeChanged,
|
|
|
|
|
+ is ReadConfigIntent.EyeProtectionEndTimeChanged ->
|
|
|
|
|
+ error("Eye protection intents update ThemeSettings")
|
|
|
ReadConfigIntent.OpenPageKeys,
|
|
ReadConfigIntent.OpenPageKeys,
|
|
|
ReadConfigIntent.OpenClickActions,
|
|
ReadConfigIntent.OpenClickActions,
|
|
|
ReadConfigIntent.OpenEyeProtection,
|
|
ReadConfigIntent.OpenEyeProtection,
|
|
|
ReadConfigIntent.DismissSheet -> error("Sheet intents do not update settings")
|
|
ReadConfigIntent.DismissSheet -> error("Sheet intents do not update settings")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-private fun ReadSettings.toUiState(activeSheet: ReadConfigSheet?): ReadConfigUiState =
|
|
|
|
|
|
|
+private fun ThemeSettings.toEyeProtectionUiState(): EyeProtectionUiState = EyeProtectionUiState(
|
|
|
|
|
+ enabled = eyeProtectionEnabled,
|
|
|
|
|
+ intensity = colorTemperature,
|
|
|
|
|
+ autoNight = eyeProtectionAutoNight,
|
|
|
|
|
+ schedule = eyeProtectionSchedule,
|
|
|
|
|
+ startTime = eyeProtectionStartTime,
|
|
|
|
|
+ endTime = eyeProtectionEndTime,
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+private fun ReadSettings.toUiState(
|
|
|
|
|
+ activeSheet: ReadConfigSheet?,
|
|
|
|
|
+ eyeProtection: EyeProtectionUiState,
|
|
|
|
|
+): ReadConfigUiState =
|
|
|
ReadConfigUiState(
|
|
ReadConfigUiState(
|
|
|
screenOrientation = screenOrientation,
|
|
screenOrientation = screenOrientation,
|
|
|
keepLight = keepLight,
|
|
keepLight = keepLight,
|
|
@@ -132,9 +182,7 @@ private fun ReadSettings.toUiState(activeSheet: ReadConfigSheet?): ReadConfigUiS
|
|
|
textBottomJustify = textBottomJustify,
|
|
textBottomJustify = textBottomJustify,
|
|
|
adaptSpecialStyle = adaptSpecialStyle,
|
|
adaptSpecialStyle = adaptSpecialStyle,
|
|
|
useZhLayout = useZhLayout,
|
|
useZhLayout = useZhLayout,
|
|
|
- eyeProtectionEnabled = eyeProtectionEnabled,
|
|
|
|
|
- eyeProtectionIntensity = eyeProtectionIntensity,
|
|
|
|
|
- eyeProtectionAutoNight = eyeProtectionAutoNight,
|
|
|
|
|
|
|
+ eyeProtection = eyeProtection,
|
|
|
showBrightnessView = showBrightnessView,
|
|
showBrightnessView = showBrightnessView,
|
|
|
brightnessVwPos = brightnessVwPos,
|
|
brightnessVwPos = brightnessVwPos,
|
|
|
brightnessAuto = brightnessAuto,
|
|
brightnessAuto = brightnessAuto,
|