Applying changed Download routines
This commit is contained in:
@@ -78,16 +78,14 @@ class Cache(context: Context) : ContextWrapper(context) {
|
||||
|
||||
@UseExperimental(ImplicitReflectionSerializer::class)
|
||||
fun setCachedMetadata(galleryID: Int, metadata: Metadata) {
|
||||
val file = File(getCachedGallery(galleryID), ".metadata")
|
||||
val file = File(getCachedGallery(galleryID) ?: File(cacheDir, "imageCache/$galleryID"), ".metadata")
|
||||
|
||||
if (!file.exists())
|
||||
return
|
||||
if (file.parentFile?.exists() != true)
|
||||
file.parentFile?.mkdirs()
|
||||
|
||||
try {
|
||||
file.writeText(Json.stringify(metadata))
|
||||
} catch (e: Exception) {
|
||||
file.createNewFile()
|
||||
|
||||
}
|
||||
file.writeText(Json.stringify(metadata))
|
||||
}
|
||||
|
||||
suspend fun getThumbnail(galleryID: Int): String? {
|
||||
@@ -135,6 +133,16 @@ class Cache(context: Context) : ContextWrapper(context) {
|
||||
return galleryBlock
|
||||
}
|
||||
|
||||
fun getReaderOrNull(galleryID: Int): Reader? {
|
||||
val metadata = getCachedMetadata(galleryID)
|
||||
|
||||
val mirrors = preference.getString("mirrors", "")!!.split('>')
|
||||
|
||||
return metadata?.readers?.firstOrNull {
|
||||
mirrors.contains(it.code.name)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getReader(galleryID: Int): Reader? {
|
||||
val metadata = getCachedMetadata(galleryID)
|
||||
|
||||
@@ -173,7 +181,7 @@ class Cache(context: Context) : ContextWrapper(context) {
|
||||
gallery.listFiles { file ->
|
||||
file.nameWithoutExtension.toIntOrNull() != null
|
||||
}?.forEach {
|
||||
append(it.nameWithoutExtension.toInt(), it)
|
||||
put(it.nameWithoutExtension.toInt(), it)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -197,14 +205,17 @@ class Cache(context: Context) : ContextWrapper(context) {
|
||||
}
|
||||
|
||||
fun moveToDownload(galleryID: Int) {
|
||||
val cache = getCachedGallery(galleryID) ?: File(cacheDir, "imageCache/$galleryID")
|
||||
val cache = getCachedGallery(galleryID)
|
||||
|
||||
val download = getDownloadDirectory(this)
|
||||
if (cache != null) {
|
||||
val download = getDownloadDirectory(this)
|
||||
|
||||
if (!download.isParentOf(cache)) {
|
||||
cache.copyRecursively(download)
|
||||
cache.deleteRecursively()
|
||||
}
|
||||
if (!download.isParentOf(cache)) {
|
||||
cache.copyRecursively(download, true)
|
||||
cache.deleteRecursively()
|
||||
}
|
||||
} else
|
||||
File(getDownloadDirectory(this), galleryID.toString()).mkdirs()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -49,8 +49,8 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
|
||||
override fun update(tag: Any?, bytesRead: Long, contentLength: Long, done: Boolean) {
|
||||
val (galleryID, index) = (tag as? Pair<Int, Int>) ?: return
|
||||
|
||||
if (!done && progress[galleryID]!![index] != Float.POSITIVE_INFINITY)
|
||||
progress[galleryID]!![index] = bytesRead * 100F / contentLength
|
||||
if (!done && progress[galleryID]?.get(index)?.isFinite() == true)
|
||||
progress[galleryID]?.set(index, bytesRead * 100F / contentLength)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,14 +156,20 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
|
||||
.build()
|
||||
|
||||
fun stop() {
|
||||
queue.clear()
|
||||
|
||||
loop.cancel()
|
||||
for (i in 0..worker.size())
|
||||
worker[worker.keyAt(i)]?.cancel()
|
||||
|
||||
client.dispatcher.cancelAll()
|
||||
|
||||
progress.clear()
|
||||
exception.clear()
|
||||
}
|
||||
|
||||
fun cancel(galleryID: Int) {
|
||||
queue.remove(galleryID)
|
||||
worker[galleryID]?.cancel()
|
||||
|
||||
client.dispatcher.queuedCalls()
|
||||
@@ -171,6 +177,13 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
|
||||
.forEach {
|
||||
it.cancel()
|
||||
}
|
||||
|
||||
if (progress.indexOfKey(galleryID) >= 0) {
|
||||
progress.remove(galleryID)
|
||||
exception.remove(galleryID)
|
||||
|
||||
nRunners--
|
||||
}
|
||||
}
|
||||
|
||||
private fun queueDownload(galleryID: Int, reader: Reader, index: Int, callback: Callback) {
|
||||
@@ -179,7 +192,7 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
|
||||
|
||||
//Cache exists :P
|
||||
cache?.get(index)?.let {
|
||||
progress[galleryID]!![index] = Float.POSITIVE_INFINITY
|
||||
progress[galleryID]?.set(index, Float.POSITIVE_INFINITY)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -231,11 +244,15 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
|
||||
if (Fabric.isInitialized())
|
||||
Crashlytics.logException(e)
|
||||
|
||||
progress[galleryID]!![i] = Float.NaN
|
||||
exception[galleryID]!![i] = e
|
||||
progress[galleryID]?.set(i, Float.NaN)
|
||||
exception[galleryID]?.set(i, e)
|
||||
|
||||
if (progress[galleryID]?.all { !it.isFinite() } == true) {
|
||||
progress.remove(galleryID)
|
||||
exception.remove(galleryID)
|
||||
|
||||
if (progress[galleryID]!!.all { !it.isFinite() })
|
||||
nRunners--
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
@@ -245,11 +262,15 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
|
||||
call.request().url.encodedPath.split('.').last()
|
||||
|
||||
Cache(this@DownloadWorker).putImage(galleryID, "$i.$ext", res)
|
||||
progress[galleryID]!![i] = Float.POSITIVE_INFINITY
|
||||
progress[galleryID]?.set(i, Float.POSITIVE_INFINITY)
|
||||
}
|
||||
|
||||
if (progress[galleryID]!!.all { !it.isFinite() })
|
||||
if (progress[galleryID]?.all { !it.isFinite() } == true) {
|
||||
progress.remove(galleryID)
|
||||
exception.remove(galleryID)
|
||||
|
||||
nRunners--
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -64,4 +64,4 @@ fun URL.download(to: File, onDownloadProgress: ((Long, Long) -> Unit)? = null) {
|
||||
}
|
||||
}
|
||||
|
||||
fun File.isParentOf(file: File) = file.absolutePath.startsWith(this.absolutePath)
|
||||
fun File.isParentOf(file: File?) = file?.absolutePath?.startsWith(this.absolutePath) ?: false
|
||||
Reference in New Issue
Block a user