Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

数据导出功能 #120

Merged
merged 8 commits into from
Oct 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
优化设置项相关代码
  • Loading branch information
dmzz-yyhyy committed Oct 5, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 83211756a7aacd620be913bba511139ab56f8a21
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package indi.dmzz_yyhyy.lightnovelreader.data.setting

import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.snapshots.StateFactoryMarker
import indi.dmzz_yyhyy.lightnovelreader.data.userdata.UserData
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

abstract class AbstractSettingState(
private val coroutineScope: CoroutineScope,
) {

@StateFactoryMarker
protected fun <T> UserData<T>.asState(initial: T): State<T> {
val state = mutableStateOf(getOrDefault(initial))
coroutineScope.launch(Dispatchers.IO) {
getFlowWithDefault(initial).collect {
state.value = it
}
}
return state
}

@StateFactoryMarker
protected fun <T> UserData<T>.safeAsState(initial: T): State<T> {
val state = mutableStateOf(initial)
coroutineScope.launch(Dispatchers.IO) {
getFlowWithDefault(initial).collect {
state.value = it
}
}
return state
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package indi.dmzz_yyhyy.lightnovelreader.data.userdata

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

abstract class UserData<T> (
open val path: String
@@ -10,11 +14,18 @@ abstract class UserData<T> (
* 此函数为阻塞函数,请务必不要在初始化阶段或主线程上调用
*/
abstract fun set(value: T)
fun asynchronousSet(value: T) {
CoroutineScope(Dispatchers.IO).launch {
set(value)
}
}
/**
* 此函数为阻塞函数,请务必不要在初始化阶段或主线程上调用
*/
abstract fun get(): T?
abstract fun getFlow(): Flow<T?>
fun getFlowWithDefault(default: T): Flow<T> = getFlow().map { it ?: default }
.map { println("${this.path}: $it");it }
/**
* 此函数为阻塞函数,请务必不要在初始化阶段或主线程上调用
*/

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -17,45 +17,11 @@ interface ContentScreenUiState {
if (userReadingData.lastReadChapterId == chapterContent.id) userReadingData.lastReadChapterProgress
else 0f
val bookVolumes: BookVolumes
val fontSize: Float
val fontLineHeight: Float
val keepScreenOn: Boolean
val isUsingFlipPage: Boolean
val isUsingClickFlipPage: Boolean
val isUsingVolumeKeyFlip: Boolean
val isUsingFlipAnime: Boolean
val fastChapterChange: Boolean
val enableBatteryIndicator: Boolean
val enableTimeIndicator: Boolean
val enableReadingChapterProgressIndicator: Boolean
val enableChapterTitleIndicator: Boolean
val autoPadding: Boolean
val topPadding: Float
val bottomPadding: Float
val leftPadding: Float
val rightPadding: Float
}

class MutableContentScreenUiState: ContentScreenUiState {
override var isLoading by mutableStateOf(true)
override var chapterContent by mutableStateOf(ChapterContent.empty())
override var userReadingData by mutableStateOf(UserReadingData.empty())
override var bookVolumes by mutableStateOf(BookVolumes.empty())
override var fontSize by mutableStateOf(14f)
override var fontLineHeight by mutableStateOf(0f)
override var keepScreenOn by mutableStateOf(false)
override var isUsingFlipPage by mutableStateOf(false)
override var isUsingClickFlipPage by mutableStateOf(false)
override var isUsingVolumeKeyFlip by mutableStateOf(false)
override var isUsingFlipAnime by mutableStateOf(false)
override var fastChapterChange by mutableStateOf(false)
override var enableBatteryIndicator by mutableStateOf(true)
override var enableTimeIndicator by mutableStateOf(true)
override var enableReadingChapterProgressIndicator by mutableStateOf(true)
override var enableChapterTitleIndicator by mutableStateOf(true)
override var autoPadding by mutableStateOf(false)
override var topPadding by mutableStateOf(12f)
override var bottomPadding by mutableStateOf(12f)
override var leftPadding by mutableStateOf(16f)
override var rightPadding by mutableStateOf(16f)
}
Original file line number Diff line number Diff line change
@@ -19,25 +19,9 @@ class ContentViewModel @Inject constructor(
) : ViewModel() {
private val _uiState = MutableContentScreenUiState()
private var _bookId: Int = -1
private val fontSizeUserData = userDataRepository.floatUserData(UserDataPath.Reader.FontSize.path)
private val fontLineHeightUserData = userDataRepository.floatUserData(UserDataPath.Reader.FontLineHeight.path)
private val keepScreenOnUserData = userDataRepository.booleanUserData(UserDataPath.Reader.KeepScreenOn.path)
private val isUsingFlipPageUserData = userDataRepository.booleanUserData(UserDataPath.Reader.IsUsingFlipPage.path)
private val isUsingClickFlipPageUserData = userDataRepository.booleanUserData(UserDataPath.Reader.IsUsingClickFlipPage.path)
private val isUsingVolumeKeyFlipUserData = userDataRepository.booleanUserData(UserDataPath.Reader.IsUsingVolumeKeyFlip.path)
private val isUsingFlipAnimeUserData = userDataRepository.booleanUserData(UserDataPath.Reader.IsUsingFlipAnime.path)
private val fastChapterChangeUserData = userDataRepository.booleanUserData(UserDataPath.Reader.FastChapterChange.path)
private val enableBatteryIndicatorUserData = userDataRepository.booleanUserData(UserDataPath.Reader.EnableBatteryIndicator.path)
private val enableTimeIndicatorUserData = userDataRepository.booleanUserData(UserDataPath.Reader.EnableTimeIndicator.path)
private val enableChapterTitleIndicatorUserData = userDataRepository.booleanUserData(UserDataPath.Reader.EnableChapterTitleIndicator.path)
private val enableReadingChapterProgressIndicatorUserData = userDataRepository.booleanUserData(UserDataPath.Reader.EnableReadingChapterProgressIndicator.path)
private val autoPaddingUserData = userDataRepository.booleanUserData(UserDataPath.Reader.AutoPadding.path)
private val topPaddingUserData = userDataRepository.floatUserData(UserDataPath.Reader.TopPadding.path)
private val bottomPaddingUserData = userDataRepository.floatUserData(UserDataPath.Reader.BottomPadding.path)
private val leftPaddingUserData = userDataRepository.floatUserData(UserDataPath.Reader.LeftPadding.path)
private val rightPaddingUserData = userDataRepository.floatUserData(UserDataPath.Reader.RightPadding.path)
private val readingBookListUserData = userDataRepository.intListUserData(UserDataPath.ReadingBooks.path)
val uiState: ContentScreenUiState = _uiState
val settingState = SettingState(userDataRepository, viewModelScope)

@Suppress("DuplicatedCode")
fun init(bookId: Int, chapterId: Int) {
@@ -60,26 +44,6 @@ class ContentViewModel @Inject constructor(
_uiState.userReadingData = it
}
}

viewModelScope.launch(Dispatchers.IO) {
_uiState.fontSize = fontSizeUserData.getOrDefault(_uiState.fontSize)
_uiState.fontLineHeight = fontLineHeightUserData.getOrDefault(_uiState.fontLineHeight)
_uiState.keepScreenOn = keepScreenOnUserData.getOrDefault(_uiState.keepScreenOn)
_uiState.isUsingFlipPage = isUsingFlipPageUserData.getOrDefault(_uiState.isUsingFlipPage)
_uiState.isUsingClickFlipPage = isUsingClickFlipPageUserData.getOrDefault(_uiState.isUsingClickFlipPage)
_uiState.isUsingVolumeKeyFlip = isUsingVolumeKeyFlipUserData.getOrDefault(_uiState.isUsingVolumeKeyFlip)
_uiState.isUsingFlipAnime = isUsingFlipAnimeUserData.getOrDefault(_uiState.isUsingFlipAnime)
_uiState.fastChapterChange = fastChapterChangeUserData.getOrDefault(_uiState.fastChapterChange)
_uiState.enableBatteryIndicator = enableBatteryIndicatorUserData.getOrDefault(_uiState.enableBatteryIndicator)
_uiState.enableTimeIndicator = enableTimeIndicatorUserData.getOrDefault(_uiState.enableTimeIndicator)
_uiState.enableChapterTitleIndicator = enableChapterTitleIndicatorUserData.getOrDefault(_uiState.enableChapterTitleIndicator)
_uiState.enableReadingChapterProgressIndicator = enableReadingChapterProgressIndicatorUserData.getOrDefault(_uiState.enableReadingChapterProgressIndicator)
_uiState.autoPadding = autoPaddingUserData.getOrDefault(_uiState.autoPadding)
_uiState.topPadding = topPaddingUserData.getOrDefault(_uiState.topPadding)
_uiState.bottomPadding = bottomPaddingUserData.getOrDefault(_uiState.bottomPadding)
_uiState.leftPadding = leftPaddingUserData.getOrDefault(_uiState.leftPadding)
_uiState.rightPadding = rightPaddingUserData.getOrDefault(_uiState.rightPadding)
}
}

private fun loadChapterContent(bookId: Int, chapterId: Int) {
@@ -173,143 +137,6 @@ class ContentViewModel @Inject constructor(
}
}

fun changeFontSize(size: Float) {
_uiState.fontSize = size
}

fun changeFontLineHeight(height: Float) {
_uiState.fontLineHeight = height
}

fun saveFontSize() {
viewModelScope.launch(Dispatchers.IO) {
fontSizeUserData.set(_uiState.fontSize)
}
}

fun saveFontLineHeight() {
viewModelScope.launch(Dispatchers.IO) {
fontLineHeightUserData.set(_uiState.fontLineHeight)
}
}

fun changeKeepScreenOn(keepScreenOn: Boolean) {
_uiState.keepScreenOn = keepScreenOn
viewModelScope.launch(Dispatchers.IO) {
keepScreenOnUserData.set(keepScreenOn)
}
}

fun changeIsUsingFlipPage(isUsingFlipPage: Boolean) {
_uiState.isUsingFlipPage = isUsingFlipPage
viewModelScope.launch(Dispatchers.IO) {
isUsingFlipPageUserData.set(isUsingFlipPage)
}
}

fun changeIsUsingClickFlipPage(isUsingClickFlipPage: Boolean) {
_uiState.isUsingClickFlipPage = isUsingClickFlipPage
viewModelScope.launch(Dispatchers.IO) {
isUsingClickFlipPageUserData.set(isUsingClickFlipPage)
}
}

fun changeIsUsingVolumeKeyFlip(isUsingVolumeKeyFlip: Boolean) {
_uiState.isUsingVolumeKeyFlip = isUsingVolumeKeyFlip
viewModelScope.launch(Dispatchers.IO) {
isUsingVolumeKeyFlipUserData.set(isUsingVolumeKeyFlip)
}
}

fun changeIsUsingFlipAnime(isUsingFlipAnime: Boolean) {
_uiState.isUsingFlipAnime = isUsingFlipAnime
viewModelScope.launch(Dispatchers.IO) {
isUsingFlipAnimeUserData.set(isUsingFlipAnime)
}
}

fun changeFastChapterChange(fastChapterChange: Boolean) {
_uiState.fastChapterChange = fastChapterChange
viewModelScope.launch(Dispatchers.IO) {
fastChapterChangeUserData.set(fastChapterChange)
}
}

fun changeEnableBatteryIndicator(enableBatteryIndicator: Boolean) {
_uiState.enableBatteryIndicator = enableBatteryIndicator
viewModelScope.launch(Dispatchers.IO) {
enableBatteryIndicatorUserData.set(enableBatteryIndicator)
}
}

fun changeEnableTimeIndicator(enableTimeIndicator: Boolean) {
_uiState.enableTimeIndicator = enableTimeIndicator
viewModelScope.launch(Dispatchers.IO) {
enableTimeIndicatorUserData.set(enableTimeIndicator)
}
}

fun changeEnableChapterTitleIndicator(enableChapterTitleIndicator: Boolean) {
_uiState.enableChapterTitleIndicator = enableChapterTitleIndicator
viewModelScope.launch(Dispatchers.IO) {
enableChapterTitleIndicatorUserData.set(enableChapterTitleIndicator)
}
}

fun changeEnableReadingChapterProgressIndicator(enableReadingChapterProgressIndicator: Boolean) {
_uiState.enableReadingChapterProgressIndicator = enableReadingChapterProgressIndicator
viewModelScope.launch(Dispatchers.IO) {
enableReadingChapterProgressIndicatorUserData.set(enableReadingChapterProgressIndicator)
}
}

fun changeAutoPadding(autoPadding: Boolean) {
_uiState.autoPadding = autoPadding
viewModelScope.launch(Dispatchers.IO) {
autoPaddingUserData.set(autoPadding)
}
}

fun changeTopPadding(padding: Float) {
_uiState.topPadding = padding
}

fun saveTopPadding() {
viewModelScope.launch(Dispatchers.IO) {
topPaddingUserData.set(_uiState.topPadding)
}
}

fun changeBottomPadding(padding: Float) {
_uiState.bottomPadding = padding
}

fun saveBottomPadding() {
viewModelScope.launch(Dispatchers.IO) {
bottomPaddingUserData.set(_uiState.bottomPadding)
}
}

fun changeLeftPadding(padding: Float) {
_uiState.leftPadding = padding
}

fun saveLeftPadding() {
viewModelScope.launch(Dispatchers.IO) {
leftPaddingUserData.set(_uiState.leftPadding)
}
}

fun changeRightPadding(padding: Float) {
_uiState.rightPadding = padding
}

fun saveRightPadding() {
viewModelScope.launch(Dispatchers.IO) {
rightPaddingUserData.set(_uiState.rightPadding)
}
}

fun addToReadingBook(bookId: Int) {
viewModelScope.launch(Dispatchers.IO) {
readingBookListUserData.update {
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package indi.dmzz_yyhyy.lightnovelreader.ui.book.content

import androidx.compose.runtime.getValue
import indi.dmzz_yyhyy.lightnovelreader.data.UserDataRepository
import indi.dmzz_yyhyy.lightnovelreader.data.setting.AbstractSettingState
import indi.dmzz_yyhyy.lightnovelreader.data.userdata.UserDataPath
import kotlinx.coroutines.CoroutineScope

class SettingState(
userDataRepository: UserDataRepository,
coroutineScope: CoroutineScope
) : AbstractSettingState(coroutineScope) {
val fontSizeUserData = userDataRepository.floatUserData(UserDataPath.Reader.FontSize.path)
val fontLineHeightUserData = userDataRepository.floatUserData(UserDataPath.Reader.FontLineHeight.path)
val keepScreenOnUserData = userDataRepository.booleanUserData(UserDataPath.Reader.KeepScreenOn.path)
val isUsingFlipPageUserData = userDataRepository.booleanUserData(UserDataPath.Reader.IsUsingFlipPage.path)
val isUsingClickFlipPageUserData = userDataRepository.booleanUserData(UserDataPath.Reader.IsUsingClickFlipPage.path)
val isUsingVolumeKeyFlipUserData = userDataRepository.booleanUserData(UserDataPath.Reader.IsUsingVolumeKeyFlip.path)
val isUsingFlipAnimeUserData = userDataRepository.booleanUserData(UserDataPath.Reader.IsUsingFlipAnime.path)
val fastChapterChangeUserData = userDataRepository.booleanUserData(UserDataPath.Reader.FastChapterChange.path)
val enableBatteryIndicatorUserData = userDataRepository.booleanUserData(UserDataPath.Reader.EnableBatteryIndicator.path)
val enableTimeIndicatorUserData = userDataRepository.booleanUserData(UserDataPath.Reader.EnableTimeIndicator.path)
val enableChapterTitleIndicatorUserData = userDataRepository.booleanUserData(
UserDataPath.Reader.EnableChapterTitleIndicator.path)
val enableReadingChapterProgressIndicatorUserData = userDataRepository.booleanUserData(
UserDataPath.Reader.EnableReadingChapterProgressIndicator.path)
val autoPaddingUserData = userDataRepository.booleanUserData(UserDataPath.Reader.AutoPadding.path)
val topPaddingUserData = userDataRepository.floatUserData(UserDataPath.Reader.TopPadding.path)
val bottomPaddingUserData = userDataRepository.floatUserData(UserDataPath.Reader.BottomPadding.path)
val leftPaddingUserData = userDataRepository.floatUserData(UserDataPath.Reader.LeftPadding.path)
val rightPaddingUserData = userDataRepository.floatUserData(UserDataPath.Reader.RightPadding.path)

val fontSize by fontSizeUserData.safeAsState(14f)
val fontLineHeight by fontLineHeightUserData.safeAsState(0f)
val keepScreenOn by keepScreenOnUserData.safeAsState(false)
val isUsingFlipPage by isUsingFlipPageUserData.safeAsState(false)
val isUsingClickFlipPage by isUsingClickFlipPageUserData.safeAsState(false)
val isUsingVolumeKeyFlip by isUsingVolumeKeyFlipUserData.safeAsState(false)
val isUsingFlipAnime by isUsingFlipAnimeUserData.safeAsState(true)
val fastChapterChange by fastChapterChangeUserData.safeAsState(false)
val enableBatteryIndicator by enableBatteryIndicatorUserData.safeAsState(true)
val enableTimeIndicator by enableTimeIndicatorUserData.safeAsState(true)
val enableChapterTitleIndicator by enableChapterTitleIndicatorUserData.safeAsState(true)
val enableReadingChapterProgressIndicator by enableReadingChapterProgressIndicatorUserData.safeAsState(true)
val autoPadding by autoPaddingUserData.safeAsState(true)
val topPadding by topPaddingUserData.safeAsState(12f)
val bottomPadding by bottomPaddingUserData.safeAsState(12f)
val leftPadding by leftPaddingUserData.safeAsState(16f)
val rightPadding by rightPaddingUserData.safeAsState(16f)
}
Loading