From de6728c5116a6ec91c33c52f6425269e3e050cf9 Mon Sep 17 00:00:00 2001 From: starry-shivam Date: Fri, 27 Sep 2024 15:27:52 +0530 Subject: [PATCH] improve state management logic in reader detail screen Signed-off-by: starry-shivam --- .../starry/myne/ui/common/BookDetailTopUI.kt | 3 +- .../reader/detail/ReaderDetailScreen.kt | 17 +++++----- .../reader/detail/ReaderDetailViewModel.kt | 31 +++++++++---------- 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/starry/myne/ui/common/BookDetailTopUI.kt b/app/src/main/java/com/starry/myne/ui/common/BookDetailTopUI.kt index 7471016..1234977 100644 --- a/app/src/main/java/com/starry/myne/ui/common/BookDetailTopUI.kt +++ b/app/src/main/java/com/starry/myne/ui/common/BookDetailTopUI.kt @@ -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), @@ -167,7 +167,6 @@ fun BookDetailTopUI( color = MaterialTheme.colorScheme.onBackground, ) } - Spacer(modifier = Modifier.height(50.dp)) } } diff --git a/app/src/main/java/com/starry/myne/ui/screens/reader/detail/ReaderDetailScreen.kt b/app/src/main/java/com/starry/myne/ui/screens/reader/detail/ReaderDetailScreen.kt index 93c0e38..9bc9cc2 100644 --- a/app/src/main/java/com/starry/myne/ui/screens/reader/detail/ReaderDetailScreen.kt +++ b/app/src/main/java/com/starry/myne/ui/screens/reader/detail/ReaderDetailScreen.kt @@ -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( @@ -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( diff --git a/app/src/main/java/com/starry/myne/ui/screens/reader/detail/ReaderDetailViewModel.kt b/app/src/main/java/com/starry/myne/ui/screens/reader/detail/ReaderDetailViewModel.kt index 19fa9d7..d6417a2 100644 --- a/app/src/main/java/com/starry/myne/ui/screens/reader/detail/ReaderDetailViewModel.kt +++ b/app/src/main/java/com/starry/myne/ui/screens/reader/detail/ReaderDetailViewModel.kt @@ -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 @@ -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 = emptyList(), + val hasProgressSaved: Boolean = false, val error: String? = null, ) @@ -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 @@ -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) } } } \ No newline at end of file