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 }
.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 {
// Fetch image URL if necessary
if (page.imageUrl.isNullOrEmpty()) {
page.status = Page.State.LOAD_PAGE
page.status = Page.State.LoadPage
try {
page.imageUrl = download.source.getImageUrl(page)
} catch (e: Throwable) {
page.status = Page.State.ERROR
page.status = Page.State.Error
}
}
@ -452,12 +452,12 @@ class Downloader(
page.uri = file.uri
page.progress = 100
page.status = Page.State.READY
page.status = Page.State.Ready
} catch (e: Throwable) {
if (e is CancellationException) throw e
// Mark this page as error and allow to download the remaining
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)
}
}
@ -471,7 +471,7 @@ class Downloader(
* @param filename the filename of the image.
*/
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
return flow {
val response = source.getImage(page)

View File

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

View File

@ -537,7 +537,7 @@ class ReaderViewModel @JvmOverloads constructor(
readerChapter.requestedPage = pageIndex
chapterPageIndex = pageIndex
if (!incognitoMode && page.status != Page.State.ERROR) {
if (!incognitoMode && page.status != Page.State.Error) {
readerChapter.chapter.last_page_read = pageIndex
if (readerChapter.pages?.lastIndex == pageIndex) {
@ -799,7 +799,7 @@ class ReaderViewModel @JvmOverloads constructor(
*/
fun saveImage() {
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 context = Injekt.get<Application>()
@ -847,7 +847,7 @@ class ReaderViewModel @JvmOverloads constructor(
*/
fun shareImage(copyToClipboard: Boolean) {
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 context = Injekt.get<Application>()
@ -877,7 +877,7 @@ class ReaderViewModel @JvmOverloads constructor(
*/
fun setAsCover() {
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 stream = page.stream ?: return

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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