mirror of
https://github.com/mihonapp/mihon.git
synced 2025-11-18 15:07:30 +01:00
Downloads view now uses a copy of the original queue. Fixes #351 and some crashes while scrolling and removing a download from the queue
This commit is contained in:
@@ -6,29 +6,41 @@ import rx.Observable
|
||||
import rx.subjects.PublishSubject
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
|
||||
class DownloadQueue : CopyOnWriteArrayList<Download>() {
|
||||
class DownloadQueue(private val queue: MutableList<Download> = CopyOnWriteArrayList<Download>())
|
||||
: List<Download> by queue {
|
||||
|
||||
private val statusSubject = PublishSubject.create<Download>()
|
||||
|
||||
override fun add(download: Download): Boolean {
|
||||
private val removeSubject = PublishSubject.create<Download>()
|
||||
|
||||
fun add(download: Download): Boolean {
|
||||
download.setStatusSubject(statusSubject)
|
||||
download.status = Download.QUEUE
|
||||
return super.add(download)
|
||||
return queue.add(download)
|
||||
}
|
||||
|
||||
fun del(download: Download) {
|
||||
super.remove(download)
|
||||
val removed = queue.remove(download)
|
||||
download.setStatusSubject(null)
|
||||
if (removed) {
|
||||
removeSubject.onNext(download)
|
||||
}
|
||||
}
|
||||
|
||||
fun del(chapter: Chapter) {
|
||||
find { it.chapter.id == chapter.id }?.let { del(it) }
|
||||
}
|
||||
|
||||
fun getActiveDownloads() =
|
||||
fun clear() {
|
||||
queue.forEach { del(it) }
|
||||
}
|
||||
|
||||
fun getActiveDownloads(): Observable<Download> =
|
||||
Observable.from(this).filter { download -> download.status == Download.DOWNLOADING }
|
||||
|
||||
fun getStatusObservable() = statusSubject.onBackpressureBuffer()
|
||||
fun getStatusObservable(): Observable<Download> = statusSubject.onBackpressureBuffer()
|
||||
|
||||
fun getRemovedObservable(): Observable<Download> = removeSubject.onBackpressureBuffer()
|
||||
|
||||
fun getProgressObservable(): Observable<Download> {
|
||||
return statusSubject.onBackpressureBuffer()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package eu.kanade.tachiyomi.data.glide
|
||||
|
||||
import android.support.v4.util.AtomicFile
|
||||
import com.bumptech.glide.Priority
|
||||
import com.bumptech.glide.load.data.DataFetcher
|
||||
import eu.kanade.tachiyomi.data.database.models.Manga
|
||||
@@ -27,16 +28,14 @@ class MangaDataFetcher(private val networkFetcher: DataFetcher<InputStream>,
|
||||
override fun loadData(priority: Priority): InputStream? {
|
||||
if (manga.favorite) {
|
||||
if (!file.exists()) {
|
||||
file.parentFile.mkdirs()
|
||||
networkFetcher.loadData(priority)?.let {
|
||||
networkFetcher.loadData(priority)?.let { input ->
|
||||
val atomicFile = AtomicFile(file)
|
||||
val output = atomicFile.startWrite()
|
||||
try {
|
||||
it.use { input ->
|
||||
file.outputStream().use { output ->
|
||||
input.copyTo(output)
|
||||
}
|
||||
}
|
||||
input.use { it.copyTo(output) }
|
||||
atomicFile.finishWrite(output)
|
||||
} catch (e: Exception) {
|
||||
file.delete()
|
||||
atomicFile.failWrite(output)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user