music: make file tree cache configurable

This commit is contained in:
Alexander Capehart
2025-06-22 16:45:39 -06:00
parent 078718fadb
commit 23615689bd
6 changed files with 83 additions and 3 deletions
@@ -29,6 +29,7 @@ import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
import org.oxycblt.auxio.image.covers.SettingCovers
import org.oxycblt.auxio.music.MusicRepository.IndexingWorker
import org.oxycblt.auxio.music.shim.NoOpFileTreeCache
import org.oxycblt.auxio.music.shim.WriteOnlyFileTreeCache
import org.oxycblt.auxio.music.shim.WriteOnlyMutableCache
import org.oxycblt.musikr.IndexingProgress
@@ -396,8 +397,12 @@ constructor(
val cache = if (withCache) cache else WriteOnlyMutableCache(cache)
val covers = settingCovers.mutate(context, newRevision)
val fileTreeCache =
if (withCache) FileTreeCache.from(context)
else WriteOnlyFileTreeCache(FileTreeCache.from(context))
if (musicSettings.useFileTreeCache) {
if (withCache) FileTreeCache.from(context)
else WriteOnlyFileTreeCache(FileTreeCache.from(context))
} else {
NoOpFileTreeCache()
}
val query = Query(source = locations, exclude = excludedLocations)
val storage = Storage(cache, covers, storedPlaylists, fileTreeCache)
val interpretation = Interpretation(nameFactory, separators, withHidden)
@@ -51,6 +51,8 @@ interface MusicSettings : Settings<MusicSettings.Listener> {
var separators: String
/** Whether to enable more advanced sorting by articles and numbers. */
val intelligentSorting: Boolean
/** Whether to use the file-system cache for improved loading times. */
val useFileTreeCache: Boolean
interface Listener {
/** Called when the current music locations changed. */
@@ -133,6 +135,9 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont
override val intelligentSorting: Boolean
get() = sharedPreferences.getBoolean(getString(R.string.set_key_auto_sort_names), true)
override val useFileTreeCache: Boolean
get() = sharedPreferences.getBoolean(getString(R.string.set_key_fs_cache), true)
override fun onSettingChanged(key: String, listener: MusicSettings.Listener) {
// TODO: Differentiate "hard reloads" (Need the cache) and "Soft reloads"
// (just need to manipulate data)
@@ -145,7 +150,8 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont
getString(R.string.set_key_separators),
getString(R.string.set_key_auto_sort_names),
getString(R.string.set_key_with_hidden),
getString(R.string.set_key_exclude_non_music) -> {
getString(R.string.set_key_exclude_non_music),
getString(R.string.set_key_fs_cache) -> {
L.d("Dispatching indexing setting change for $key")
listener.onIndexingSettingChanged()
}
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2025 Auxio Project
* NoOpFileTreeCache.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.music.shim
import android.net.Uri
import org.oxycblt.musikr.fs.device.CachedDirectory
import org.oxycblt.musikr.fs.device.CachedFile
import org.oxycblt.musikr.fs.device.FileTree
import org.oxycblt.musikr.fs.device.FileTreeCache
/**
* A no-op implementation of [FileTreeCache] that doesn't cache anything. This implementation always
* returns cache misses and discards all updates.
*/
class NoOpFileTreeCache : FileTreeCache {
override fun read(): FileTree = NoOpFileTree()
}
/**
* A no-op implementation of [FileTree] that doesn't cache anything. All queries return null (cache
* miss) and all updates are discarded.
*/
class NoOpFileTree : FileTree {
override suspend fun queryDirectory(uri: Uri): CachedDirectory? {
// Always return null to simulate a cache miss
return null
}
override suspend fun updateDirectory(uri: Uri, directory: CachedDirectory) {
// Do nothing - discard the update
}
override suspend fun queryFile(uri: Uri): CachedFile? {
// Always return null to simulate a cache miss
return null
}
override suspend fun updateFile(uri: Uri, file: CachedFile) {
// Do nothing - discard the update
}
override suspend fun write() {
// Do nothing - no cache to write
}
}