Skip to content

Commit

Permalink
improve state management logic in reader detail screen
Browse files Browse the repository at this point in the history
Signed-off-by: starry-shivam <[email protected]>
  • Loading branch information
starry-shivam committed Sep 27, 2024
1 parent 8ef752c commit de6728c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ fun BookDetailTopUI(
color = MaterialTheme.colorScheme.onBackground,
)

progressPercent?.let {
if (!progressPercent.isNullOrBlank()) {
Text(
text = "$progressPercent% Completed",
modifier = Modifier.padding(start = 12.dp, end = 8.dp),
Expand All @@ -167,7 +167,6 @@ fun BookDetailTopUI(
color = MaterialTheme.colorScheme.onBackground,
)
}

Spacer(modifier = Modifier.height(50.dp))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,14 @@ private fun ReaderDetailScaffold(
.background(MaterialTheme.colorScheme.background)
.padding(it)
) {
// Get the cover image data.
val imageData =
state.ebookData!!.coverImage ?: state.ebookData.epubBook.coverImage

BookDetailTopUI(
title = state.ebookData.title,
authors = state.ebookData.authors,
imageData = imageData,
title = state.title,
authors = state.authors,
imageData = state.coverImage,
currentThemeMode = settingsVM.getCurrentTheme(),
showReaderBackground = true,
progressPercent = progressData?.getProgressPercent(state.ebookData.epubBook.chapters.size),
progressPercent = if (state.hasProgressSaved)
progressData?.getProgressPercent(state.chapters.size) else ""
)

HorizontalDivider(
Expand All @@ -181,8 +178,8 @@ private fun ReaderDetailScaffold(
lazyListState, color = MaterialTheme.colorScheme.primary
)
) {
items(state.ebookData.epubBook.chapters.size) { idx ->
val chapter = state.ebookData.epubBook.chapters[idx]
items(state.chapters.size) { idx ->
val chapter = state.chapters[idx]
ChapterItem(chapterTitle = chapter.title, onClick = {
val intent = Intent(context, ReaderActivity::class.java)
intent.putExtra(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import com.starry.myne.database.library.LibraryDao
import com.starry.myne.database.progress.ProgressDao
import com.starry.myne.database.progress.ProgressData
import com.starry.myne.epub.EpubParser
import com.starry.myne.epub.models.EpubBook
import com.starry.myne.epub.models.EpubChapter
import com.starry.myne.helpers.NetworkObserver
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
Expand All @@ -38,16 +38,13 @@ import kotlinx.coroutines.launch
import javax.inject.Inject


data class EbookData(
val coverImage: String?,
val title: String,
val authors: String,
val epubBook: EpubBook,
)

data class ReaderDetailScreenState(
val isLoading: Boolean = true,
val ebookData: EbookData? = null,
val title: String = "",
val authors: String = "",
val coverImage: Any? = null,
val chapters: List<EpubChapter> = emptyList(),
val hasProgressSaved: Boolean = false,
val error: String? = null,
)

Expand All @@ -72,13 +69,15 @@ class ReaderDetailViewModel @Inject constructor(
state = state.copy(isLoading = false, error = "Library item not found.")
return@launch
}
// Get reader data if it exists.
// Get progress data for the current book.
progressData = progressDao.getReaderDataAsFlow(libraryItemId.toInt())
// Fetch cover image from google books api if available.
val coverImage: String? = try {
if (!libraryItem.isExternalBook
&& networkStatus == NetworkObserver.Status.Available
) bookAPI.getExtraInfo(libraryItem.title)?.coverImage else null
} catch (exc: Exception) {
Log.e("ReaderDetailViewModel", "Failed to fetch cover image.", exc)
null
}
// Gutenberg for some reason don't include proper navMap in chinese books
Expand All @@ -89,15 +88,15 @@ class ReaderDetailViewModel @Inject constructor(
Log.d("ReaderDetailViewModel", "Parsing book without toc for chinese book.")
epubBook = epubParser.createEpubBook(libraryItem.filePath, shouldUseToc = false)
}
// Create ebook data.
val ebookData = EbookData(
coverImage = coverImage,
state = state.copy(
title = libraryItem.title,
authors = libraryItem.authors,
epubBook = epubBook
coverImage = coverImage ?: epubBook.coverImage,
chapters = epubBook.chapters,
hasProgressSaved = progressData != null
)
delay(350) // Add delay to avoid flickering.
state = state.copy(isLoading = false, ebookData = ebookData)
delay(350) // Small delay for smooth transition.
state = state.copy(isLoading = false)
}
}
}

0 comments on commit de6728c

Please sign in to comment.