mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-07 20:31:02 -05:00
Minor cleanup
This commit is contained in:
parent
3f9820ac79
commit
c88b79fa17
6 changed files with 45 additions and 63 deletions
|
@ -13,16 +13,18 @@ import kotlinx.serialization.Serializable
|
|||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.encodeToString
|
||||
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.
|
||||
*
|
||||
* @param context the application context.
|
||||
*/
|
||||
class DownloadStore(
|
||||
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 json: Json by injectLazy()
|
||||
|
||||
private val getManga: GetManga by injectLazy()
|
||||
private val getChapter: GetChapter by injectLazy()
|
||||
|
||||
/**
|
||||
* Counter used to keep the queue order.
|
||||
*/
|
||||
|
@ -129,14 +126,14 @@ class DownloadStore(
|
|||
null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class used for download serialization
|
||||
*
|
||||
* @param mangaId the id of the manga.
|
||||
* @param chapterId the id of the chapter.
|
||||
* @param order the order of the download in the queue.
|
||||
*/
|
||||
@Serializable
|
||||
data class DownloadObject(val mangaId: Long, val chapterId: Long, val order: Int)
|
||||
}
|
||||
|
||||
/**
|
||||
* Class used for download serialization
|
||||
*
|
||||
* @param mangaId the id of the manga.
|
||||
* @param chapterId the id of the chapter.
|
||||
* @param order the order of the download in the queue.
|
||||
*/
|
||||
@Serializable
|
||||
private data class DownloadObject(val mangaId: Long, val chapterId: Long, val order: Int)
|
||||
|
|
|
@ -40,7 +40,6 @@ import rx.schedulers.Schedulers
|
|||
import rx.subscriptions.CompositeSubscription
|
||||
import uy.kohesive.injekt.Injekt
|
||||
import uy.kohesive.injekt.api.get
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
import java.io.BufferedOutputStream
|
||||
import java.io.File
|
||||
import java.util.zip.CRC32
|
||||
|
@ -68,17 +67,13 @@ class Downloader(
|
|||
private val sourceManager: SourceManager = Injekt.get(),
|
||||
private val chapterCache: ChapterCache = 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.
|
||||
*/
|
||||
private val store = DownloadStore(context, sourceManager)
|
||||
private val store = DownloadStore(context)
|
||||
|
||||
/**
|
||||
* Queue where active downloads are kept.
|
||||
|
|
|
@ -36,10 +36,10 @@ data class Download(
|
|||
}
|
||||
|
||||
@Transient
|
||||
private var statusSubject: PublishSubject<Download>? = null
|
||||
var statusSubject: PublishSubject<Download>? = null
|
||||
|
||||
@Transient
|
||||
private var statusCallback: ((Download) -> Unit)? = null
|
||||
var statusCallback: ((Download) -> Unit)? = null
|
||||
|
||||
val progress: Int
|
||||
get() {
|
||||
|
@ -47,14 +47,6 @@ data class Download(
|
|||
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) {
|
||||
NOT_DOWNLOADED(0),
|
||||
QUEUE(1),
|
||||
|
|
|
@ -31,8 +31,8 @@ class DownloadQueue(
|
|||
|
||||
fun addAll(downloads: List<Download>) {
|
||||
downloads.forEach { download ->
|
||||
download.setStatusSubject(statusSubject)
|
||||
download.setStatusCallback(::setPagesFor)
|
||||
download.statusSubject = statusSubject
|
||||
download.statusCallback = ::setPagesFor
|
||||
download.status = Download.State.QUEUE
|
||||
}
|
||||
queue.addAll(downloads)
|
||||
|
@ -45,8 +45,8 @@ class DownloadQueue(
|
|||
fun remove(download: Download) {
|
||||
val removed = queue.remove(download)
|
||||
store.remove(download)
|
||||
download.setStatusSubject(null)
|
||||
download.setStatusCallback(null)
|
||||
download.statusSubject = null
|
||||
download.statusCallback = null
|
||||
if (download.status == Download.State.DOWNLOADING || download.status == Download.State.QUEUE) {
|
||||
download.status = Download.State.NOT_DOWNLOADED
|
||||
}
|
||||
|
@ -62,9 +62,7 @@ class DownloadQueue(
|
|||
}
|
||||
|
||||
fun remove(chapters: List<Chapter>) {
|
||||
for (chapter in chapters) {
|
||||
remove(chapter)
|
||||
}
|
||||
chapters.forEach(::remove)
|
||||
}
|
||||
|
||||
fun remove(manga: Manga) {
|
||||
|
@ -73,8 +71,8 @@ class DownloadQueue(
|
|||
|
||||
fun clear() {
|
||||
queue.forEach { download ->
|
||||
download.setStatusSubject(null)
|
||||
download.setStatusCallback(null)
|
||||
download.statusSubject = null
|
||||
download.statusCallback = null
|
||||
if (download.status == Download.State.DOWNLOADING || download.status == Download.State.QUEUE) {
|
||||
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> =
|
||||
Observable.from(this).filter { download -> download.status == Download.State.DOWNLOADING }
|
||||
|
||||
|
@ -93,14 +95,6 @@ class DownloadQueue(
|
|||
.startWith(getActiveDownloads())
|
||||
.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> {
|
||||
return statusSubject.onBackpressureBuffer()
|
||||
.startWith(getActiveDownloads())
|
||||
|
@ -120,7 +114,11 @@ class DownloadQueue(
|
|||
.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>?) {
|
||||
pages?.forEach { it.setStatusSubject(subject) }
|
||||
|
|
|
@ -333,7 +333,7 @@ class LibraryUpdateNotifier(private val context: Context) {
|
|||
private fun getNotificationIntent(): PendingIntent {
|
||||
val intent = Intent(context, MainActivity::class.java).apply {
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -342,15 +342,15 @@ class MainActivity : BaseActivity() {
|
|||
|
||||
when (intent.action) {
|
||||
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 -> {
|
||||
val idToOpen = intent.extras?.getLong(Constants.MANGA_EXTRA) ?: return false
|
||||
navigator.popUntilRoot()
|
||||
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 -> {
|
||||
navigator.popUntilRoot()
|
||||
HomeScreen.openTab(HomeScreen.Tab.More(toDownloads = true))
|
||||
|
@ -413,12 +413,12 @@ class MainActivity : BaseActivity() {
|
|||
|
||||
// Shortcut actions
|
||||
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_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_DOWNLOADS = "eu.kanade.tachiyomi.SHOW_DOWNLOADS"
|
||||
|
||||
const val INTENT_SEARCH = "eu.kanade.tachiyomi.SEARCH"
|
||||
const val INTENT_SEARCH_QUERY = "query"
|
||||
|
|
Loading…
Reference in a new issue