2018-12-21 04:09:07 +00:00
|
|
|
package com.adlerosn.brasilfurfest.schedule.managers
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import com.adlerosn.brasilfurfest.helper.joinFile
|
2018-12-28 04:26:22 +00:00
|
|
|
import com.adlerosn.brasilfurfest.helper.lastPathPart
|
2018-12-21 04:09:07 +00:00
|
|
|
import java.io.File
|
|
|
|
import java.io.InputStream
|
|
|
|
import java.io.Serializable
|
|
|
|
|
|
|
|
class CacheManager(context: Context): Serializable {
|
|
|
|
private val cacheLocation = File(context.filesDir, "cache_data")
|
|
|
|
.also {
|
|
|
|
if (!it.isDirectory) {
|
|
|
|
it.mkdirs()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private val cacheStamps = File(context.filesDir, "cache_stamps")
|
|
|
|
.also {
|
|
|
|
if (!it.isDirectory) {
|
|
|
|
it.mkdirs()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 04:26:22 +00:00
|
|
|
init {
|
|
|
|
sanitize()
|
|
|
|
}
|
|
|
|
|
2018-12-21 04:09:07 +00:00
|
|
|
fun getStamp(file: String): String?{
|
2018-12-28 04:26:22 +00:00
|
|
|
knownNeeded?.add(file)
|
2018-12-21 04:09:07 +00:00
|
|
|
val cachedFile = cacheLocation.joinFile(file)
|
|
|
|
val cachedData = cacheStamps.joinFile(file)
|
|
|
|
if(!cachedFile.exists() || !cachedData.exists()) return null
|
|
|
|
return cachedData.readText().let { if(it.isBlank()) null else it }
|
|
|
|
}
|
|
|
|
|
|
|
|
operator fun get(file: String): Pair<InputStream, String>?{
|
2018-12-28 04:26:22 +00:00
|
|
|
knownNeeded?.add(file)
|
2018-12-21 04:09:07 +00:00
|
|
|
val cachedFile = cacheLocation.joinFile(file)
|
|
|
|
val cachedData = cacheStamps.joinFile(file)
|
|
|
|
if(!cachedFile.exists() || !cachedData.exists()) return null
|
|
|
|
val stamp = cachedData.readText()
|
|
|
|
if(stamp.isBlank()) return null
|
|
|
|
return Pair(
|
|
|
|
cachedFile.inputStream(),
|
|
|
|
stamp
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
operator fun set(file: String, requestResult: Pair<ByteArray, String>){
|
|
|
|
if (requestResult.second.isBlank()) return
|
2018-12-28 04:26:22 +00:00
|
|
|
knownNeeded?.add(file)
|
2018-12-21 04:09:07 +00:00
|
|
|
val cachedFile = cacheLocation.joinFile(file)
|
|
|
|
val cachedData = cacheStamps.joinFile(file)
|
|
|
|
cachedData.writeText(requestResult.second)
|
|
|
|
cachedFile.writeBytes(requestResult.first)
|
|
|
|
}
|
2018-12-28 04:26:22 +00:00
|
|
|
|
|
|
|
fun delete(file: String): Boolean {
|
|
|
|
val cachedFile = cacheLocation.joinFile(file)
|
|
|
|
val cachedData = cacheStamps.joinFile(file)
|
|
|
|
return (if (cachedFile.exists()) cachedFile.delete() else false) and (if (cachedData.exists()) cachedData.delete() else false)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun sanitize(): Int{
|
|
|
|
val datas = cacheLocation.listFiles().toList().map { it.absolutePath.lastPathPart() }
|
|
|
|
val stmps = cacheStamps.listFiles().toList().map { it.absolutePath.lastPathPart() }
|
|
|
|
val unreliableEntries = (datas-stmps) + (stmps-datas)
|
|
|
|
return unreliableEntries.map { delete(it) }.size
|
|
|
|
}
|
|
|
|
|
|
|
|
val files: Set<String>
|
|
|
|
get() {
|
|
|
|
val datas = cacheLocation.listFiles().toList().map { it.absolutePath.lastPathPart() }
|
|
|
|
val stmps = cacheStamps.listFiles().toList().map { it.absolutePath.lastPathPart() }
|
|
|
|
return datas.intersect(stmps)
|
|
|
|
}
|
|
|
|
|
|
|
|
private var knownNeeded: MutableList<String>? = null
|
|
|
|
fun startMonitoringNeededFiles(){ knownNeeded = mutableListOf() }
|
|
|
|
fun removeNotNeededFiles(): Int{
|
|
|
|
sanitize()
|
|
|
|
if(knownNeeded==null) return 0
|
|
|
|
val notNeeded = files - knownNeeded!!
|
|
|
|
knownNeeded = null
|
|
|
|
return notNeeded.map { delete(it) }.size
|
|
|
|
}
|
2018-12-21 04:09:07 +00:00
|
|
|
}
|