Auto cache clean

This commit is contained in:
tom5079
2020-10-02 12:51:59 +09:00
parent 87fdbdbb6e
commit 2b67858453
13 changed files with 127 additions and 52 deletions

View File

@@ -61,9 +61,18 @@ class GalleryBlockAdapter(private val galleries: List<Int>) : RecyclerSwipeAdapt
var thin: Boolean = Preferences["thin"]
inner class GalleryViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
var update = true
private var galleryID: Int = 0
private fun updateProgress(context: Context, galleryID: Int) {
init {
CoroutineScope(Dispatchers.Main).launch {
while (updateAll) {
updateProgress(view.context)
delay(1000)
}
}
}
private fun updateProgress(context: Context) {
val cache = Cache.getInstance(context, galleryID)
CoroutineScope(Dispatchers.Main).launch {
@@ -116,9 +125,13 @@ class GalleryBlockAdapter(private val galleries: List<Int>) : RecyclerSwipeAdapt
}
fun bind(galleryID: Int) {
this.galleryID = galleryID
val cache = Cache.getInstance(view.context, galleryID)
val galleryBlock = cache.metadata.galleryBlock ?: return
val galleryBlock = runBlocking {
cache.getGalleryBlock()
} ?: return
with(view) {
val resources = context.resources
@@ -162,13 +175,6 @@ class GalleryBlockAdapter(private val galleries: List<Int>) : RecyclerSwipeAdapt
}
}
CoroutineScope(Dispatchers.Main).launch {
while (updateAll && update) {
updateProgress(context, galleryID)
delay(1000)
}
}
galleryblock_title.text = galleryBlock.title
with(galleryblock_artist) {
text = artists.joinToString { it.wordCapitalize() }
@@ -377,13 +383,6 @@ class GalleryBlockAdapter(private val galleries: List<Int>) : RecyclerSwipeAdapt
}
}
override fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder) {
super.onViewDetachedFromWindow(holder)
if (holder is GalleryViewHolder)
holder.update = false
}
override fun getItemCount() =
galleries.size +
(if (showNext) 1 else 0) +

View File

@@ -62,7 +62,14 @@ class ReaderAdapter(
class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
fun clear() {
view.image.ssiv?.recycle()
view.image.mainView.let {
when (it) {
is SubsamplingScaleImageView ->
it.recycle()
is SimpleDraweeView ->
it.controller = null
}
}
}
}

View File

@@ -42,6 +42,7 @@ import xyz.quaver.pupil.R
import xyz.quaver.pupil.client
import xyz.quaver.pupil.interceptors
import xyz.quaver.pupil.ui.ReaderActivity
import xyz.quaver.pupil.util.cleanCache
import xyz.quaver.pupil.util.downloader.Cache
import xyz.quaver.pupil.util.downloader.DownloadManager
import xyz.quaver.pupil.util.ellipsize
@@ -295,6 +296,8 @@ class DownloadService : Service() {
}
fun download(galleryID: Int, priority: Boolean = false, startId: Int? = null): Job = CoroutineScope(Dispatchers.IO).launch {
cleanCache(this@DownloadService)
if (progress.containsKey(galleryID))
cancel(galleryID)

View File

@@ -31,6 +31,7 @@ import xyz.quaver.io.util.deleteRecursively
import xyz.quaver.pupil.R
import xyz.quaver.pupil.histories
import xyz.quaver.pupil.util.byteToString
import xyz.quaver.pupil.util.downloader.Cache
import xyz.quaver.pupil.util.downloader.DownloadManager
import java.io.File
@@ -61,6 +62,8 @@ class ManageStorageFragment : PreferenceFragmentCompat(), Preference.OnPreferenc
if (dir.exists())
dir.deleteRecursively()
Cache.instances.clear()
summary = context.getString(R.string.settings_storage_usage, byteToString(0))
CoroutineScope(Dispatchers.IO).launch {
var size = 0L

View File

@@ -186,7 +186,7 @@ class Cache private constructor(context: Context, val galleryID: Int) : ContextW
}
fun getImage(index: Int): FileX? =
metadata.imageList?.get(index)?.let { findFile(it) }
metadata.imageList?.getOrNull(index)?.let { findFile(it) }
@Suppress("BlockingMethodInNonBlockingContext")
fun putImage(index: Int, fileName: String, data: ByteArray) {

View File

@@ -19,35 +19,63 @@
package xyz.quaver.pupil.util
import android.content.Context
import android.os.storage.StorageManager
import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import xyz.quaver.pupil.histories
import xyz.quaver.pupil.util.downloader.Cache
import xyz.quaver.pupil.util.downloader.DownloadManager
import java.io.File
import java.io.FileOutputStream
import java.lang.reflect.Array
import java.net.URL
@Suppress("DEPRECATION")
@Deprecated("Use downloader.Cache instead")
fun getCachedGallery(context: Context, galleryID: Int) =
File(getDownloadDirectory(context), galleryID.toString()).let {
if (it.exists())
it
else
File(context.cacheDir, "imageCache/$galleryID")
val mutex = Mutex()
fun cleanCache(context: Context) = CoroutineScope(Dispatchers.IO).launch {
if (mutex.isLocked) return@launch
mutex.withLock {
val cacheFolder = File(context.cacheDir, "imageCache")
val downloadManager = DownloadManager.getInstance(context)
cacheFolder.listFiles { file ->
val galleryID = file.name.toIntOrNull() ?: return@listFiles true
!(downloadManager.downloadFolderMap.containsKey(galleryID) || histories.contains(galleryID))
}?.forEach {
it.deleteRecursively()
}
DownloadManager.getInstance(context).downloadFolderMap.keys.forEach {
val folder = File(cacheFolder, it.toString())
if (!downloadManager.isDownloading(it) && folder.exists()) {
folder.deleteRecursively()
}
}
val limit = (Preferences.get<String>("cache_limit").toLongOrNull() ?: 0L)*1024*1024*1024
if (limit == 0L) return@withLock
val cacheSize = {
var size = 0L
cacheFolder.walk().forEach {
size += it.length()
}
size
}
if (cacheSize.invoke() > limit)
while (cacheSize.invoke() > limit/2) {
val caches = cacheFolder.list() ?: return@withLock
(histories.firstOrNull {
caches.contains(it.toString()) && !downloadManager.isDownloading(it)
} ?: return@withLock).let {
Cache.delete(it)
}
}
}
@Suppress("DEPRECATION")
@Deprecated("Use downloader.Cache instead")
fun getDownloadDirectory(context: Context) =
Preferences.get<String>("dl_location").let {
if (it.isNotEmpty() && !it.startsWith("content"))
File(it)
else
context.getExternalFilesDir(null)!!
}
@Suppress("DEPRECATION")
@Deprecated("Use FileX instead")
fun File.isParentOf(another: File) =
another.absolutePath.startsWith(this.absolutePath)
}