Minor cleanup

This commit is contained in:
arkon 2022-12-05 14:14:50 -05:00
parent 3f9820ac79
commit c88b79fa17
6 changed files with 45 additions and 63 deletions

View File

@ -13,16 +13,18 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
/** /**
* This class is used to persist active downloads across application restarts. * This class is used to persist active downloads across application restarts.
*
* @param context the application context.
*/ */
class DownloadStore( class DownloadStore(
context: Context, context: Context,
private val sourceManager: SourceManager, private val sourceManager: SourceManager = Injekt.get(),
private val json: Json = Injekt.get(),
private val getManga: GetManga = Injekt.get(),
private val getChapter: GetChapter = Injekt.get(),
) { ) {
/** /**
@ -30,11 +32,6 @@ class DownloadStore(
*/ */
private val preferences = context.getSharedPreferences("active_downloads", Context.MODE_PRIVATE) private val preferences = context.getSharedPreferences("active_downloads", Context.MODE_PRIVATE)
private val json: Json by injectLazy()
private val getManga: GetManga by injectLazy()
private val getChapter: GetChapter by injectLazy()
/** /**
* Counter used to keep the queue order. * Counter used to keep the queue order.
*/ */
@ -129,14 +126,14 @@ class DownloadStore(
null null
} }
} }
}
/** /**
* Class used for download serialization * Class used for download serialization
* *
* @param mangaId the id of the manga. * @param mangaId the id of the manga.
* @param chapterId the id of the chapter. * @param chapterId the id of the chapter.
* @param order the order of the download in the queue. * @param order the order of the download in the queue.
*/ */
@Serializable @Serializable
data class DownloadObject(val mangaId: Long, val chapterId: Long, val order: Int) private data class DownloadObject(val mangaId: Long, val chapterId: Long, val order: Int)
}

View File

