idk wtf i'm doin'

This commit is contained in:
tom5079
2020-06-21 22:43:47 +09:00
parent 0bf2f1b6e1
commit 9f363d8900
8 changed files with 129 additions and 59 deletions

View File

@@ -34,8 +34,10 @@ import xyz.quaver.pupil.util.getCachedGallery
import xyz.quaver.pupil.util.getDownloadDirectory
import xyz.quaver.pupil.util.isParentOf
import xyz.quaver.pupil.util.json
import java.io.BufferedInputStream
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.net.URL
import java.util.*
import java.util.concurrent.locks.Lock
@@ -250,7 +252,7 @@ class Cache(context: Context) : ContextWrapper(context) {
}
fun putImage(galleryID: Int, index: Int, ext: String, data: ByteArray) {
fun putImage(galleryID: Int, index: Int, ext: String, data: InputStream) {
if (preference.getBoolean("cache_disable", false))
return
@@ -260,8 +262,10 @@ class Cache(context: Context) : ContextWrapper(context) {
}
try {
FileOutputStream(cache).use {
it.write(data)
BufferedInputStream(data).use { inputStream ->
FileOutputStream(cache).use { outputStream ->
inputStream.copyTo(outputStream)
}
}
} catch (e: Exception) {
cache.delete()

View File

@@ -143,7 +143,6 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
* null -> Download in progress / Loading
*/
val exception = SparseArray<MutableList<Throwable?>?>()
val results = SparseArray<MutableList<ByteArray?>?>()
val notification = SparseArray<NotificationCompat.Builder>()
private val loop = loop()
@@ -196,7 +195,6 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
progress.clear()
exception.clear()
results.clear()
notification.clear()
notificationManager.cancelAll()
}
@@ -213,7 +211,6 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
progress.remove(galleryID)
exception.remove(galleryID)
results.remove(galleryID)
notification.remove(galleryID)
notificationManager.cancel(galleryID)
@@ -262,7 +259,6 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
if (reader == null) {
progress.put(galleryID, null)
exception.put(galleryID, null)
results.put(galleryID, null)
Cache(this@DownloadWorker).setDownloading(galleryID, false)
return@launch
@@ -277,9 +273,6 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
0F
}.toMutableList())
exception.put(galleryID, reader.galleryInfo.files.map { null }.toMutableList())
results.put(galleryID, reader.galleryInfo.files.indices.map { index ->
cache?.firstOrNull { it?.nameWithoutExtension?.toIntOrNull() == index }?.readBytes()
}.toMutableList())
if (notification[galleryID] == null)
initNotification(galleryID)
@@ -329,19 +322,13 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
try {
response.body().use {
it!!
results[galleryID]?.set(i, it.source().readByteArray())
Cache(this@DownloadWorker).putImage(galleryID, i, ext, it!!.byteStream())
}
progress[galleryID]?.set(i, Float.POSITIVE_INFINITY)
notify(galleryID)
CoroutineScope(Dispatchers.IO).launch {
results[galleryID]?.get(i)?.also {
Cache(this@DownloadWorker).putImage(galleryID, i, ext, it)
}
if (isCompleted(galleryID)) {
with(Cache(this@DownloadWorker)) {
if (isDownloading(galleryID)) {

View File

@@ -319,7 +319,7 @@ fun importOldGalleries(context: Context, folder: File) = CoroutineScope(Dispatch
@Suppress("NAME_SHADOWING")
val index = it.nameWithoutExtension.toIntOrNull() ?: return@forEach
Cache(context).putImage(galleryID, index, it.extension, it.readBytes())
Cache(context).putImage(galleryID, index, it.extension, it.inputStream())
}
}