Fixed crash when download folder is changed

This commit is contained in:
tom5079
2020-09-02 12:15:37 +09:00
parent 2b8facfb97
commit 861994e804
10 changed files with 96 additions and 59 deletions

View File

@@ -275,13 +275,6 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
if (e.message?.contains("cancel", true) != false)
return
Log.i("PUPILD", "FAIL ${call.request().tag()} (${e.message})")
FirebaseCrashlytics.getInstance().apply {
log("FAIL ${call.request().tag()} (${e.message})")
setCustomKey("POS", "FAIL")
recordException(e)
}
cancel(galleryID)
queue.add(galleryID)
}

View File

@@ -20,7 +20,6 @@ package xyz.quaver.pupil.util.downloader
import android.content.Context
import android.content.ContextWrapper
import android.util.Log
import android.util.SparseArray
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -78,7 +77,7 @@ class Cache private constructor(context: Context, val galleryID: Int) : ContextW
}.getOrNull() ?: Metadata()
val downloadFolder: FileX?
get() = DownloadFolderManager.getInstance(this).getDownloadFolder(galleryID)
get() = DownloadManager.getInstance(this).getDownloadFolder(galleryID)
val cacheFolder: FileX
get() = FileX(this, cacheDir, "imageCache/$galleryID").also {
@@ -101,7 +100,6 @@ class Cache private constructor(context: Context, val galleryID: Int) : ContextW
kotlin.runCatching {
if (!file.exists()) {
Log.i("PUPILD", "$file")
file.createNewFile()
}
file.writeText(Json.encodeToString(metadata))
@@ -202,7 +200,6 @@ class Cache private constructor(context: Context, val galleryID: Int) : ContextW
@Suppress("BlockingMethodInNonBlockingContext")
fun moveToDownload() = CoroutineScope(Dispatchers.IO).launch {
val downloadFolder = downloadFolder ?: return@launch
Log.i("PUPILD", "MOVING $galleryID")
metadata.imageList?.forEach { imageName ->
imageName ?: return@forEach

View File

@@ -32,14 +32,14 @@ import xyz.quaver.pupil.services.DownloadService
import xyz.quaver.pupil.util.Preferences
import xyz.quaver.pupil.util.formatDownloadFolder
class DownloadFolderManager private constructor(context: Context) : ContextWrapper(context) {
class DownloadManager private constructor(context: Context) : ContextWrapper(context) {
companion object {
@Volatile private var instance: DownloadFolderManager? = null
@Volatile private var instance: DownloadManager? = null
fun getInstance(context: Context) =
instance ?: synchronized(this) {
instance ?: DownloadFolderManager(context).also { instance = it }
instance ?: DownloadManager(context).also { instance = it }
}
}
@@ -55,22 +55,34 @@ class DownloadFolderManager private constructor(context: Context) : ContextWrapp
}
}.invoke()
val downloadFolderMap: MutableMap<Int, String> = {
val file = downloadFolder.getChild(".download")
private var prevDownloadFolder: FileX? = null
private var downloadFolderMapInstance: MutableMap<Int, String>? = null
val downloadFolderMap: MutableMap<Int, String>
@Synchronized
get() {
if (prevDownloadFolder != downloadFolder) {
prevDownloadFolder = downloadFolder
downloadFolderMapInstance = {
val file = downloadFolder.getChild(".download")
val data = if (file.exists())
kotlin.runCatching {
file.readText()?.let { Json.decodeFromString<MutableMap<Int, String>>(it) }
}.onFailure { file.delete() }.getOrNull()
else
null
val data = if (file.exists())
kotlin.runCatching {
file.readText()?.let { Json.decodeFromString<MutableMap<Int, String>>(it) }
}.onFailure { file.delete() }.getOrNull()
else
null
data ?: {
file.createNewFile()
file.writeText("{}")
mutableMapOf<Int, String>()
}.invoke()
}.invoke()
}
return downloadFolderMapInstance!!
}
data ?: {
file.createNewFile()
file.writeText("{}")
mutableMapOf<Int, String>()
}.invoke()
}.invoke()
@Synchronized
fun isDownloading(galleryID: Int): Boolean {
@@ -90,7 +102,7 @@ class DownloadFolderManager private constructor(context: Context) : ContextWrapp
return
val name = runBlocking {
Cache.getInstance(this@DownloadFolderManager, galleryID).getGalleryBlock()
Cache.getInstance(this@DownloadManager, galleryID).getGalleryBlock()
}?.formatDownloadFolder() ?: return
val folder = downloadFolder.getChild(name)
@@ -98,7 +110,7 @@ class DownloadFolderManager private constructor(context: Context) : ContextWrapp
if (!folder.exists())
folder.mkdirs()
downloadFolderMap[galleryID] = name
downloadFolderMap[galleryID] = folder.name
downloadFolder.getChild(".download").writeText(Json.encodeToString(downloadFolderMap))
}