@ -40,7 +40,6 @@ import rx.schedulers.Schedulers
import rx.subscriptions.CompositeSubscription import rx.subscriptions.CompositeSubscription
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import java.io.BufferedOutputStream import java.io.BufferedOutputStream
import java.io.File import java.io.File
import java.util.zip.CRC32 import java.util.zip.CRC32
@ -68,17 +67,13 @@ class Downloader(
private val sourceManager: SourceManager = Injekt.get(), private val sourceManager: SourceManager = Injekt.get(),
private val chapterCache: ChapterCache = Injekt.get(), private val chapterCache: ChapterCache = Injekt.get(),
private val downloadPreferences: DownloadPreferences = Injekt.get(), private val downloadPreferences: DownloadPreferences = Injekt.get(),
private val xml: XML = Injekt.get(),
) { ) {
/**
* xml format used for ComicInfo files
*/
private val xml: XML by injectLazy()
/** /**
* Store for persisting downloads across restarts. * Store for persisting downloads across restarts.
*/ */
private val store = DownloadStore(context, sourceManager) private val store = DownloadStore(context)
/** /**
* Queue where active downloads are kept. * Queue where active downloads are kept.

View File

@ -36,10 +36,10 @@ data class Download(
} }
@Transient @Transient
private var statusSubject: PublishSubject<Download>? = null var statusSubject: PublishSubject<Download>? = null
@Transient @Transient
private var statusCallback: ((Download) -> Unit)? = null var statusCallback: ((Download) -> Unit)? = null
val progress: Int val progress: Int
get() { get() {
@ -47,14 +47,6 @@ data class Download(
return pages.map(Page::progress).average().toInt() return pages.map(Page::progress).average().toInt()
} }
fun setStatusSubject(subject: PublishSubject<Download>?) {
statusSubject = subject
}
fun setStatusCallback(f: ((Download) -> Unit)?) {
statusCallback = f
}
enum class State(val value: Int) { enum class State(val value: Int) {
NOT_DOWNLOADED(0), NOT_DOWNLOADED(0),
QUEUE(1), QUEUE(1),

View File

@ -31,8 +31,8 @@ class DownloadQueue(
fun addAll(downloads: List<Download>) { fun addAll(downloads: List<Download>) {
downloads.forEach { download -> downloads.forEach { download ->
download.setStatusSubject(statusSubject) download.statusSubject = statusSubject
download.setStatusCallback(::setPagesFor) download.statusCallback = ::setPagesFor
download.status = Download.State.QUEUE download.status = Download.State.QUEUE
} }
queue.addAll(downloads) queue.addAll(downloads)
@ -45,8 +45,8 @@ class DownloadQueue(
fun remove(download: Download) { fun remove(download: Download) {
val removed = queue.remove(download) val removed = queue.remove(download)
store.remove(download) store.remove(download)
download.setStatusSubject(null) download.statusSubject = null
download.setStatusCallback(null) download.statusCallback = null
if (download.status == Download.State.DOWNLOADING || download.status == Download.State.QUEUE) { if (download.status == Download.State.DOWNLOADING || download.status == Download.State.QUEUE) {
download.status = Download.State.NOT_DOWNLOADED download.status = Download.State.NOT_DOWNLOADED
} }
@ -62,9 +62,7 @@ class DownloadQueue(
} }
fun remove(chapters: List<Chapter>) { fun remove(chapters: List<Chapter>) {
for (chapter in chapters) { chapters.forEach(::remove)
remove(chapter)
}
} }
fun remove(manga: Manga) { fun remove(manga: Manga) {
@ -73,8 +71,8 @@ class DownloadQueue(
fun clear() { fun clear() {
queue.forEach { download -> queue.forEach { download ->
download.setStatusSubject(null) download.statusSubject = null
download.setStatusCallback(null) download.statusCallback = null
if (download.status == Download.State.DOWNLOADING || download.status == Download.State.QUEUE) { if (download.status == Download.State.DOWNLOADING || download.status == Download.State.QUEUE) {
download.status = Download.State.NOT_DOWNLOADED download.status = Download.State.NOT_DOWNLOADED
} }
@ -86,6 +84,10 @@ class DownloadQueue(
} }
} }
fun statusFlow(): Flow<Download> = getStatusObservable().asFlow()
fun progressFlow(): Flow<Download> = getProgressObservable().asFlow()
private fun getActiveDownloads(): Observable<Download> = private fun getActiveDownloads(): Observable<Download> =
Observable.from(this).filter { download -> download.status == Download.State.DOWNLOADING } Observable.from(this).filter { download -> download.status == Download.State.DOWNLOADING }
@ -93,14 +95,6 @@ class DownloadQueue(
.startWith(getActiveDownloads()) .startWith(getActiveDownloads())
.onBackpressureBuffer() .onBackpressureBuffer()
fun statusFlow(): Flow<Download> = getStatusObservable().asFlow()
private fun setPagesFor(download: Download) {
if (download.status == Download.State.DOWNLOADED || download.status == Download.State.ERROR) {
setPagesSubject(download.pages, null)
}
}
private fun getProgressObservable(): Observable<Download> { private fun getProgressObservable(): Observable<Download> {
return statusSubject.onBackpressureBuffer() return statusSubject.onBackpressureBuffer()
.startWith(getActiveDownloads()) .startWith(getActiveDownloads())
@ -120,7 +114,11 @@ class DownloadQueue(
.filter { it.status == Download.State.DOWNLOADING } .filter { it.status == Download.State.DOWNLOADING }
} }
fun progressFlow(): Flow<Download> = getProgressObservable().asFlow() private fun setPagesFor(download: Download) {
if (download.status == Download.State.DOWNLOADED || download.status == Download.State.ERROR) {
setPagesSubject(download.pages, null)
}
}
private fun setPagesSubject(pages: List<Page>?, subject: PublishSubject<Int>?) { private fun setPagesSubject(pages: List<Page>?, subject: PublishSubject<Int>?) {
pages?.forEach { it.setStatusSubject(subject) } pages?.forEach { it.setStatusSubject(subject) }

View File

@ -333,7 +333,7 @@ class LibraryUpdateNotifier(private val context: Context) {
private fun getNotificationIntent(): PendingIntent { private fun getNotificationIntent(): PendingIntent {
val intent = Intent(context, MainActivity::class.java).apply { val intent = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
action = MainActivity.SHORTCUT_RECENTLY_UPDATED action = MainActivity.SHORTCUT_UPDATES
} }
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
} }

View File

@ -342,15 +342,15 @@ class MainActivity : BaseActivity() {
when (intent.action) { when (intent.action) {
SHORTCUT_LIBRARY -> HomeScreen.openTab(HomeScreen.Tab.Library()) SHORTCUT_LIBRARY -> HomeScreen.openTab(HomeScreen.Tab.Library())
SHORTCUT_RECENTLY_UPDATED -> HomeScreen.openTab(HomeScreen.Tab.Updates)
SHORTCUT_RECENTLY_READ -> HomeScreen.openTab(HomeScreen.Tab.History)
SHORTCUT_CATALOGUES -> HomeScreen.openTab(HomeScreen.Tab.Browse(false))
SHORTCUT_EXTENSIONS -> HomeScreen.openTab(HomeScreen.Tab.Browse(true))
SHORTCUT_MANGA -> { SHORTCUT_MANGA -> {
val idToOpen = intent.extras?.getLong(Constants.MANGA_EXTRA) ?: return false val idToOpen = intent.extras?.getLong(Constants.MANGA_EXTRA) ?: return false
navigator.popUntilRoot() navigator.popUntilRoot()
HomeScreen.openTab(HomeScreen.Tab.Library(idToOpen)) HomeScreen.openTab(HomeScreen.Tab.Library(idToOpen))
} }
SHORTCUT_UPDATES -> HomeScreen.openTab(HomeScreen.Tab.Updates)
SHORTCUT_HISTORY -> HomeScreen.openTab(HomeScreen.Tab.History)
SHORTCUT_SOURCES -> HomeScreen.openTab(HomeScreen.Tab.Browse(false))
SHORTCUT_EXTENSIONS -> HomeScreen.openTab(HomeScreen.Tab.Browse(true))
SHORTCUT_DOWNLOADS -> { SHORTCUT_DOWNLOADS -> {
navigator.popUntilRoot() navigator.popUntilRoot()
HomeScreen.openTab(HomeScreen.Tab.More(toDownloads = true)) HomeScreen.openTab(HomeScreen.Tab.More(toDownloads = true))
@ -413,12 +413,12 @@ class MainActivity : BaseActivity() {
// Shortcut actions // Shortcut actions
const val SHORTCUT_LIBRARY = "eu.kanade.tachiyomi.SHOW_LIBRARY" const val SHORTCUT_LIBRARY = "eu.kanade.tachiyomi.SHOW_LIBRARY"
const val SHORTCUT_RECENTLY_UPDATED = "eu.kanade.tachiyomi.SHOW_RECENTLY_UPDATED"
const val SHORTCUT_RECENTLY_READ = "eu.kanade.tachiyomi.SHOW_RECENTLY_READ"
const val SHORTCUT_CATALOGUES = "eu.kanade.tachiyomi.SHOW_CATALOGUES"
const val SHORTCUT_DOWNLOADS = "eu.kanade.tachiyomi.SHOW_DOWNLOADS"
const val SHORTCUT_MANGA = "eu.kanade.tachiyomi.SHOW_MANGA" const val SHORTCUT_MANGA = "eu.kanade.tachiyomi.SHOW_MANGA"
const val SHORTCUT_UPDATES = "eu.kanade.tachiyomi.SHOW_RECENTLY_UPDATED"
const val SHORTCUT_HISTORY = "eu.kanade.tachiyomi.SHOW_RECENTLY_READ"
const val SHORTCUT_SOURCES = "eu.kanade.tachiyomi.SHOW_CATALOGUES"
const val SHORTCUT_EXTENSIONS = "eu.kanade.tachiyomi.EXTENSIONS" const val SHORTCUT_EXTENSIONS = "eu.kanade.tachiyomi.EXTENSIONS"
const val SHORTCUT_DOWNLOADS = "eu.kanade.tachiyomi.SHOW_DOWNLOADS"
const val INTENT_SEARCH = "eu.kanade.tachiyomi.SEARCH" const val INTENT_SEARCH = "eu.kanade.tachiyomi.SEARCH"
const val INTENT_SEARCH_QUERY = "query" const val INTENT_SEARCH_QUERY = "query"