Skip to content

Commit 370bb62

Browse files
AwkwardPeak7null2264
authored andcommitted
refactor: Change Page.State to sealed interface
1 parent 18528fb commit 370bb62

File tree

14 files changed

+63
-66
lines changed

14 files changed

+63
-66
lines changed

app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import kotlinx.coroutines.flow.map
2121
import kotlinx.coroutines.flow.merge
2222
import kotlinx.coroutines.flow.onStart
2323
import uy.kohesive.injekt.Injekt
24-
import uy.kohesive.injekt.injectLazy
2524
import uy.kohesive.injekt.api.get
25+
import uy.kohesive.injekt.injectLazy
2626
import yokai.domain.download.DownloadPreferences
2727
import yokai.i18n.MR
2828
import yokai.util.lang.getString
@@ -171,7 +171,7 @@ class DownloadManager(
171171

172172
return files.sortedBy { it.name }
173173
.mapIndexed { i, file ->
174-
Page(i, uri = file.uri).apply { status = Page.State.READY }
174+
Page(i, uri = file.uri).apply { status = Page.State.Ready }
175175
}
176176
}
177177

app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package eu.kanade.tachiyomi.data.download
22

33
import android.content.Context
4-
import android.os.Looper
54
import co.touchlab.kermit.Logger
65
import com.hippo.unifile.UniFile
76
import eu.kanade.tachiyomi.data.cache.ChapterCache
@@ -55,8 +54,8 @@ import kotlinx.coroutines.supervisorScope
5554
import nl.adaptivity.xmlutil.serialization.XML
5655
import okhttp3.Response
5756
import uy.kohesive.injekt.Injekt
58-
import uy.kohesive.injekt.injectLazy
5957
import uy.kohesive.injekt.api.get
58+
import uy.kohesive.injekt.injectLazy
6059
import yokai.core.archive.ZipWriter
6160
import yokai.core.metadata.COMIC_INFO_FILE
6261
import yokai.core.metadata.ComicInfo
@@ -365,11 +364,11 @@ class Downloader(
365364

366365
// Get all the URLs to the source images, fetch pages if necessary
367366
pageList.filter { it.imageUrl.isNullOrEmpty() }.forEach { page ->
368-
page.status = Page.State.LOAD_PAGE
367+
page.status = Page.State.LoadPage
369368
try {
370369
page.imageUrl = download.source.getImageUrl(page)
371370
} catch (e: Throwable) {
372-
page.status = Page.State.ERROR
371+
page.status = Page.State.Error
373372
}
374373
}
375374

@@ -494,12 +493,12 @@ class Downloader(
494493

495494
page.uri = file.uri
496495
page.progress = 100
497-
page.status = Page.State.READY
496+
page.status = Page.State.Ready
498497
} catch (e: Throwable) {
499498
if (e is CancellationException) throw e
500499
// Mark this page as error and allow to download the remaining
501500
page.progress = 0
502-
page.status = Page.State.ERROR
501+
page.status = Page.State.Error
503502
notifier.onError(e.message, chapName, download.manga.title)
504503
}
505504
}
@@ -518,7 +517,7 @@ class Downloader(
518517
tmpDir: UniFile,
519518
filename: String,
520519
): UniFile {
521-
page.status = Page.State.DOWNLOAD_IMAGE
520+
page.status = Page.State.DownloadImage
522521
page.progress = 0
523522
return flow {
524523
val response = source.getImage(page)

app/src/main/java/eu/kanade/tachiyomi/data/download/model/Download.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Download(val source: HttpSource, val manga: Manga, val chapter: Chapter) {
2222
get() = pages?.sumOf(Page::progress) ?: 0
2323

2424
val downloadedImages: Int
25-
get() = pages?.count { it.status == Page.State.READY } ?: 0
25+
get() = pages?.count { it.status is Page.State.Ready } ?: 0
2626

2727
@Transient
2828
private val _statusFlow = MutableStateFlow(State.NOT_DOWNLOADED)

app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,7 @@ class ReaderActivity : BaseActivity<ReaderActivityBinding>() {
16551655
}
16561656

16571657
private fun showSetCoverPrompt(page: ReaderPage) {
1658-
if (page.status != Page.State.READY) return
1658+
if (page.status !is Page.State.Ready) return
16591659

16601660
materialAlertDialog()
16611661
.setMessage(MR.strings.use_image_as_cover)

app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ class ReaderViewModel(
613613
}
614614

615615
val shouldTrack = !preferences.incognitoMode().get() || hasTrackers
616-
if (shouldTrack && page.status != Page.State.ERROR) {
616+
if (shouldTrack && page.status !is Page.State.Error) {
617617
readerChapter.chapter.last_page_read = page.index
618618
readerChapter.chapter.pages_left = (readerChapter.pages?.size ?: page.index) - page.index
619619
// For double pages, check if the second to last page is doubled up
@@ -860,7 +860,7 @@ class ReaderViewModel(
860860
* There's also a notification to allow sharing the image somewhere else or deleting it.
861861
*/
862862
fun saveImage(page: ReaderPage) {
863-
if (page.status != Page.State.READY) return
863+
if (page.status !is Page.State.Ready) return
864864
val manga = manga ?: return
865865
val context = Injekt.get<Application>()
866866

@@ -891,8 +891,8 @@ class ReaderViewModel(
891891

892892
fun saveImages(firstPage: ReaderPage, secondPage: ReaderPage, isLTR: Boolean, @ColorInt bg: Int) {
893893
viewModelScope.launch {
894-
if (firstPage.status != Page.State.READY) return@launch
895-
if (secondPage.status != Page.State.READY) return@launch
894+
if (firstPage.status !is Page.State.Ready) return@launch
895+
if (secondPage.status !is Page.State.Ready) return@launch
896896
val manga = manga ?: return@launch
897897
val context = Injekt.get<Application>()
898898

@@ -926,7 +926,7 @@ class ReaderViewModel(
926926
* image will be kept so it won't be taking lots of internal disk space.
927927
*/
928928
fun shareImage(page: ReaderPage) {
929-
if (page.status != Page.State.READY) return
929+
if (page.status !is Page.State.Ready) return
930930
val manga = manga ?: return
931931
val context = Injekt.get<Application>()
932932

@@ -940,8 +940,8 @@ class ReaderViewModel(
940940

941941
fun shareImages(firstPage: ReaderPage, secondPage: ReaderPage, isLTR: Boolean, @ColorInt bg: Int) {
942942
viewModelScope.launch {
943-
if (firstPage.status != Page.State.READY) return@launch
944-
if (secondPage.status != Page.State.READY) return@launch
943+
if (firstPage.status !is Page.State.Ready) return@launch
944+
if (secondPage.status !is Page.State.Ready) return@launch
945945
val manga = manga ?: return@launch
946946
val context = Injekt.get<Application>()
947947

@@ -958,7 +958,7 @@ class ReaderViewModel(
958958
* Sets the image of this [page] as cover and notifies the UI of the result.
959959
*/
960960
fun setAsCover(page: ReaderPage) {
961-
if (page.status != Page.State.READY) return
961+
if (page.status !is Page.State.Ready) return
962962
val manga = manga ?: return
963963
val stream = page.stream ?: return
964964

app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ArchivePageLoader.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal class ArchivePageLoader(private val reader: ArchiveReader) : PageLoader
3131
.mapIndexed { i, entry ->
3232
ReaderPage(i).apply {
3333
stream = { reader.getInputStream(entry.name)!! }
34-
status = Page.State.READY
34+
status = Page.State.Ready
3535
}
3636
}
3737
.toList()

app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/DirectoryPageLoader.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class DirectoryPageLoader(val file: UniFile) : PageLoader() {
2424
val streamFn = { file.openInputStream() }
2525
ReaderPage(i).apply {
2626
stream = streamFn
27-
status = Page.State.READY
27+
status = Page.State.Ready
2828
}
2929
} ?: emptyList()
3030
}

app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/DownloadPageLoader.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class DownloadPageLoader(
6060
ReaderPage(page.index, page.url, page.imageUrl, stream = {
6161
context.contentResolver.openInputStream(page.uri ?: Uri.EMPTY)!!
6262
},).apply {
63-
status = Page.State.READY
63+
status = Page.State.Ready
6464
}
6565
}
6666
}

app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/EpubPageLoader.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class EpubPageLoader(private val epub: EpubReader) : PageLoader() {
2828
val streamFn = { epub.getInputStream(path)!! }
2929
ReaderPage(i).apply {
3030
stream = streamFn
31-
status = Page.State.READY
31+
status = Page.State.Ready
3232
}
3333
}
3434
}

app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class HttpPageLoader(
5151
emit(runInterruptible { queue.take() }.page)
5252
}
5353
}
54-
.filter { it.status == Page.State.QUEUE }
54+
.filter { it.status is Page.State.Queue }
5555
.collect {
5656
_loadPage(it)
5757
}
@@ -108,25 +108,25 @@ class HttpPageLoader(
108108
val imageUrl = page.imageUrl
109109

110110
// Check if the image has been deleted
111-
if (page.status == Page.State.READY && imageUrl != null && !chapterCache.isImageInCache(imageUrl)) {
112-
page.status = Page.State.QUEUE
111+
if (page.status is Page.State.Ready && imageUrl != null && !chapterCache.isImageInCache(imageUrl)) {
112+
page.status = Page.State.Queue
113113
}
114114

115115
// Automatically retry failed pages when subscribed to this page
116-
if (page.status == Page.State.ERROR) {
117-
page.status = Page.State.QUEUE
116+
if (page.status is Page.State.Error) {
117+
page.status = Page.State.Queue
118118
}
119119

120120
val queuedPages = mutableListOf<PriorityPage>()
121-
if (page.status == Page.State.QUEUE) {
121+
if (page.status is Page.State.Queue) {
122122
queuedPages += PriorityPage(page, 1).also { queue.offer(it) }
123123
}
124124
queuedPages += preloadNextPages(page, preloadSize)
125125

126126
suspendCancellableCoroutine<Nothing> { continuation ->
127127
continuation.invokeOnCancellation {
128128
queuedPages.forEach {
129-
if (it.page.status == Page.State.QUEUE) {
129+
if (it.page.status is Page.State.Queue) {
130130
queue.remove(it)
131131
}
132132
}
@@ -146,7 +146,7 @@ class HttpPageLoader(
146146
return pages
147147
.subList(pageIndex + 1, min(pageIndex + 1 + amount, pages.size))
148148
.mapNotNull {
149-
if (it.status == Page.State.QUEUE) {
149+
if (it.status is Page.State.Queue) {
150150
PriorityPage(it, 0).apply { queue.offer(this) }
151151
} else {
152152
null
@@ -158,8 +158,8 @@ class HttpPageLoader(
158158
* Retries a page. This method is only called from user interaction on the viewer.
159159
*/
160160
override fun retryPage(page: ReaderPage) {
161-
if (page.status == Page.State.ERROR) {
162-
page.status = Page.State.QUEUE
161+
if (page.status is Page.State.Error) {
162+
page.status = Page.State.Queue
163163
}
164164
queue.offer(PriorityPage(page, 2))
165165
}
@@ -192,21 +192,21 @@ class HttpPageLoader(
192192
private suspend fun _loadPage(page: ReaderPage) {
193193
try {
194194
if (page.imageUrl.isNullOrEmpty()) {
195-
page.status = Page.State.LOAD_PAGE
195+
page.status = Page.State.LoadPage
196196
page.imageUrl = source.getImageUrl(page)
197197
}
198198
val imageUrl = page.imageUrl!!
199199

200200
if (!chapterCache.isImageInCache(imageUrl)) {
201-
page.status = Page.State.DOWNLOAD_IMAGE
201+
page.status = Page.State.DownloadImage
202202
val imageResponse = source.getImage(page)
203203
chapterCache.putImageToCache(imageUrl, imageResponse)
204204
}
205205

206206
page.stream = { chapterCache.getImageFile(imageUrl).inputStream() }
207-
page.status = Page.State.READY
207+
page.status = Page.State.Ready
208208
} catch (e: Throwable) {
209-
page.status = Page.State.ERROR
209+
page.status = Page.State.Error
210210
if (e is CancellationException) {
211211
throw e
212212
}

0 commit comments

Comments
 (0)