music: reload when perms granted

This commit is contained in:
Alexander Capehart
2025-07-09 20:14:22 -06:00
parent 2a4417c82a
commit 0ba5ef4315
3 changed files with 56 additions and 11 deletions
@@ -64,6 +64,8 @@ interface MusicSettings : Settings<MusicSettings.Listener> {
/** Whether to use the file-system cache for improved loading times. */ /** Whether to use the file-system cache for improved loading times. */
val useFileTreeCache: Boolean val useFileTreeCache: Boolean
fun forceLocationUpdate()
interface Listener { interface Listener {
/** Called when the current music locations changed. */ /** Called when the current music locations changed. */
fun onMusicLocationsChanged() {} fun onMusicLocationsChanged() {}
@@ -188,12 +190,22 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont
} }
} }
override fun forceLocationUpdate() {
// TODO: Temporary!!! This is really dumb, just need to ship a workaround for this rn
val cur = sharedPreferences.getInt(getString(R.string.set_key_locations_mode), 0)
sharedPreferences.edit {
putInt(getString(R.string.set_key_force_reload_workaround), cur + 1)
apply()
}
}
override fun onSettingChanged(key: String, listener: MusicSettings.Listener) { override fun onSettingChanged(key: String, listener: MusicSettings.Listener) {
// TODO: Differentiate "hard reloads" (Need the cache) and "Soft reloads" // TODO: Differentiate "hard reloads" (Need the cache) and "Soft reloads"
// (just need to manipulate data) // (just need to manipulate data)
when (key) { when (key) {
getString(R.string.set_key_locations_mode), getString(R.string.set_key_locations_mode),
getString(R.string.set_key_music_locations) -> { getString(R.string.set_key_music_locations),
getString(R.string.set_key_force_reload_workaround) -> {
L.d("Dispatching music locations change") L.d("Dispatching music locations change")
listener.onMusicLocationsChanged() listener.onMusicLocationsChanged()
} }
@@ -86,6 +86,7 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
private var hasStoragePermission = false private var hasStoragePermission = false
private var isExtrasExpanded = false private var isExtrasExpanded = false
private var pendingLocationCallback: ((Location.Unopened) -> Unit)? = null private var pendingLocationCallback: ((Location.Unopened) -> Unit)? = null
private var permissionGrantedInSession = false
override fun onCreateBinding(inflater: LayoutInflater) = override fun onCreateBinding(inflater: LayoutInflater) =
DialogMusicLocationsBinding.inflate(inflater) DialogMusicLocationsBinding.inflate(inflater)
@@ -110,6 +111,9 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted -> registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
L.d("Storage permission granted: $isGranted") L.d("Storage permission granted: $isGranted")
hasStoragePermission = isGranted hasStoragePermission = isGranted
if (isGranted && !permissionGrantedInSession) {
permissionGrantedInSession = true
}
updateModeUI(binding) updateModeUI(binding)
updateSaveButtonState() updateSaveButtonState()
} }
@@ -507,33 +511,60 @@ class LocationsDialog : ViewBindingMaterialDialogFragment<DialogMusicLocationsBi
private fun saveChanges() { private fun saveChanges() {
val binding = requireBinding() val binding = requireBinding()
// Save the mode setting // Check if configuration has actually changed
musicSettings.locationMode = val currentMode = musicSettings.locationMode
if (isFilePickerMode) LocationMode.SAF else LocationMode.MEDIA_STORE val modeChanged =
currentMode != (if (isFilePickerMode) LocationMode.SAF else LocationMode.MEDIA_STORE)
var configChanged = modeChanged
if (isFilePickerMode) { if (isFilePickerMode) {
// File picker mode - create and save SAF query // Check if SAF query changed
val safQuery = val currentSafQuery = musicSettings.safQuery
val newSafQuery =
SAF.Query( SAF.Query(
source = includeLocationAdapter.locations, source = includeLocationAdapter.locations,
exclude = excludeLocationAdapter.locations, exclude = excludeLocationAdapter.locations,
withHidden = binding.locationsWithHiddenSwitch.isChecked) withHidden = binding.locationsWithHiddenSwitch.isChecked)
musicSettings.safQuery = safQuery
if (!modeChanged && currentMode == LocationMode.SAF) {
configChanged = currentSafQuery != newSafQuery
}
// Save the new SAF query
musicSettings.safQuery = newSafQuery
} else { } else {
// System database mode - create and save MediaStore query // Check if MediaStore query changed
val currentMediaStoreQuery = musicSettings.mediaStoreQuery
val filterMode = val filterMode =
if (isIncludeMode) { if (isIncludeMode) {
MediaStore.FilterMode.INCLUDE MediaStore.FilterMode.INCLUDE
} else { } else {
MediaStore.FilterMode.EXCLUDE MediaStore.FilterMode.EXCLUDE
} }
val newMediaStoreQuery =
val mediaStoreQuery =
MediaStore.Query( MediaStore.Query(
mode = filterMode, mode = filterMode,
filtered = filterLocationAdapter.locations, filtered = filterLocationAdapter.locations,
excludeNonMusic = binding.locationsExcludeNonMusicSwitch.isChecked) excludeNonMusic = binding.locationsExcludeNonMusicSwitch.isChecked)
musicSettings.mediaStoreQuery = mediaStoreQuery
if (!modeChanged && currentMode == LocationMode.MEDIA_STORE) {
configChanged = currentMediaStoreQuery != newMediaStoreQuery
}
// Save the new MediaStore query
musicSettings.mediaStoreQuery = newMediaStoreQuery
}
// Save the mode setting
musicSettings.locationMode =
if (isFilePickerMode) LocationMode.SAF else LocationMode.MEDIA_STORE
// If no configuration changed but permission was granted in this session,
// force a location update
if (!configChanged && permissionGrantedInSession) {
L.d("No config changes detected, but permission was granted - forcing location update")
musicSettings.forceLocationUpdate()
} }
} }
+2
View File
@@ -61,6 +61,8 @@
<string name="set_key_library_revision" translatable="false">auxio_library_revision</string> <string name="set_key_library_revision" translatable="false">auxio_library_revision</string>
<string name="set_key_force_reload_workaround" translatable="false">auxio_force_reload</string>
<string-array name="entries_theme"> <string-array name="entries_theme">
<item>@string/set_theme_auto</item> <item>@string/set_theme_auto</item>
<item>@string/set_theme_day</item> <item>@string/set_theme_day</item>