From 23615689bd3b69252747f408240dc52d557dbc04 Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Sun, 22 Jun 2025 16:45:39 -0600 Subject: [PATCH] music: make file tree cache configurable --- .../oxycblt/auxio/music/MusicRepository.kt | 9 ++- .../org/oxycblt/auxio/music/MusicSettings.kt | 8 ++- .../auxio/music/shim/NoOpFileTreeCache.kt | 61 +++++++++++++++++++ app/src/main/res/values/settings.xml | 1 + app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/preferences_root.xml | 5 ++ 6 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/org/oxycblt/auxio/music/shim/NoOpFileTreeCache.kt diff --git a/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt b/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt index f83f202e9..8574205b2 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt @@ -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) diff --git a/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt b/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt index 8317f67d8..08ee7a3a6 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt @@ -51,6 +51,8 @@ interface MusicSettings : Settings { 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() } diff --git a/app/src/main/java/org/oxycblt/auxio/music/shim/NoOpFileTreeCache.kt b/app/src/main/java/org/oxycblt/auxio/music/shim/NoOpFileTreeCache.kt new file mode 100644 index 000000000..a28de5139 --- /dev/null +++ b/app/src/main/java/org/oxycblt/auxio/music/shim/NoOpFileTreeCache.kt @@ -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 . + */ + +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 + } +} diff --git a/app/src/main/res/values/settings.xml b/app/src/main/res/values/settings.xml index 86e8d255a..fd5d1857b 100644 --- a/app/src/main/res/values/settings.xml +++ b/app/src/main/res/values/settings.xml @@ -15,6 +15,7 @@ auxio_observing auxio_music_dirs auxio_excluded_dirs + auxio_fs_cache auxio_cover_mode2 auxio_square_covers auxio_exclude_non_music diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 26d456242..9111f1465 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -315,6 +315,8 @@ Music folders Manage where music should be loaded from New folder + Use file-system cache + Improves loading times, but can prevent new music from being added Excluded folders Manage folders to exclude from music loading Refresh music diff --git a/app/src/main/res/xml/preferences_root.xml b/app/src/main/res/xml/preferences_root.xml index 12f8fb94b..a45aa9142 100644 --- a/app/src/main/res/xml/preferences_root.xml +++ b/app/src/main/res/xml/preferences_root.xml @@ -33,6 +33,11 @@ app:summary="@string/set_locations_desc" app:title="@string/set_locations" /> + +