Change Page.State to sealed interface (#1988)

This commit is contained in:
AwkwardPeak7
2025-04-13 15:32:20 +05:00
committed by GitHub
parent 180318f57d
commit f1e2efcb37
13 changed files with 47 additions and 47 deletions

View File

@ -169,7 +169,7 @@ class DownloadManager(
return files.sortedBy { it.name } return files.sortedBy { it.name }
.mapIndexed { i, file -> .mapIndexed { i, file ->
Page(i, uri = file.uri).apply { status = Page.State.READY } Page(i, uri = file.uri).apply { status = Page.State.Ready }
} }
} }

View File

@ -361,11 +361,11 @@ class Downloader(
flow { flow {
// Fetch image URL if necessary // Fetch image URL if necessary
if (page.imageUrl.isNullOrEmpty()) { if (page.imageUrl.isNullOrEmpty()) {
page.status = Page.State.LOAD_PAGE page.status = Page.State.LoadPage
try { try {
page.imageUrl = download.source.getImageUrl(page) page.imageUrl = download.source.getImageUrl(page)
} catch (e: Throwable) { } catch (e: Throwable) {
page.status = Page.State.ERROR page.status = Page.State.Error
} }
} }
@ -452,12 +452,12 @@ class Downloader(
page.uri = file.uri page.uri = file.uri
page.progress = 100 page.progress = 100
page.status = Page.State.READY page.status = Page.State.Ready
} catch (e: Throwable) { } catch (e: Throwable) {
if (e is CancellationException) throw e if (e is CancellationException) throw e
// Mark this page as error and allow to download the remaining // Mark this page as error and allow to download the remaining
page.progress = 0 page.progress = 0
page.status = Page.State.ERROR page.status = Page.State.Error
notifier.onError(e.message, download.chapter.name, download.manga.title, download.manga.id) notifier.onError(e.message, download.chapter.name, download.manga.title, download.manga.id)
} }
} }
@ -471,7 +471,7 @@ class Downloader(
* @param filename the filename of the image. * @param filename the filename of the image.
*/ */
private suspend fun downloadImage(page: Page, source: HttpSource, tmpDir: UniFile, filename: String): UniFile { private suspend fun downloadImage(page: Page, source: HttpSource, tmpDir: UniFile, filename: String): UniFile {
page.status = Page.State.DOWNLOAD_IMAGE page.status = Page.State.DownloadImage
page.progress = 0 page.progress = 0
return flow { return flow {
val response = source.getImage(page) val response = source.getImage(page)

View File

@ -29,7 +29,7 @@ data class Download(
get() = pages?.sumOf(Page::progress) ?: 0 get() = pages?.sumOf(Page::progress) ?: 0
val downloadedImages: Int val downloadedImages: Int
get() = pages?.count { it.status == Page.State.READY } ?: 0 get() = pages?.count { it.status == Page.State.Ready } ?: 0
@Transient @Transient
private val _statusFlow = MutableStateFlow(State.NOT_DOWNLOADED) private val _statusFlow = MutableStateFlow(State.NOT_DOWNLOADED)

View File

@ -537,7 +537,7 @@ class ReaderViewModel @JvmOverloads constructor(
readerChapter.requestedPage = pageIndex readerChapter.requestedPage = pageIndex
chapterPageIndex = pageIndex chapterPageIndex = pageIndex
if (!incognitoMode && page.status != Page.State.ERROR) { if (!incognitoMode && page.status != Page.State.Error) {
readerChapter.chapter.last_page_read = pageIndex readerChapter.chapter.last_page_read = pageIndex
if (readerChapter.pages?.lastIndex == pageIndex) { if (readerChapter.pages?.lastIndex == pageIndex) {
@ -799,7 +799,7 @@ class ReaderViewModel @JvmOverloads constructor(
*/ */
fun saveImage() { fun saveImage() {
val page = (state.value.dialog as? Dialog.PageActions)?.page val page = (state.value.dialog as? Dialog.PageActions)?.page
if (page?.status != Page.State.READY) return if (page?.status != Page.State.Ready) return
val manga = manga ?: return val manga = manga ?: return
val context = Injekt.get<Application>() val context = Injekt.get<Application>()
@ -847,7 +847,7 @@ class ReaderViewModel @JvmOverloads constructor(
*/ */
fun shareImage(copyToClipboard: Boolean) { fun shareImage(copyToClipboard: Boolean) {
val page = (state.value.dialog as? Dialog.PageActions)?.page val page = (state.value.dialog as? Dialog.PageActions)?.page
if (page?.status != Page.State.READY) return if (page?.status != Page.State.Ready) return
val manga = manga ?: return val manga = manga ?: return
val context = Injekt.get<Application>() val context = Injekt.get<Application>()
@ -877,7 +877,7 @@ class ReaderViewModel @JvmOverloads constructor(
*/ */
fun setAsCover() { fun setAsCover() {
val page = (state.value.dialog as? Dialog.PageActions)?.page val page = (state.value.dialog as? Dialog.PageActions)?.page
if (page?.status != Page.State.READY) return if (page?.status != Page.State.Ready) return
val manga = manga ?: return val manga = manga ?: return
val stream = page.stream ?: return val stream = page.stream ?: return

View File

@ -19,7 +19,7 @@ internal class ArchivePageLoader(private val reader: ArchiveReader) : PageLoader
.mapIndexed { i, entry -> .mapIndexed { i, entry ->
ReaderPage(i).apply { ReaderPage(i).apply {
stream = { reader.getInputStream(entry.name)!! } stream = { reader.getInputStream(entry.name)!! }
status = Page.State.READY status = Page.State.Ready
} }
} }
.toList() .toList()

View File

@ -21,7 +21,7 @@ internal class DirectoryPageLoader(val file: UniFile) : PageLoader() {
val streamFn = { file.openInputStream() } val streamFn = { file.openInputStream() }
ReaderPage(i).apply { ReaderPage(i).apply {
stream = streamFn stream = streamFn
status = Page.State.READY status = Page.State.Ready
} }
} }
.orEmpty() .orEmpty()

View File

@ -57,7 +57,7 @@ internal class DownloadPageLoader(
ReaderPage(page.index, page.url, page.imageUrl) { ReaderPage(page.index, page.url, page.imageUrl) {
context.contentResolver.openInputStream(page.uri ?: Uri.EMPTY)!! context.contentResolver.openInputStream(page.uri ?: Uri.EMPTY)!!
}.apply { }.apply {
status = Page.State.READY status = Page.State.Ready
} }
} }
} }

View File

@ -15,7 +15,7 @@ internal class EpubPageLoader(private val reader: EpubReader) : PageLoader() {
return reader.getImagesFromPages().mapIndexed { i, path -> return reader.getImagesFromPages().mapIndexed { i, path ->
ReaderPage(i).apply { ReaderPage(i).apply {
stream = { reader.getInputStream(path)!! } stream = { reader.getInputStream(path)!! }
status = Page.State.READY status = Page.State.Ready
} }
} }
} }

View File

@ -50,7 +50,7 @@ internal class HttpPageLoader(
emit(runInterruptible { queue.take() }.page) emit(runInterruptible { queue.take() }.page)
} }
} }
.filter { it.status == Page.State.QUEUE } .filter { it.status == Page.State.Queue }
.collect(::internalLoadPage) .collect(::internalLoadPage)
} }
} }
@ -83,17 +83,17 @@ internal class HttpPageLoader(
val imageUrl = page.imageUrl val imageUrl = page.imageUrl
// Check if the image has been deleted // Check if the image has been deleted
if (page.status == Page.State.READY && imageUrl != null && !chapterCache.isImageInCache(imageUrl)) { if (page.status == Page.State.Ready && imageUrl != null && !chapterCache.isImageInCache(imageUrl)) {
page.status = Page.State.QUEUE page.status = Page.State.Queue
} }
// Automatically retry failed pages when subscribed to this page // Automatically retry failed pages when subscribed to this page
if (page.status == Page.State.ERROR) { if (page.status == Page.State.Error) {
page.status = Page.State.QUEUE page.status = Page.State.Queue
} }
val queuedPages = mutableListOf<PriorityPage>() val queuedPages = mutableListOf<PriorityPage>()
if (page.status == Page.State.QUEUE) { if (page.status == Page.State.Queue) {
queuedPages += PriorityPage(page, 1).also { queue.offer(it) } queuedPages += PriorityPage(page, 1).also { queue.offer(it) }
} }
queuedPages += preloadNextPages(page, preloadSize) queuedPages += preloadNextPages(page, preloadSize)
@ -101,7 +101,7 @@ internal class HttpPageLoader(
suspendCancellableCoroutine<Nothing> { continuation -> suspendCancellableCoroutine<Nothing> { continuation ->
continuation.invokeOnCancellation { continuation.invokeOnCancellation {
queuedPages.forEach { queuedPages.forEach {
if (it.page.status == Page.State.QUEUE) { if (it.page.status == Page.State.Queue) {
queue.remove(it) queue.remove(it)
} }
} }
@ -113,8 +113,8 @@ internal class HttpPageLoader(
* Retries a page. This method is only called from user interaction on the viewer. * Retries a page. This method is only called from user interaction on the viewer.
*/ */
override fun retryPage(page: ReaderPage) { override fun retryPage(page: ReaderPage) {
if (page.status == Page.State.ERROR) { if (page.status == Page.State.Error) {
page.status = Page.State.QUEUE page.status = Page.State.Queue
} }
queue.offer(PriorityPage(page, 2)) queue.offer(PriorityPage(page, 2))
} }
@ -153,7 +153,7 @@ internal class HttpPageLoader(
return pages return pages
.subList(pageIndex + 1, min(pageIndex + 1 + amount, pages.size)) .subList(pageIndex + 1, min(pageIndex + 1 + amount, pages.size))
.mapNotNull { .mapNotNull {
if (it.status == Page.State.QUEUE) { if (it.status == Page.State.Queue) {
PriorityPage(it, 0).apply { queue.offer(this) } PriorityPage(it, 0).apply { queue.offer(this) }
} else { } else {
null null
@ -170,21 +170,21 @@ internal class HttpPageLoader(
private suspend fun internalLoadPage(page: ReaderPage) { private suspend fun internalLoadPage(page: ReaderPage) {
try { try {
if (page.imageUrl.isNullOrEmpty()) { if (page.imageUrl.isNullOrEmpty()) {
page.status = Page.State.LOAD_PAGE page.status = Page.State.LoadPage
page.imageUrl = source.getImageUrl(page) page.imageUrl = source.getImageUrl(page)
} }
val imageUrl = page.imageUrl!! val imageUrl = page.imageUrl!!
if (!chapterCache.isImageInCache(imageUrl)) { if (!chapterCache.isImageInCache(imageUrl)) {
page.status = Page.State.DOWNLOAD_IMAGE page.status = Page.State.DownloadImage
val imageResponse = source.getImage(page) val imageResponse = source.getImage(page)
chapterCache.putImageToCache(imageUrl, imageResponse) chapterCache.putImageToCache(imageUrl, imageResponse)
} }
page.stream = { chapterCache.getImageFile(imageUrl).inputStream() } page.stream = { chapterCache.getImageFile(imageUrl).inputStream() }
page.status = Page.State.READY page.status = Page.State.Ready
} catch (e: Throwable) { } catch (e: Throwable) {
page.status = Page.State.ERROR page.status = Page.State.Error
if (e is CancellationException) { if (e is CancellationException) {
throw e throw e
} }

View File

@ -5,7 +5,7 @@ class InsertPage(val parent: ReaderPage) : ReaderPage(parent.index, parent.url,
override var chapter: ReaderChapter = parent.chapter override var chapter: ReaderChapter = parent.chapter
init { init {
status = State.READY status = State.Ready
stream = parent.stream stream = parent.stream
} }
} }

View File

@ -96,16 +96,16 @@ class PagerPageHolder(
} }
page.statusFlow.collectLatest { state -> page.statusFlow.collectLatest { state ->
when (state) { when (state) {
Page.State.QUEUE -> setQueued() Page.State.Queue -> setQueued()
Page.State.LOAD_PAGE -> setLoading() Page.State.LoadPage -> setLoading()
Page.State.DOWNLOAD_IMAGE -> { Page.State.DownloadImage -> {
setDownloading() setDownloading()
page.progressFlow.collectLatest { value -> page.progressFlow.collectLatest { value ->
progressIndicator?.setProgress(value) progressIndicator?.setProgress(value)
} }
} }
Page.State.READY -> setImage() Page.State.Ready -> setImage()
Page.State.ERROR -> setError() Page.State.Error -> setError()
} }
} }
} }

View File

@ -136,16 +136,16 @@ class WebtoonPageHolder(
} }
page.statusFlow.collectLatest { state -> page.statusFlow.collectLatest { state ->
when (state) { when (state) {
Page.State.QUEUE -> setQueued() Page.State.Queue -> setQueued()
Page.State.LOAD_PAGE -> setLoading() Page.State.LoadPage -> setLoading()
Page.State.DOWNLOAD_IMAGE -> { Page.State.DownloadImage -> {
setDownloading() setDownloading()
page.progressFlow.collectLatest { value -> page.progressFlow.collectLatest { value ->
progressIndicator.setProgress(value) progressIndicator.setProgress(value)
} }
} }
Page.State.READY -> setImage() Page.State.Ready -> setImage()
Page.State.ERROR -> setError() Page.State.Error -> setError()
} }
} }
} }

View File

@ -19,7 +19,7 @@ open class Page(
get() = index + 1 get() = index + 1
@Transient @Transient
private val _statusFlow = MutableStateFlow(State.QUEUE) private val _statusFlow = MutableStateFlow<State>(State.Queue)
@Transient @Transient
val statusFlow = _statusFlow.asStateFlow() val statusFlow = _statusFlow.asStateFlow()
@ -48,11 +48,11 @@ open class Page(
} }
} }
enum class State { sealed interface State {
QUEUE, data object Queue : State
LOAD_PAGE, data object LoadPage : State
DOWNLOAD_IMAGE, data object DownloadImage : State
READY, data object Ready : State
ERROR, data object Error : State
} }
} }