🛠️ Remove/Reformat code in Reader
* Removed and Reformatted code related to progress tracking and start/end items in Reader
This commit is contained in:
parent
3fa6e2a054
commit
83260d354d
3 changed files with 26 additions and 114 deletions
|
|
@ -4,7 +4,6 @@ import androidx.activity.ComponentActivity
|
||||||
import androidx.compose.foundation.pager.PagerState
|
import androidx.compose.foundation.pager.PagerState
|
||||||
import androidx.compose.runtime.Immutable
|
import androidx.compose.runtime.Immutable
|
||||||
import ua.acclorite.book_story.domain.model.Book
|
import ua.acclorite.book_story.domain.model.Book
|
||||||
import ua.acclorite.book_story.domain.util.OnNavigate
|
|
||||||
import ua.acclorite.book_story.domain.util.UIText
|
import ua.acclorite.book_story.domain.util.UIText
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
|
|
@ -37,13 +36,6 @@ sealed class ReaderEvent {
|
||||||
data class OnScroll(val progress: Float) : ReaderEvent()
|
data class OnScroll(val progress: Float) : ReaderEvent()
|
||||||
data object OnShowHideSettingsBottomSheet : ReaderEvent()
|
data object OnShowHideSettingsBottomSheet : ReaderEvent()
|
||||||
data class OnScrollToSettingsPage(val page: Int, val pagerState: PagerState?) : ReaderEvent()
|
data class OnScrollToSettingsPage(val page: Int, val pagerState: PagerState?) : ReaderEvent()
|
||||||
data class OnMoveBookToAlreadyRead(
|
|
||||||
val context: ComponentActivity,
|
|
||||||
val onUpdateCategories: (Book) -> Unit,
|
|
||||||
val updatePage: (Int) -> Unit,
|
|
||||||
val onNavigate: OnNavigate
|
|
||||||
) : ReaderEvent()
|
|
||||||
|
|
||||||
data class OnOpenTranslator(
|
data class OnOpenTranslator(
|
||||||
val textToTranslate: String,
|
val textToTranslate: String,
|
||||||
val translateWholeParagraph: Boolean,
|
val translateWholeParagraph: Boolean,
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,6 @@ import ua.acclorite.book_story.presentation.core.constants.Constants
|
||||||
data class ReaderState(
|
data class ReaderState(
|
||||||
val book: Book = Constants.EMPTY_BOOK,
|
val book: Book = Constants.EMPTY_BOOK,
|
||||||
val text: List<String> = emptyList(),
|
val text: List<String> = emptyList(),
|
||||||
val words: Int = 0,
|
|
||||||
val letters: Int = 0,
|
|
||||||
val listState: LazyListState = LazyListState(),
|
val listState: LazyListState = LazyListState(),
|
||||||
|
|
||||||
val errorMessage: UIText? = null,
|
val errorMessage: UIText? = null,
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,6 @@ import kotlinx.coroutines.launch
|
||||||
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.model.Category
|
|
||||||
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
|
||||||
|
|
@ -73,24 +72,9 @@ class ReaderViewModel @Inject constructor(
|
||||||
event.onTextIsEmpty()
|
event.onTextIsEmpty()
|
||||||
}
|
}
|
||||||
|
|
||||||
val textAsLine = text
|
|
||||||
.joinToString(
|
|
||||||
separator = "\n"
|
|
||||||
)
|
|
||||||
|
|
||||||
val letters = textAsLine
|
|
||||||
.replace("\n", "")
|
|
||||||
.length
|
|
||||||
val words = textAsLine
|
|
||||||
.replace("\n", " ")
|
|
||||||
.split("\\s+".toRegex())
|
|
||||||
.size
|
|
||||||
|
|
||||||
_state.update {
|
_state.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
text = text,
|
text = text
|
||||||
letters = letters,
|
|
||||||
words = words
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -285,46 +269,6 @@ class ReaderViewModel @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is ReaderEvent.OnMoveBookToAlreadyRead -> {
|
|
||||||
launch {
|
|
||||||
yield()
|
|
||||||
|
|
||||||
_state.update {
|
|
||||||
it.copy(
|
|
||||||
lockMenu = true,
|
|
||||||
book = it.book.copy(
|
|
||||||
category = Category.ALREADY_READ,
|
|
||||||
progress = 1f,
|
|
||||||
scrollIndex = it.listState.firstVisibleItemIndex,
|
|
||||||
scrollOffset = it.listState.firstVisibleItemScrollOffset
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
updateBooks.execute(listOf(_state.value.book))
|
|
||||||
event.onUpdateCategories(
|
|
||||||
_state.value.book
|
|
||||||
)
|
|
||||||
event.updatePage(
|
|
||||||
Category.entries.dropLastWhile {
|
|
||||||
it != Category.ALREADY_READ
|
|
||||||
}.size - 1
|
|
||||||
)
|
|
||||||
|
|
||||||
WindowCompat.getInsetsController(
|
|
||||||
event.context.window,
|
|
||||||
event.context.window.decorView
|
|
||||||
).show(WindowInsetsCompat.Type.systemBars())
|
|
||||||
|
|
||||||
event.onNavigate {
|
|
||||||
navigate(
|
|
||||||
Screen.Library,
|
|
||||||
useBackAnimation = true
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is ReaderEvent.OnOpenTranslator -> {
|
is ReaderEvent.OnOpenTranslator -> {
|
||||||
launch {
|
launch {
|
||||||
val translatorIntent = Intent()
|
val translatorIntent = Intent()
|
||||||
|
|
@ -508,35 +452,15 @@ class ReaderViewModel @Inject constructor(
|
||||||
}
|
}
|
||||||
.distinctUntilChanged()
|
.distinctUntilChanged()
|
||||||
.debounce(300)
|
.debounce(300)
|
||||||
.collectLatest { (firstVisibleItemIndex, firstVisibleItemScrollOffset) ->
|
.collectLatest { (firstVisibleItemIndex, firstVisibleItemOffset) ->
|
||||||
val listState = _state.value.listState
|
calculateProgress(firstVisibleItemIndex).apply {
|
||||||
if (
|
if (this == _state.value.book.progress) return@apply
|
||||||
!_state.value.loading &&
|
|
||||||
_state.value.text.isNotEmpty() &&
|
|
||||||
listState.layoutInfo.totalItemsCount > 0
|
|
||||||
) {
|
|
||||||
val lastVisibleItemIndex = listState
|
|
||||||
.layoutInfo
|
|
||||||
.visibleItemsInfo
|
|
||||||
.last()
|
|
||||||
.index
|
|
||||||
val totalItemsCount = listState.layoutInfo.totalItemsCount - 1
|
|
||||||
|
|
||||||
val progress = if (firstVisibleItemIndex > 0) {
|
|
||||||
if (lastVisibleItemIndex >= totalItemsCount) {
|
|
||||||
1f
|
|
||||||
} else {
|
|
||||||
firstVisibleItemIndex / (_state.value.text.size - 1).toFloat()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
0f
|
|
||||||
}
|
|
||||||
|
|
||||||
onEvent(
|
onEvent(
|
||||||
ReaderEvent.OnChangeProgress(
|
ReaderEvent.OnChangeProgress(
|
||||||
progress = progress,
|
progress = this,
|
||||||
firstVisibleItemIndex = firstVisibleItemIndex,
|
firstVisibleItemIndex = firstVisibleItemIndex,
|
||||||
firstVisibleItemOffset = firstVisibleItemScrollOffset,
|
firstVisibleItemOffset = firstVisibleItemOffset,
|
||||||
refreshList = { book ->
|
refreshList = { book ->
|
||||||
refreshList(book)
|
refreshList(book)
|
||||||
}
|
}
|
||||||
|
|
@ -547,30 +471,28 @@ class ReaderViewModel @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun calculateProgress(): Float {
|
private fun calculateProgress(firstVisibleItemIndex: Int? = null): Float {
|
||||||
val listState = _state.value.listState
|
return _state.value.run {
|
||||||
|
|
||||||
if (
|
if (
|
||||||
_state.value.loading ||
|
loading ||
|
||||||
listState.layoutInfo.totalItemsCount == 0 ||
|
listState.layoutInfo.totalItemsCount == 0 ||
|
||||||
_state.value.text.isEmpty()
|
text.isEmpty()
|
||||||
) {
|
) {
|
||||||
return _state.value.book.progress
|
return book.progress
|
||||||
}
|
}
|
||||||
|
|
||||||
val lastVisibleItemIndex = listState.layoutInfo
|
if ((firstVisibleItemIndex ?: listState.firstVisibleItemIndex) == 0) {
|
||||||
.visibleItemsInfo.last().index
|
|
||||||
val totalItemsCount = listState.layoutInfo.totalItemsCount - 1
|
|
||||||
|
|
||||||
if (listState.firstVisibleItemIndex == 0) {
|
|
||||||
return 0f
|
return 0f
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lastVisibleItemIndex >= totalItemsCount) {
|
val lastVisibleItemIndex = listState.layoutInfo.visibleItemsInfo.last().index
|
||||||
|
if (lastVisibleItemIndex >= text.lastIndex) {
|
||||||
return 1f
|
return 1f
|
||||||
}
|
}
|
||||||
|
|
||||||
return listState.firstVisibleItemIndex / (_state.value.text.size - 1).toFloat()
|
return@run (firstVisibleItemIndex ?: listState.firstVisibleItemIndex) /
|
||||||
|
(text.lastIndex).toFloat()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun clear() {
|
private suspend fun clear() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue