music: integrate filetreecache
This commit is contained in:
@@ -21,6 +21,7 @@ package org.oxycblt.musikr
|
||||
import org.oxycblt.musikr.cache.MutableCache
|
||||
import org.oxycblt.musikr.covers.Cover
|
||||
import org.oxycblt.musikr.covers.MutableCovers
|
||||
import org.oxycblt.musikr.fs.device.FileTreeCache
|
||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||
import org.oxycblt.musikr.tag.interpret.Naming
|
||||
import org.oxycblt.musikr.tag.interpret.Separators
|
||||
@@ -45,7 +46,12 @@ data class Storage(
|
||||
* be used during music loading and mutated when creating, renaming, or deleting playlists in
|
||||
* the library.
|
||||
*/
|
||||
val storedPlaylists: StoredPlaylists
|
||||
val storedPlaylists: StoredPlaylists,
|
||||
|
||||
/**
|
||||
* A cache for file tree information to optimize file system exploration.
|
||||
*/
|
||||
val fileTreeCache: FileTreeCache
|
||||
)
|
||||
|
||||
/** Configuration for how to interpret and extrapolate certain audio tags. */
|
||||
|
||||
@@ -31,6 +31,10 @@ import kotlinx.serialization.json.Json
|
||||
|
||||
interface FileTreeCache {
|
||||
fun read(): FileTree
|
||||
|
||||
companion object {
|
||||
fun from(context: Context): FileTreeCache = FileTreeCacheImpl(context)
|
||||
}
|
||||
}
|
||||
|
||||
interface FileTree {
|
||||
@@ -47,7 +51,7 @@ interface FileTree {
|
||||
|
||||
// Define the sealed interface
|
||||
@Serializable
|
||||
sealed interface FileSystemEntry {
|
||||
private sealed interface FileSystemEntry {
|
||||
val uri: String
|
||||
val modifiedMs: Long
|
||||
}
|
||||
@@ -71,14 +75,13 @@ data class CachedFile(
|
||||
) : FileSystemEntry
|
||||
|
||||
@Serializable
|
||||
data class FileTreeData(
|
||||
private data class FileTreeData(
|
||||
val directories: Map<String, CachedDirectory> = mapOf(),
|
||||
val files: Map<String, CachedFile> = mapOf()
|
||||
)
|
||||
|
||||
class FileTreeCacheImpl(private val context: Context) : FileTreeCache {
|
||||
private class FileTreeCacheImpl(private val context: Context) : FileTreeCache {
|
||||
companion object {
|
||||
private const val TAG = "FileTreeCache"
|
||||
private const val CACHE_FILENAME = "file_tree_cache.json"
|
||||
private val json = Json {
|
||||
ignoreUnknownKeys = true
|
||||
@@ -98,17 +101,15 @@ class FileTreeCacheImpl(private val context: Context) : FileTreeCache {
|
||||
fileTreeCache.directories.toMutableMap(),
|
||||
fileTreeCache.files.toMutableMap())
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to read cache file", e)
|
||||
FileTreeImpl(context, mutableMapOf(), mutableMapOf())
|
||||
}
|
||||
} else {
|
||||
Log.i(TAG, "Cache file does not exist, creating new FileTree")
|
||||
FileTreeImpl(context, mutableMapOf(), mutableMapOf())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class FileTreeImpl(
|
||||
private class FileTreeImpl(
|
||||
private val context: Context,
|
||||
private val mutableDirectories: MutableMap<String, CachedDirectory> = mutableMapOf(),
|
||||
private val mutableFiles: MutableMap<String, CachedFile> = mutableMapOf()
|
||||
@@ -124,26 +125,22 @@ class FileTreeImpl(
|
||||
|
||||
// Directory operations
|
||||
override suspend fun queryDirectory(uri: Uri): CachedDirectory? {
|
||||
Log.d("FileTreeCache", "Querying directory: $uri ${mutableDirectories[uri.toString()]}")
|
||||
val uriString = uri.toString()
|
||||
return mutableDirectories[uriString]
|
||||
}
|
||||
|
||||
override suspend fun updateDirectory(uri: Uri, directory: CachedDirectory) {
|
||||
Log.d("FileTreeCache", "Updating directory: $uri")
|
||||
val uriString = uri.toString()
|
||||
mutableDirectories[uriString] = directory
|
||||
}
|
||||
|
||||
// File operations
|
||||
override suspend fun queryFile(uri: Uri): CachedFile? {
|
||||
Log.d("FileTreeCache", "Querying file: $uri")
|
||||
val uriString = uri.toString()
|
||||
return mutableFiles[uriString]
|
||||
}
|
||||
|
||||
override suspend fun updateFile(uri: Uri, file: CachedFile) {
|
||||
Log.d("FileTreeCache", "Updating file: $uri")
|
||||
val uriString = uri.toString()
|
||||
mutableFiles[uriString] = file
|
||||
}
|
||||
|
||||
@@ -44,7 +44,6 @@ import org.oxycblt.musikr.covers.Covers
|
||||
import org.oxycblt.musikr.fs.MusicLocation
|
||||
import org.oxycblt.musikr.fs.device.DeviceFS
|
||||
import org.oxycblt.musikr.fs.device.FileTreeCache
|
||||
import org.oxycblt.musikr.fs.device.FileTreeCacheImpl
|
||||
import org.oxycblt.musikr.fs.device.flatten
|
||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||
import org.oxycblt.musikr.playlist.m3u.M3U
|
||||
@@ -58,7 +57,7 @@ internal interface ExploreStep {
|
||||
DeviceFS.from(context, interpretation.withHidden),
|
||||
storage.cache,
|
||||
storage.covers,
|
||||
FileTreeCacheImpl(context),
|
||||
storage.fileTreeCache,
|
||||
storage.storedPlaylists)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user