🛠️ MainViewModel reformat
* Reformatted MainEvent * Changed various variables names Thanks for idea: Co-authored-by: Ridho Pratama <dhoable@gmail.com>
This commit is contained in:
parent
3d3d85415e
commit
f888c3b0c7
14 changed files with 358 additions and 500 deletions
|
|
@ -162,7 +162,7 @@ class BookRepositoryImpl @Inject constructor(
|
||||||
*
|
*
|
||||||
* @return If [Resource.Success] and returns null, then the book is up-to-date.
|
* @return If [Resource.Success] and returns null, then the book is up-to-date.
|
||||||
*/
|
*/
|
||||||
override suspend fun checkTextForUpdate(bookId: Int): Resource<Pair<List<String>, List<Chapter>>?> {
|
override suspend fun checkForTextUpdate(bookId: Int): Resource<Pair<List<String>, List<Chapter>>?> {
|
||||||
yield()
|
yield()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ interface BookRepository {
|
||||||
textPath: String
|
textPath: String
|
||||||
): List<AnnotatedString>
|
): List<AnnotatedString>
|
||||||
|
|
||||||
suspend fun checkTextForUpdate(bookId: Int): Resource<Pair<List<String>, List<Chapter>>?>
|
suspend fun checkForTextUpdate(bookId: Int): Resource<Pair<List<String>, List<Chapter>>?>
|
||||||
|
|
||||||
suspend fun insertBook(
|
suspend fun insertBook(
|
||||||
bookWithTextAndCover: BookWithTextAndCover
|
bookWithTextAndCover: BookWithTextAndCover
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,9 @@ import ua.acclorite.book_story.domain.repository.BookRepository
|
||||||
import ua.acclorite.book_story.domain.util.Resource
|
import ua.acclorite.book_story.domain.util.Resource
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class CheckTextForUpdate @Inject constructor(private val repository: BookRepository) {
|
class CheckForTextUpdate @Inject constructor(private val repository: BookRepository) {
|
||||||
|
|
||||||
suspend fun execute(bookId: Int): Resource<Pair<List<String>, List<Chapter>>?> {
|
suspend fun execute(bookId: Int): Resource<Pair<List<String>, List<Chapter>>?> {
|
||||||
return repository.checkTextForUpdate(bookId)
|
return repository.checkForTextUpdate(bookId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -4,43 +4,43 @@ import androidx.compose.runtime.Immutable
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
sealed class MainEvent {
|
sealed class MainEvent {
|
||||||
data class OnChangeLanguage(val lang: String) : MainEvent()
|
data class OnChangeLanguage(val value: String) : MainEvent()
|
||||||
data class OnChangeTheme(val theme: String) : MainEvent()
|
data class OnChangeTheme(val value: String) : MainEvent()
|
||||||
data class OnChangeDarkTheme(val darkTheme: String) : MainEvent()
|
data class OnChangeDarkTheme(val value: String) : MainEvent()
|
||||||
data class OnChangePureDark(val pureDark: String) : MainEvent()
|
data class OnChangePureDark(val value: String) : MainEvent()
|
||||||
data class OnChangeThemeContrast(val themeContrast: String) : MainEvent()
|
data class OnChangeThemeContrast(val value: String) : MainEvent()
|
||||||
data class OnChangeFontFamily(val fontFamily: String) : MainEvent()
|
data class OnChangeFontFamily(val value: String) : MainEvent()
|
||||||
data class OnChangeFontStyle(val fontStyle: Boolean) : MainEvent()
|
data class OnChangeFontStyle(val value: Boolean) : MainEvent()
|
||||||
data class OnChangeFontSize(val fontSize: Int) : MainEvent()
|
data class OnChangeFontSize(val value: Int) : MainEvent()
|
||||||
data class OnChangeLineHeight(val lineHeight: Int) : MainEvent()
|
data class OnChangeLineHeight(val value: Int) : MainEvent()
|
||||||
data class OnChangeParagraphHeight(val paragraphHeight: Int) : MainEvent()
|
data class OnChangeParagraphHeight(val value: Int) : MainEvent()
|
||||||
data class OnChangeParagraphIndentation(val indentation: Int) : MainEvent()
|
data class OnChangeParagraphIndentation(val value: Int) : MainEvent()
|
||||||
data class OnChangeShowStartScreen(val bool: Boolean) : MainEvent()
|
data class OnChangeShowStartScreen(val value: Boolean) : MainEvent()
|
||||||
data class OnChangeCheckForUpdates(val bool: Boolean) : MainEvent()
|
data class OnChangeCheckForUpdates(val value: Boolean) : MainEvent()
|
||||||
data class OnChangeSidePadding(val sidePadding: Int) : MainEvent()
|
data class OnChangeSidePadding(val value: Int) : MainEvent()
|
||||||
data class OnChangeDoubleClickTranslation(val bool: Boolean) : MainEvent()
|
data class OnChangeDoubleClickTranslation(val value: Boolean) : MainEvent()
|
||||||
data class OnChangeFastColorPresetChange(val bool: Boolean) : MainEvent()
|
data class OnChangeFastColorPresetChange(val value: Boolean) : MainEvent()
|
||||||
data class OnChangeBrowseFilesStructure(val structure: String) : MainEvent()
|
data class OnChangeBrowseFilesStructure(val value: String) : MainEvent()
|
||||||
data class OnChangeBrowseLayout(val layout: String) : MainEvent()
|
data class OnChangeBrowseLayout(val value: String) : MainEvent()
|
||||||
data class OnChangeBrowseAutoGridSize(val bool: Boolean) : MainEvent()
|
data class OnChangeBrowseAutoGridSize(val value: Boolean) : MainEvent()
|
||||||
data class OnChangeBrowseGridSize(val size: Int) : MainEvent()
|
data class OnChangeBrowseGridSize(val value: Int) : MainEvent()
|
||||||
data class OnChangeBrowsePinFavoriteDirectories(val bool: Boolean) : MainEvent()
|
data class OnChangeBrowsePinFavoriteDirectories(val value: Boolean) : MainEvent()
|
||||||
data class OnChangeBrowseSortOrder(val order: String) : MainEvent()
|
data class OnChangeBrowseSortOrder(val value: String) : MainEvent()
|
||||||
data class OnChangeBrowseSortOrderDescending(val bool: Boolean) : MainEvent()
|
data class OnChangeBrowseSortOrderDescending(val value: Boolean) : MainEvent()
|
||||||
data class OnChangeBrowseIncludedFilterItem(val item: String) : MainEvent()
|
data class OnChangeBrowseIncludedFilterItem(val value: String) : MainEvent()
|
||||||
data class OnChangeTextAlignment(val alignment: String) : MainEvent()
|
data class OnChangeTextAlignment(val value: String) : MainEvent()
|
||||||
data class OnChangeDoublePressExit(val bool: Boolean) : MainEvent()
|
data class OnChangeDoublePressExit(val value: Boolean) : MainEvent()
|
||||||
data class OnChangeLetterSpacing(val spacing: Int) : MainEvent()
|
data class OnChangeLetterSpacing(val value: Int) : MainEvent()
|
||||||
data class OnChangeAbsoluteDark(val bool: Boolean) : MainEvent()
|
data class OnChangeAbsoluteDark(val value: Boolean) : MainEvent()
|
||||||
data class OnChangeCutoutPadding(val bool: Boolean) : MainEvent()
|
data class OnChangeCutoutPadding(val value: Boolean) : MainEvent()
|
||||||
data class OnChangeFullscreen(val bool: Boolean) : MainEvent()
|
data class OnChangeFullscreen(val value: Boolean) : MainEvent()
|
||||||
data class OnChangeKeepScreenOn(val bool: Boolean) : MainEvent()
|
data class OnChangeKeepScreenOn(val value: Boolean) : MainEvent()
|
||||||
data class OnChangeVerticalPadding(val padding: Int) : MainEvent()
|
data class OnChangeVerticalPadding(val value: Int) : MainEvent()
|
||||||
data class OnChangeHideBarsOnFastScroll(val bool: Boolean) : MainEvent()
|
data class OnChangeHideBarsOnFastScroll(val value: Boolean) : MainEvent()
|
||||||
data class OnChangePerceptionExpander(val bool: Boolean) : MainEvent()
|
data class OnChangePerceptionExpander(val value: Boolean) : MainEvent()
|
||||||
data class OnChangePerceptionExpanderPadding(val padding: Int) : MainEvent()
|
data class OnChangePerceptionExpanderPadding(val value: Int) : MainEvent()
|
||||||
data class OnChangePerceptionExpanderThickness(val thickness: Int) : MainEvent()
|
data class OnChangePerceptionExpanderThickness(val value: Int) : MainEvent()
|
||||||
data class OnChangeCheckForTextUpdate(val bool: Boolean) : MainEvent()
|
data class OnChangeCheckForTextUpdate(val value: Boolean) : MainEvent()
|
||||||
data class OnChangeCheckForTextUpdateToast(val bool: Boolean) : MainEvent()
|
data class OnChangeCheckForTextUpdateToast(val value: Boolean) : MainEvent()
|
||||||
data class OnChangeScreenOrientation(val orientation: String) : MainEvent()
|
data class OnChangeScreenOrientation(val value: String) : MainEvent()
|
||||||
}
|
}
|
||||||
|
|
@ -13,9 +13,9 @@ import ua.acclorite.book_story.presentation.core.constants.DataStoreConstants
|
||||||
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.BrowseFilesStructure
|
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.BrowseFilesStructure
|
||||||
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.BrowseLayout
|
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.BrowseLayout
|
||||||
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.BrowseSortOrder
|
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.BrowseSortOrder
|
||||||
|
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.toBrowseFilesStructure
|
||||||
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.toBrowseLayout
|
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.toBrowseLayout
|
||||||
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.toBrowseSortOrder
|
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.toBrowseSortOrder
|
||||||
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.toFilesStructure
|
|
||||||
import ua.acclorite.book_story.presentation.screens.settings.nested.reader.data.ReaderScreenOrientation
|
import ua.acclorite.book_story.presentation.screens.settings.nested.reader.data.ReaderScreenOrientation
|
||||||
import ua.acclorite.book_story.presentation.screens.settings.nested.reader.data.ReaderTextAlignment
|
import ua.acclorite.book_story.presentation.screens.settings.nested.reader.data.ReaderTextAlignment
|
||||||
import ua.acclorite.book_story.presentation.screens.settings.nested.reader.data.toReaderScreenOrientation
|
import ua.acclorite.book_story.presentation.screens.settings.nested.reader.data.toReaderScreenOrientation
|
||||||
|
|
@ -187,7 +187,7 @@ data class MainState(
|
||||||
) { fastColorPresetChange },
|
) { fastColorPresetChange },
|
||||||
|
|
||||||
browseFilesStructure = provideValue(
|
browseFilesStructure = provideValue(
|
||||||
BROWSE_FILES_STRUCTURE, convert = { toFilesStructure() }
|
BROWSE_FILES_STRUCTURE, convert = { toBrowseFilesStructure() }
|
||||||
) { browseFilesStructure },
|
) { browseFilesStructure },
|
||||||
|
|
||||||
browseLayout = provideValue(
|
browseLayout = provideValue(
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package ua.acclorite.book_story.presentation.data
|
package ua.acclorite.book_story.presentation.data
|
||||||
|
|
||||||
|
import androidx.datastore.preferences.core.Preferences
|
||||||
import androidx.lifecycle.SavedStateHandle
|
import androidx.lifecycle.SavedStateHandle
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
|
@ -11,6 +12,7 @@ import kotlinx.coroutines.flow.combine
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import ua.acclorite.book_story.domain.use_case.ChangeLanguage
|
import ua.acclorite.book_story.domain.use_case.ChangeLanguage
|
||||||
import ua.acclorite.book_story.domain.use_case.CheckForUpdates
|
import ua.acclorite.book_story.domain.use_case.CheckForUpdates
|
||||||
import ua.acclorite.book_story.domain.use_case.GetAllSettings
|
import ua.acclorite.book_story.domain.use_case.GetAllSettings
|
||||||
|
|
@ -20,9 +22,9 @@ import ua.acclorite.book_story.presentation.core.constants.DataStoreConstants
|
||||||
import ua.acclorite.book_story.presentation.core.util.BaseViewModel
|
import ua.acclorite.book_story.presentation.core.util.BaseViewModel
|
||||||
import ua.acclorite.book_story.presentation.screens.library.data.LibraryViewModel
|
import ua.acclorite.book_story.presentation.screens.library.data.LibraryViewModel
|
||||||
import ua.acclorite.book_story.presentation.screens.settings.data.SettingsViewModel
|
import ua.acclorite.book_story.presentation.screens.settings.data.SettingsViewModel
|
||||||
|
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.toBrowseFilesStructure
|
||||||
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.toBrowseLayout
|
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.toBrowseLayout
|
||||||
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.toBrowseSortOrder
|
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.toBrowseSortOrder
|
||||||
import ua.acclorite.book_story.presentation.screens.settings.nested.browse.data.toFilesStructure
|
|
||||||
import ua.acclorite.book_story.presentation.screens.settings.nested.reader.data.toReaderScreenOrientation
|
import ua.acclorite.book_story.presentation.screens.settings.nested.reader.data.toReaderScreenOrientation
|
||||||
import ua.acclorite.book_story.presentation.screens.settings.nested.reader.data.toTextAlignment
|
import ua.acclorite.book_story.presentation.screens.settings.nested.reader.data.toTextAlignment
|
||||||
import ua.acclorite.book_story.presentation.ui.toDarkTheme
|
import ua.acclorite.book_story.presentation.ui.toDarkTheme
|
||||||
|
|
@ -31,9 +33,7 @@ import ua.acclorite.book_story.presentation.ui.toTheme
|
||||||
import ua.acclorite.book_story.presentation.ui.toThemeContrast
|
import ua.acclorite.book_story.presentation.ui.toThemeContrast
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
/**
|
|
||||||
* Stores all variables such as theme, language etc
|
|
||||||
*/
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class MainViewModel @Inject constructor(
|
class MainViewModel @Inject constructor(
|
||||||
private val stateHandle: SavedStateHandle,
|
private val stateHandle: SavedStateHandle,
|
||||||
|
|
@ -56,494 +56,312 @@ class MainViewModel @Inject constructor(
|
||||||
override val state = _state.asStateFlow()
|
override val state = _state.asStateFlow()
|
||||||
|
|
||||||
override fun onEvent(event: MainEvent) {
|
override fun onEvent(event: MainEvent) {
|
||||||
when (event) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
is MainEvent.OnChangeLanguage -> {
|
when (event) {
|
||||||
viewModelScope.launch(Dispatchers.Main) {
|
is MainEvent.OnChangeLanguage -> handleLanguageUpdate(event.value)
|
||||||
changeLanguage.execute(event.lang)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
language = event.lang
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeDarkTheme -> {
|
is MainEvent.OnChangeDarkTheme -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.DARK_THEME,
|
||||||
setDatastore.execute(DataStoreConstants.DARK_THEME, event.darkTheme)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(darkTheme = toDarkTheme())
|
||||||
darkTheme = event.darkTheme.toDarkTheme()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangePureDark -> {
|
is MainEvent.OnChangePureDark -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.PURE_DARK,
|
||||||
setDatastore.execute(DataStoreConstants.PURE_DARK, event.pureDark)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(pureDark = toPureDark())
|
||||||
pureDark = event.pureDark.toPureDark()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeThemeContrast -> {
|
is MainEvent.OnChangeThemeContrast -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.THEME_CONTRAST,
|
||||||
setDatastore.execute(DataStoreConstants.THEME_CONTRAST, event.themeContrast)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(themeContrast = toThemeContrast())
|
||||||
themeContrast = event.themeContrast.toThemeContrast()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeTheme -> {
|
is MainEvent.OnChangeTheme -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.THEME,
|
||||||
setDatastore.execute(DataStoreConstants.THEME, event.theme)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(theme = toTheme())
|
||||||
theme = event.theme.toTheme()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeFontFamily -> {
|
is MainEvent.OnChangeFontFamily -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.FONT,
|
||||||
setDatastore.execute(DataStoreConstants.FONT, event.fontFamily)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(
|
||||||
fontFamily = Constants.FONTS.find { font -> font.id == event.fontFamily }?.id
|
fontFamily = Constants.FONTS.find { font -> font.id == event.value }?.id
|
||||||
?: Constants.FONTS[0].id
|
?: Constants.FONTS[0].id
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeFontStyle -> {
|
is MainEvent.OnChangeFontStyle -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.IS_ITALIC,
|
||||||
setDatastore.execute(DataStoreConstants.IS_ITALIC, event.fontStyle)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(isItalic = this)
|
||||||
isItalic = event.fontStyle
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeFontSize -> {
|
is MainEvent.OnChangeFontSize -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.FONT_SIZE,
|
||||||
setDatastore.execute(DataStoreConstants.FONT_SIZE, event.fontSize)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(fontSize = this)
|
||||||
fontSize = event.fontSize
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeLineHeight -> {
|
is MainEvent.OnChangeLineHeight -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.LINE_HEIGHT,
|
||||||
setDatastore.execute(DataStoreConstants.LINE_HEIGHT, event.lineHeight)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(lineHeight = this)
|
||||||
lineHeight = event.lineHeight
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeParagraphHeight -> {
|
is MainEvent.OnChangeParagraphHeight -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.PARAGRAPH_HEIGHT,
|
||||||
setDatastore.execute(DataStoreConstants.PARAGRAPH_HEIGHT, event.paragraphHeight)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(paragraphHeight = this)
|
||||||
paragraphHeight = event.paragraphHeight
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeParagraphIndentation -> {
|
is MainEvent.OnChangeParagraphIndentation -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.PARAGRAPH_INDENTATION,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.PARAGRAPH_INDENTATION,
|
updateState = {
|
||||||
event.indentation
|
it.copy(paragraphIndentation = this)
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
paragraphIndentation = event.indentation
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeShowStartScreen -> {
|
is MainEvent.OnChangeShowStartScreen -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.SHOW_START_SCREEN,
|
||||||
setDatastore.execute(DataStoreConstants.SHOW_START_SCREEN, event.bool)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(showStartScreen = this)
|
||||||
showStartScreen = event.bool
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeCheckForUpdates -> {
|
is MainEvent.OnChangeCheckForUpdates -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.CHECK_FOR_UPDATES,
|
||||||
setDatastore.execute(DataStoreConstants.CHECK_FOR_UPDATES, event.bool)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(checkForUpdates = this)
|
||||||
checkForUpdates = event.bool
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeSidePadding -> {
|
is MainEvent.OnChangeSidePadding -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.SIDE_PADDING,
|
||||||
setDatastore.execute(DataStoreConstants.SIDE_PADDING, event.sidePadding)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(sidePadding = this)
|
||||||
sidePadding = event.sidePadding
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeDoubleClickTranslation -> {
|
is MainEvent.OnChangeDoubleClickTranslation -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.DOUBLE_CLICK_TRANSLATION,
|
||||||
setDatastore.execute(DataStoreConstants.DOUBLE_CLICK_TRANSLATION, event.bool)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(doubleClickTranslation = this)
|
||||||
doubleClickTranslation = event.bool
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeFastColorPresetChange -> {
|
is MainEvent.OnChangeFastColorPresetChange -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.FAST_COLOR_PRESET_CHANGE,
|
||||||
setDatastore.execute(DataStoreConstants.FAST_COLOR_PRESET_CHANGE, event.bool)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(fastColorPresetChange = this)
|
||||||
fastColorPresetChange = event.bool
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeBrowseFilesStructure -> {
|
is MainEvent.OnChangeBrowseFilesStructure -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.BROWSE_FILES_STRUCTURE,
|
||||||
setDatastore.execute(DataStoreConstants.BROWSE_FILES_STRUCTURE, event.structure)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(browseFilesStructure = toBrowseFilesStructure())
|
||||||
browseFilesStructure = event.structure.toFilesStructure()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeBrowseLayout -> {
|
is MainEvent.OnChangeBrowseLayout -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.BROWSE_LAYOUT,
|
||||||
setDatastore.execute(DataStoreConstants.BROWSE_LAYOUT, event.layout)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(browseLayout = toBrowseLayout())
|
||||||
browseLayout = event.layout.toBrowseLayout()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeBrowseAutoGridSize -> {
|
is MainEvent.OnChangeBrowseAutoGridSize -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.BROWSE_AUTO_GRID_SIZE,
|
||||||
setDatastore.execute(DataStoreConstants.BROWSE_AUTO_GRID_SIZE, event.bool)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(browseAutoGridSize = this)
|
||||||
browseAutoGridSize = event.bool
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeBrowseGridSize -> {
|
is MainEvent.OnChangeBrowseGridSize -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.BROWSE_GRID_SIZE,
|
||||||
setDatastore.execute(DataStoreConstants.BROWSE_GRID_SIZE, event.size)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(browseGridSize = this)
|
||||||
browseGridSize = event.size
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeBrowsePinFavoriteDirectories -> {
|
is MainEvent.OnChangeBrowsePinFavoriteDirectories -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.BROWSE_PIN_FAVORITE_DIRECTORIES,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.BROWSE_PIN_FAVORITE_DIRECTORIES,
|
updateState = {
|
||||||
event.bool
|
it.copy(browsePinFavoriteDirectories = this)
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
browsePinFavoriteDirectories = event.bool
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeBrowseSortOrder -> {
|
is MainEvent.OnChangeBrowseSortOrder -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.BROWSE_SORT_ORDER,
|
||||||
setDatastore.execute(DataStoreConstants.BROWSE_SORT_ORDER, event.order)
|
value = event.value,
|
||||||
updateStateWithSavedHandle {
|
updateState = {
|
||||||
it.copy(
|
it.copy(browseSortOrder = toBrowseSortOrder())
|
||||||
browseSortOrder = event.order.toBrowseSortOrder()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeBrowseSortOrderDescending -> {
|
is MainEvent.OnChangeBrowseSortOrderDescending -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.BROWSE_SORT_ORDER_DESCENDING,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.BROWSE_SORT_ORDER_DESCENDING,
|
updateState = {
|
||||||
event.bool
|
it.copy(browseSortOrderDescending = this)
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
browseSortOrderDescending = event.bool
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeBrowseIncludedFilterItem -> {
|
is MainEvent.OnChangeBrowseIncludedFilterItem -> handleBrowseIncludedFilterItemUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
event.value
|
||||||
val set = _state.value.browseIncludedFilterItems.toMutableSet()
|
)
|
||||||
if (!set.add(event.item)) {
|
|
||||||
set.remove(event.item)
|
|
||||||
}
|
|
||||||
|
|
||||||
setDatastore.execute(DataStoreConstants.BROWSE_INCLUDED_FILTER_ITEMS, set)
|
is MainEvent.OnChangeTextAlignment -> handleDatastoreUpdate(
|
||||||
updateStateWithSavedHandle {
|
key = DataStoreConstants.TEXT_ALIGNMENT,
|
||||||
it.copy(
|
value = event.value,
|
||||||
browseIncludedFilterItems = set.toList()
|
updateState = {
|
||||||
)
|
it.copy(textAlignment = toTextAlignment())
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeTextAlignment -> {
|
is MainEvent.OnChangeDoublePressExit -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.DOUBLE_PRESS_EXIT,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.TEXT_ALIGNMENT,
|
updateState = {
|
||||||
event.alignment
|
it.copy(doublePressExit = this)
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
textAlignment = event.alignment.toTextAlignment()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeDoublePressExit -> {
|
is MainEvent.OnChangeLetterSpacing -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.LETTER_SPACING,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.DOUBLE_PRESS_EXIT,
|
updateState = {
|
||||||
event.bool
|
it.copy(letterSpacing = this)
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
doublePressExit = event.bool
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeLetterSpacing -> {
|
is MainEvent.OnChangeAbsoluteDark -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.ABSOLUTE_DARK,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.LETTER_SPACING,
|
updateState = {
|
||||||
event.spacing
|
it.copy(absoluteDark = this)
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
letterSpacing = event.spacing
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeAbsoluteDark -> {
|
is MainEvent.OnChangeCutoutPadding -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.CUTOUT_PADDING,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.ABSOLUTE_DARK,
|
updateState = {
|
||||||
event.bool
|
it.copy(cutoutPadding = this)
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
absoluteDark = event.bool
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeCutoutPadding -> {
|
is MainEvent.OnChangeFullscreen -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.FULLSCREEN,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.CUTOUT_PADDING,
|
updateState = {
|
||||||
event.bool
|
it.copy(fullscreen = this)
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
cutoutPadding = event.bool
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeFullscreen -> {
|
is MainEvent.OnChangeKeepScreenOn -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.KEEP_SCREEN_ON,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.FULLSCREEN,
|
updateState = {
|
||||||
event.bool
|
it.copy(keepScreenOn = this)
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
fullscreen = event.bool
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeKeepScreenOn -> {
|
is MainEvent.OnChangeVerticalPadding -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.VERTICAL_PADDING,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.KEEP_SCREEN_ON,
|
updateState = {
|
||||||
event.bool
|
it.copy(verticalPadding = this)
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
keepScreenOn = event.bool
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeVerticalPadding -> {
|
is MainEvent.OnChangeHideBarsOnFastScroll -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.HIDE_BARS_ON_FAST_SCROLL,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.VERTICAL_PADDING,
|
updateState = {
|
||||||
event.padding
|
it.copy(hideBarsOnFastScroll = this)
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
verticalPadding = event.padding
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeHideBarsOnFastScroll -> {
|
is MainEvent.OnChangePerceptionExpander -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.PERCEPTION_EXPANDER,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.HIDE_BARS_ON_FAST_SCROLL,
|
updateState = {
|
||||||
event.bool
|
it.copy(perceptionExpander = this)
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
hideBarsOnFastScroll = event.bool
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangePerceptionExpander -> {
|
is MainEvent.OnChangePerceptionExpanderPadding -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.PERCEPTION_EXPANDER_PADDING,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.PERCEPTION_EXPANDER,
|
updateState = {
|
||||||
event.bool
|
it.copy(perceptionExpanderPadding = this)
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
perceptionExpander = event.bool
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangePerceptionExpanderPadding -> {
|
is MainEvent.OnChangePerceptionExpanderThickness -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.PERCEPTION_EXPANDER_THICKNESS,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.PERCEPTION_EXPANDER_PADDING,
|
updateState = {
|
||||||
event.padding
|
it.copy(perceptionExpanderThickness = this)
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
perceptionExpanderPadding = event.padding
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangePerceptionExpanderThickness -> {
|
is MainEvent.OnChangeCheckForTextUpdate -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.CHECK_FOR_TEXT_UPDATE,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.PERCEPTION_EXPANDER_THICKNESS,
|
updateState = {
|
||||||
event.thickness
|
it.copy(checkForTextUpdate = this)
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
perceptionExpanderThickness = event.thickness
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeCheckForTextUpdate -> {
|
is MainEvent.OnChangeCheckForTextUpdateToast -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.CHECK_FOR_TEXT_UPDATE_TOAST,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.CHECK_FOR_TEXT_UPDATE,
|
updateState = {
|
||||||
event.bool
|
it.copy(checkForTextUpdateToast = this)
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
checkForTextUpdate = event.bool
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeCheckForTextUpdateToast -> {
|
is MainEvent.OnChangeScreenOrientation -> handleDatastoreUpdate(
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
key = DataStoreConstants.SCREEN_ORIENTATION,
|
||||||
setDatastore.execute(
|
value = event.value,
|
||||||
DataStoreConstants.CHECK_FOR_TEXT_UPDATE_TOAST,
|
updateState = {
|
||||||
event.bool
|
it.copy(screenOrientation = toReaderScreenOrientation())
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
checkForTextUpdateToast = event.bool
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
|
||||||
is MainEvent.OnChangeScreenOrientation -> {
|
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
|
||||||
setDatastore.execute(
|
|
||||||
DataStoreConstants.SCREEN_ORIENTATION,
|
|
||||||
event.orientation
|
|
||||||
)
|
|
||||||
updateStateWithSavedHandle {
|
|
||||||
it.copy(
|
|
||||||
screenOrientation = event.orientation.toReaderScreenOrientation()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -565,6 +383,7 @@ class MainViewModel @Inject constructor(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* - - - - - - - - - - - */
|
||||||
|
|
||||||
updateStateWithSavedHandle { settings }
|
updateStateWithSavedHandle { settings }
|
||||||
isSettingsReady.update { true }
|
isSettingsReady.update { true }
|
||||||
|
|
@ -602,6 +421,29 @@ class MainViewModel @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private suspend fun handleLanguageUpdate(value: String) {
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
|
changeLanguage.execute(value)
|
||||||
|
updateStateWithSavedHandle {
|
||||||
|
it.copy(language = value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun handleBrowseIncludedFilterItemUpdate(value: String) {
|
||||||
|
val set = _state.value.browseIncludedFilterItems.toMutableSet()
|
||||||
|
if (!set.add(value)) {
|
||||||
|
set.remove(value)
|
||||||
|
}
|
||||||
|
handleDatastoreUpdate(
|
||||||
|
key = DataStoreConstants.BROWSE_INCLUDED_FILTER_ITEMS,
|
||||||
|
value = set,
|
||||||
|
updateState = {
|
||||||
|
it.copy(browseIncludedFilterItems = toList())
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates [MainState] along with [SavedStateHandle].
|
* Updates [MainState] along with [SavedStateHandle].
|
||||||
*/
|
*/
|
||||||
|
|
@ -613,4 +455,20 @@ class MainViewModel @Inject constructor(
|
||||||
function(it)
|
function(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles and updates Datastore.
|
||||||
|
*/
|
||||||
|
private suspend fun <V> handleDatastoreUpdate(
|
||||||
|
key: Preferences.Key<V>,
|
||||||
|
value: V,
|
||||||
|
updateState: V.(MainState) -> MainState
|
||||||
|
) {
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
setDatastore.execute(key = key, value = value)
|
||||||
|
updateStateWithSavedHandle {
|
||||||
|
value.updateState(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -102,7 +102,7 @@ private fun BookInfoScreen(snackbarState: SnackbarHostState) {
|
||||||
refreshing = state.value.updating || state.value.checkingForUpdate,
|
refreshing = state.value.updating || state.value.checkingForUpdate,
|
||||||
onRefresh = {
|
onRefresh = {
|
||||||
onEvent(
|
onEvent(
|
||||||
BookInfoEvent.OnCheckForUpdate(
|
BookInfoEvent.OnCheckForTextUpdate(
|
||||||
snackbarState,
|
snackbarState,
|
||||||
context
|
context
|
||||||
)
|
)
|
||||||
|
|
@ -122,7 +122,7 @@ private fun BookInfoScreen(snackbarState: SnackbarHostState) {
|
||||||
if (state.value.showMoveDialog) {
|
if (state.value.showMoveDialog) {
|
||||||
BookInfoMoveDialog()
|
BookInfoMoveDialog()
|
||||||
}
|
}
|
||||||
if (state.value.showConfirmUpdateDialog) {
|
if (state.value.showConfirmTextUpdateDialog) {
|
||||||
BookInfoConfirmUpdateDialog(snackbarHostState = snackbarState)
|
BookInfoConfirmUpdateDialog(snackbarHostState = snackbarState)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -261,7 +261,7 @@ private fun BookInfoScreen(snackbarState: SnackbarHostState) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!state.value.updating) {
|
if (!state.value.updating) {
|
||||||
onEvent(BookInfoEvent.OnCancelUpdate)
|
onEvent(BookInfoEvent.OnCancelTextUpdate)
|
||||||
onNavigate {
|
onNavigate {
|
||||||
navigateBack()
|
navigateBack()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@ fun BookInfoTopBar(
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
GoBackButton(onNavigate = onNavigate, enabled = !state.value.updating) {
|
GoBackButton(onNavigate = onNavigate, enabled = !state.value.updating) {
|
||||||
onEvent(BookInfoEvent.OnCancelUpdate)
|
onEvent(BookInfoEvent.OnCancelTextUpdate)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -114,7 +114,7 @@ fun BookInfoTopBar(
|
||||||
enabled = !state.value.updating && !state.value.checkingForUpdate
|
enabled = !state.value.updating && !state.value.checkingForUpdate
|
||||||
) {
|
) {
|
||||||
onEvent(
|
onEvent(
|
||||||
BookInfoEvent.OnCheckForUpdate(
|
BookInfoEvent.OnCheckForTextUpdate(
|
||||||
snackbarState,
|
snackbarState,
|
||||||
context
|
context
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -36,10 +36,10 @@ fun BookInfoConfirmUpdateDialog(snackbarHostState: SnackbarHostState) {
|
||||||
),
|
),
|
||||||
actionText = stringResource(id = R.string.confirm),
|
actionText = stringResource(id = R.string.confirm),
|
||||||
isActionEnabled = true,
|
isActionEnabled = true,
|
||||||
onDismiss = { onEvent(BookInfoEvent.OnDismissConfirmUpdateDialog) },
|
onDismiss = { onEvent(BookInfoEvent.OnDismissConfirmTextUpdateDialog) },
|
||||||
onAction = {
|
onAction = {
|
||||||
onEvent(
|
onEvent(
|
||||||
BookInfoEvent.OnConfirmUpdate(
|
BookInfoEvent.OnConfirmTextUpdate(
|
||||||
snackbarState = snackbarHostState,
|
snackbarState = snackbarHostState,
|
||||||
context = context,
|
context = context,
|
||||||
refreshList = {
|
refreshList = {
|
||||||
|
|
|
||||||
|
|
@ -101,25 +101,25 @@ sealed class BookInfoEvent {
|
||||||
val snackbarState: SnackbarHostState
|
val snackbarState: SnackbarHostState
|
||||||
) : BookInfoEvent()
|
) : BookInfoEvent()
|
||||||
|
|
||||||
data class OnCheckForUpdate(
|
|
||||||
val snackbarState: SnackbarHostState,
|
|
||||||
val context: Context
|
|
||||||
) : BookInfoEvent()
|
|
||||||
|
|
||||||
data class OnNavigateToReaderScreen(
|
data class OnNavigateToReaderScreen(
|
||||||
val onNavigate: OnNavigate
|
val onNavigate: OnNavigate
|
||||||
) : BookInfoEvent()
|
) : BookInfoEvent()
|
||||||
|
|
||||||
data object OnDismissConfirmUpdateDialog : BookInfoEvent()
|
data class OnCheckForTextUpdate(
|
||||||
|
val snackbarState: SnackbarHostState,
|
||||||
|
val context: Context
|
||||||
|
) : BookInfoEvent()
|
||||||
|
|
||||||
data class OnShowConfirmUpdateDialog(
|
data object OnDismissConfirmTextUpdateDialog : BookInfoEvent()
|
||||||
|
|
||||||
|
data class OnShowConfirmTextUpdateDialog(
|
||||||
val updatedText: List<String>,
|
val updatedText: List<String>,
|
||||||
val updatedChapters: List<Chapter>
|
val updatedChapters: List<Chapter>
|
||||||
) : BookInfoEvent()
|
) : BookInfoEvent()
|
||||||
|
|
||||||
data object OnCancelUpdate : BookInfoEvent()
|
data object OnCancelTextUpdate : BookInfoEvent()
|
||||||
|
|
||||||
data class OnConfirmUpdate(
|
data class OnConfirmTextUpdate(
|
||||||
val snackbarState: SnackbarHostState,
|
val snackbarState: SnackbarHostState,
|
||||||
val context: Context,
|
val context: Context,
|
||||||
val refreshList: (Book) -> Unit
|
val refreshList: (Book) -> Unit
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ data class BookInfoState(
|
||||||
val checkingForUpdate: Boolean = false,
|
val checkingForUpdate: Boolean = false,
|
||||||
val updating: Boolean = false,
|
val updating: Boolean = false,
|
||||||
|
|
||||||
val showConfirmUpdateDialog: Boolean = false,
|
val showConfirmTextUpdateDialog: Boolean = false,
|
||||||
val updatedText: List<String>? = null,
|
val updatedText: List<String>? = null,
|
||||||
val updatedChapters: List<Chapter>? = null,
|
val updatedChapters: List<Chapter>? = null,
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ import ua.acclorite.book_story.domain.model.BookWithText
|
||||||
import ua.acclorite.book_story.domain.model.Category
|
import ua.acclorite.book_story.domain.model.Category
|
||||||
import ua.acclorite.book_story.domain.model.History
|
import ua.acclorite.book_story.domain.model.History
|
||||||
import ua.acclorite.book_story.domain.use_case.CanResetCover
|
import ua.acclorite.book_story.domain.use_case.CanResetCover
|
||||||
import ua.acclorite.book_story.domain.use_case.CheckTextForUpdate
|
import ua.acclorite.book_story.domain.use_case.CheckForTextUpdate
|
||||||
import ua.acclorite.book_story.domain.use_case.DeleteBooks
|
import ua.acclorite.book_story.domain.use_case.DeleteBooks
|
||||||
import ua.acclorite.book_story.domain.use_case.GetBookById
|
import ua.acclorite.book_story.domain.use_case.GetBookById
|
||||||
import ua.acclorite.book_story.domain.use_case.InsertHistory
|
import ua.acclorite.book_story.domain.use_case.InsertHistory
|
||||||
|
|
@ -54,7 +54,7 @@ class BookInfoViewModel @Inject constructor(
|
||||||
private val getBookById: GetBookById,
|
private val getBookById: GetBookById,
|
||||||
private val canResetCover: CanResetCover,
|
private val canResetCover: CanResetCover,
|
||||||
private val resetCoverImage: ResetCoverImage,
|
private val resetCoverImage: ResetCoverImage,
|
||||||
private val checkTextForUpdate: CheckTextForUpdate,
|
private val checkForTextUpdate: CheckForTextUpdate,
|
||||||
) : BaseViewModel<BookInfoState, BookInfoEvent>() {
|
) : BaseViewModel<BookInfoState, BookInfoEvent>() {
|
||||||
|
|
||||||
private val _state = MutableStateFlow(BookInfoState())
|
private val _state = MutableStateFlow(BookInfoState())
|
||||||
|
|
@ -450,12 +450,12 @@ class BookInfoViewModel @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is BookInfoEvent.OnCheckForUpdate -> {
|
is BookInfoEvent.OnCheckForTextUpdate -> {
|
||||||
updateJob?.cancel()
|
updateJob?.cancel()
|
||||||
updateJob = launch(Dispatchers.IO) {
|
updateJob = launch(Dispatchers.IO) {
|
||||||
_state.update {
|
_state.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
showConfirmUpdateDialog = false,
|
showConfirmTextUpdateDialog = false,
|
||||||
checkingForUpdate = true,
|
checkingForUpdate = true,
|
||||||
editTitle = false,
|
editTitle = false,
|
||||||
editAuthor = false,
|
editAuthor = false,
|
||||||
|
|
@ -465,7 +465,7 @@ class BookInfoViewModel @Inject constructor(
|
||||||
|
|
||||||
yield()
|
yield()
|
||||||
|
|
||||||
val result = checkTextForUpdate.execute(bookId = _state.value.book.id)
|
val result = checkForTextUpdate.execute(bookId = _state.value.book.id)
|
||||||
when (result) {
|
when (result) {
|
||||||
is Resource.Success -> {
|
is Resource.Success -> {
|
||||||
if (result.data == null) {
|
if (result.data == null) {
|
||||||
|
|
@ -486,7 +486,7 @@ class BookInfoViewModel @Inject constructor(
|
||||||
return@launch
|
return@launch
|
||||||
} else {
|
} else {
|
||||||
onEvent(
|
onEvent(
|
||||||
BookInfoEvent.OnShowConfirmUpdateDialog(
|
BookInfoEvent.OnShowConfirmTextUpdateDialog(
|
||||||
updatedText = result.data.first,
|
updatedText = result.data.first,
|
||||||
updatedChapters = result.data.second
|
updatedChapters = result.data.second
|
||||||
)
|
)
|
||||||
|
|
@ -508,7 +508,7 @@ class BookInfoViewModel @Inject constructor(
|
||||||
action = event.context.getString(R.string.retry),
|
action = event.context.getString(R.string.retry),
|
||||||
onAction = {
|
onAction = {
|
||||||
onEvent(
|
onEvent(
|
||||||
BookInfoEvent.OnCheckForUpdate(
|
BookInfoEvent.OnCheckForTextUpdate(
|
||||||
snackbarState = event.snackbarState,
|
snackbarState = event.snackbarState,
|
||||||
context = event.context
|
context = event.context
|
||||||
)
|
)
|
||||||
|
|
@ -531,32 +531,32 @@ class BookInfoViewModel @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is BookInfoEvent.OnDismissConfirmUpdateDialog -> {
|
is BookInfoEvent.OnDismissConfirmTextUpdateDialog -> {
|
||||||
_state.update {
|
_state.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
showConfirmUpdateDialog = false,
|
showConfirmTextUpdateDialog = false,
|
||||||
updatedText = null,
|
updatedText = null,
|
||||||
updatedChapters = null
|
updatedChapters = null
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is BookInfoEvent.OnShowConfirmUpdateDialog -> {
|
is BookInfoEvent.OnShowConfirmTextUpdateDialog -> {
|
||||||
_state.update {
|
_state.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
showConfirmUpdateDialog = true,
|
showConfirmTextUpdateDialog = true,
|
||||||
updatedText = event.updatedText,
|
updatedText = event.updatedText,
|
||||||
updatedChapters = event.updatedChapters
|
updatedChapters = event.updatedChapters
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is BookInfoEvent.OnConfirmUpdate -> {
|
is BookInfoEvent.OnConfirmTextUpdate -> {
|
||||||
launch(Dispatchers.IO) {
|
launch(Dispatchers.IO) {
|
||||||
_state.update {
|
_state.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
updating = true,
|
updating = true,
|
||||||
showConfirmUpdateDialog = false
|
showConfirmTextUpdateDialog = false
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -569,7 +569,7 @@ class BookInfoViewModel @Inject constructor(
|
||||||
action = event.context.getString(R.string.retry),
|
action = event.context.getString(R.string.retry),
|
||||||
onAction = {
|
onAction = {
|
||||||
onEvent(
|
onEvent(
|
||||||
BookInfoEvent.OnCheckForUpdate(
|
BookInfoEvent.OnCheckForTextUpdate(
|
||||||
snackbarState = event.snackbarState,
|
snackbarState = event.snackbarState,
|
||||||
context = event.context
|
context = event.context
|
||||||
)
|
)
|
||||||
|
|
@ -614,7 +614,7 @@ class BookInfoViewModel @Inject constructor(
|
||||||
action = event.context.getString(R.string.retry),
|
action = event.context.getString(R.string.retry),
|
||||||
onAction = {
|
onAction = {
|
||||||
onEvent(
|
onEvent(
|
||||||
BookInfoEvent.OnCheckForUpdate(
|
BookInfoEvent.OnCheckForTextUpdate(
|
||||||
snackbarState = event.snackbarState,
|
snackbarState = event.snackbarState,
|
||||||
context = event.context
|
context = event.context
|
||||||
)
|
)
|
||||||
|
|
@ -646,7 +646,7 @@ class BookInfoViewModel @Inject constructor(
|
||||||
action = event.context.getString(R.string.retry),
|
action = event.context.getString(R.string.retry),
|
||||||
onAction = {
|
onAction = {
|
||||||
onEvent(
|
onEvent(
|
||||||
BookInfoEvent.OnCheckForUpdate(
|
BookInfoEvent.OnCheckForTextUpdate(
|
||||||
snackbarState = event.snackbarState,
|
snackbarState = event.snackbarState,
|
||||||
context = event.context
|
context = event.context
|
||||||
)
|
)
|
||||||
|
|
@ -696,12 +696,12 @@ class BookInfoViewModel @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is BookInfoEvent.OnCancelUpdate -> {
|
is BookInfoEvent.OnCancelTextUpdate -> {
|
||||||
_state.update {
|
_state.update {
|
||||||
updateJob?.cancel()
|
updateJob?.cancel()
|
||||||
|
|
||||||
it.copy(
|
it.copy(
|
||||||
showConfirmUpdateDialog = false,
|
showConfirmTextUpdateDialog = false,
|
||||||
checkingForUpdate = false,
|
checkingForUpdate = false,
|
||||||
updatedText = null,
|
updatedText = null,
|
||||||
updatedChapters = null
|
updatedChapters = null
|
||||||
|
|
@ -711,7 +711,7 @@ class BookInfoViewModel @Inject constructor(
|
||||||
|
|
||||||
is BookInfoEvent.OnNavigateToReaderScreen -> {
|
is BookInfoEvent.OnNavigateToReaderScreen -> {
|
||||||
launch {
|
launch {
|
||||||
onEvent(BookInfoEvent.OnCancelUpdate)
|
onEvent(BookInfoEvent.OnCancelTextUpdate)
|
||||||
_state.value.book.id.let {
|
_state.value.book.id.let {
|
||||||
insertHistory.execute(
|
insertHistory.execute(
|
||||||
History(
|
History(
|
||||||
|
|
@ -757,7 +757,7 @@ class BookInfoViewModel @Inject constructor(
|
||||||
|
|
||||||
if (screen.startUpdate) {
|
if (screen.startUpdate) {
|
||||||
onEvent(
|
onEvent(
|
||||||
BookInfoEvent.OnCheckForUpdate(
|
BookInfoEvent.OnCheckForTextUpdate(
|
||||||
snackbarState = snackbarState,
|
snackbarState = snackbarState,
|
||||||
context = context
|
context = context
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ import kotlinx.coroutines.withContext
|
||||||
import kotlinx.coroutines.yield
|
import kotlinx.coroutines.yield
|
||||||
import ua.acclorite.book_story.R
|
import ua.acclorite.book_story.R
|
||||||
import ua.acclorite.book_story.domain.model.Book
|
import ua.acclorite.book_story.domain.model.Book
|
||||||
import ua.acclorite.book_story.domain.use_case.CheckTextForUpdate
|
import ua.acclorite.book_story.domain.use_case.CheckForTextUpdate
|
||||||
import ua.acclorite.book_story.domain.use_case.GetBookById
|
import ua.acclorite.book_story.domain.use_case.GetBookById
|
||||||
import ua.acclorite.book_story.domain.use_case.GetLatestHistory
|
import ua.acclorite.book_story.domain.use_case.GetLatestHistory
|
||||||
import ua.acclorite.book_story.domain.use_case.GetText
|
import ua.acclorite.book_story.domain.use_case.GetText
|
||||||
|
|
@ -47,7 +47,7 @@ class ReaderViewModel @Inject constructor(
|
||||||
private val getText: GetText,
|
private val getText: GetText,
|
||||||
private val getLatestHistory: GetLatestHistory,
|
private val getLatestHistory: GetLatestHistory,
|
||||||
private val getBookById: GetBookById,
|
private val getBookById: GetBookById,
|
||||||
private val checkTextForUpdate: CheckTextForUpdate
|
private val checkForTextUpdate: CheckForTextUpdate
|
||||||
) : BaseViewModel<ReaderState, ReaderEvent>() {
|
) : BaseViewModel<ReaderState, ReaderEvent>() {
|
||||||
|
|
||||||
private val _state = MutableStateFlow(ReaderState())
|
private val _state = MutableStateFlow(ReaderState())
|
||||||
|
|
@ -420,7 +420,7 @@ class ReaderViewModel @Inject constructor(
|
||||||
|
|
||||||
yield()
|
yield()
|
||||||
|
|
||||||
val result = checkTextForUpdate.execute(bookId = _state.value.book.id)
|
val result = checkForTextUpdate.execute(bookId = _state.value.book.id)
|
||||||
when (result) {
|
when (result) {
|
||||||
is Resource.Success -> {
|
is Resource.Success -> {
|
||||||
if (result.data == null) {
|
if (result.data == null) {
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,6 @@ enum class BrowseFilesStructure {
|
||||||
ALL_FILES, DIRECTORIES
|
ALL_FILES, DIRECTORIES
|
||||||
}
|
}
|
||||||
|
|
||||||
fun String.toFilesStructure(): BrowseFilesStructure {
|
fun String.toBrowseFilesStructure(): BrowseFilesStructure {
|
||||||
return BrowseFilesStructure.valueOf(this)
|
return BrowseFilesStructure.valueOf(this)
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue