musikr.fs: stream in file tree cache
This commit is contained in:
@@ -28,6 +28,8 @@ import kotlinx.serialization.Serializable
|
|||||||
import kotlinx.serialization.decodeFromString
|
import kotlinx.serialization.decodeFromString
|
||||||
import kotlinx.serialization.encodeToString
|
import kotlinx.serialization.encodeToString
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
|
import kotlinx.serialization.json.decodeFromStream
|
||||||
|
import kotlinx.serialization.json.encodeToStream
|
||||||
|
|
||||||
interface FileTreeCache {
|
interface FileTreeCache {
|
||||||
fun read(): FileTree
|
fun read(): FileTree
|
||||||
@@ -94,8 +96,7 @@ private class FileTreeCacheImpl(private val context: Context) : FileTreeCache {
|
|||||||
|
|
||||||
return if (cacheFile.exists()) {
|
return if (cacheFile.exists()) {
|
||||||
try {
|
try {
|
||||||
val jsonContent = cacheFile.readText()
|
val fileTreeCache = json.decodeFromStream<FileTreeData>(cacheFile.inputStream())
|
||||||
val fileTreeCache = json.decodeFromString<FileTreeData>(jsonContent)
|
|
||||||
FileTreeImpl(
|
FileTreeImpl(
|
||||||
context,
|
context,
|
||||||
fileTreeCache.directories.toMutableMap(),
|
fileTreeCache.directories.toMutableMap(),
|
||||||
@@ -153,9 +154,7 @@ private class FileTreeImpl(
|
|||||||
val fileTreeCache =
|
val fileTreeCache =
|
||||||
FileTreeData(
|
FileTreeData(
|
||||||
directories = mutableDirectories.toMap(), files = mutableFiles.toMap())
|
directories = mutableDirectories.toMap(), files = mutableFiles.toMap())
|
||||||
|
json.encodeToStream(fileTreeCache, cacheFile.outputStream())
|
||||||
val jsonContent = json.encodeToString(fileTreeCache)
|
|
||||||
cacheFile.writeText(jsonContent)
|
|
||||||
Log.d(TAG, "Successfully wrote cache to disk")
|
Log.d(TAG, "Successfully wrote cache to disk")
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "Failed to write cache file", e)
|
Log.e(TAG, "Failed to write cache file", e)
|
||||||
|
|||||||
@@ -74,4 +74,4 @@ internal inline fun <reified R> ContentResolver.useQuery(
|
|||||||
args: Array<String>? = null,
|
args: Array<String>? = null,
|
||||||
sortOrder: String? = null,
|
sortOrder: String? = null,
|
||||||
block: (Cursor) -> R
|
block: (Cursor) -> R
|
||||||
) = safeQuery(uri, projection, selector, args, sortOrder).use(block)
|
) = query(uri, projection, selector, args, sortOrder)?.use(block)
|
||||||
|
|||||||
Reference in New Issue
Block a user