music: add excluded folders configuration

Co-authored by Claude.
This commit is contained in:
Alexander Capehart
2025-06-16 07:51:02 -06:00
parent bc18879a8e
commit 1479ec4703
8 changed files with 295 additions and 4 deletions
@@ -104,7 +104,8 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont
}
set(value) {
sharedPreferences.edit {
putString(getString(R.string.set_key_excluded_locations), value.stringifyLocations())
putString(
getString(R.string.set_key_excluded_locations), value.stringifyLocations())
commit()
listener?.onMusicLocationsChanged()
}
@@ -0,0 +1,124 @@
/*
* Copyright (c) 2021 Auxio Project
* ExcludedLocationAdapter.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.locations
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import org.oxycblt.auxio.databinding.ItemMusicLocationBinding
import org.oxycblt.auxio.list.recycler.DialogRecyclerView
import org.oxycblt.auxio.util.context
import org.oxycblt.auxio.util.inflater
import org.oxycblt.musikr.fs.Location
import timber.log.Timber as L
/**
* [RecyclerView.Adapter] that manages a list of [Location] excluded directory instances.
*
* @param listener A [ExcludedLocationAdapter.Listener] to bind interactions to.
* @author Alexander Capehart (OxygenCobalt)
*/
class ExcludedLocationAdapter(private val listener: Listener) : RecyclerView.Adapter<ExcludedDirViewHolder>() {
private val _locations = mutableListOf<Location>()
/**
* The current list of [Location]s, may not line up with [Location]s due to
* removals.
*/
val locations: List<Location> = _locations
override fun getItemCount() = locations.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
ExcludedDirViewHolder.from(parent)
override fun onBindViewHolder(holder: ExcludedDirViewHolder, position: Int) =
holder.bind(locations[position], listener)
/**
* Add a [Location] to the end of the list.
*
* @param location The [Location] to add.
*/
fun add(location: Location) {
if (_locations.contains(location)) return
L.d("Adding $location")
_locations.add(location)
notifyItemInserted(_locations.lastIndex)
}
/**
* Add a list of [Location] instances to the end of the list.
*
* @param locations The [Location] instances to add.
*/
fun addAll(locations: List<Location>) {
L.d("Adding ${locations.size} locations")
val oldLastIndex = _locations.size
_locations.addAll(locations)
notifyItemRangeInserted(oldLastIndex, locations.size)
}
/**
* Remove a [Location] from the list.
*
* @param location The [Location] to remove. Must exist in the list.
*/
fun remove(location: Location) {
L.d("Removing $location")
val idx = _locations.indexOf(location)
_locations.removeAt(idx)
notifyItemRemoved(idx)
}
/** A Listener for [ExcludedLocationAdapter] interactions. */
interface Listener {
/** Called when the delete button on a directory item is clicked. */
fun onRemoveLocation(location: Location)
}
}
/**
* A [RecyclerView.Recycler] that displays a [Location]. Use [from] to create an instance.
*
* @author Alexander Capehart (OxygenCobalt)
*/
class ExcludedDirViewHolder private constructor(private val binding: ItemMusicLocationBinding) :
DialogRecyclerView.ViewHolder(binding.root) {
/**
* Bind new data to this instance.
*
* @param location The new [Location] to bind.
* @param listener A [ExcludedLocationAdapter.Listener] to bind interactions to.
*/
fun bind(location: Location, listener: ExcludedLocationAdapter.Listener) {
binding.locationPath.text = location.path.resolve(binding.context)
binding.locationDelete.setOnClickListener { listener.onRemoveLocation(location) }
}
companion object {
/**
* Create a new instance.
*
* @param parent The parent to inflate this instance from.
* @return A new instance.
*/
fun from(parent: ViewGroup) =
ExcludedDirViewHolder(ItemMusicLocationBinding.inflate(parent.context.inflater, parent, false))
}
}
@@ -0,0 +1,144 @@
/*
* Copyright (c) 2021 Auxio Project
* ExcludedLocationsDialog.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.locations
import android.content.ActivityNotFoundException
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.core.net.toUri
import androidx.recyclerview.widget.ConcatAdapter
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import org.oxycblt.auxio.BuildConfig
import org.oxycblt.auxio.R
import org.oxycblt.auxio.databinding.DialogMusicLocationsBinding
import org.oxycblt.auxio.music.MusicSettings
import org.oxycblt.auxio.ui.ViewBindingMaterialDialogFragment
import org.oxycblt.auxio.util.showToast
import org.oxycblt.musikr.fs.Location
import timber.log.Timber as L
/**
* Dialog that manages the excluded locations setting.
*
* @author Alexander Capehart (OxygenCobalt)
*/
@AndroidEntryPoint
class ExcludedLocationsDialog :
ViewBindingMaterialDialogFragment<DialogMusicLocationsBinding>(),
ExcludedLocationAdapter.Listener,
NewLocationFooterAdapter.Listener {
private val locationAdapter = ExcludedLocationAdapter(this)
private val locationFooterAdapter = NewLocationFooterAdapter(this)
private var openDocumentTreeLauncher: ActivityResultLauncher<Uri?>? = null
@Inject lateinit var musicSettings: MusicSettings
override fun onCreateBinding(inflater: LayoutInflater) =
DialogMusicLocationsBinding.inflate(inflater)
override fun onConfigDialog(builder: AlertDialog.Builder) {
builder
.setTitle(R.string.set_excluded_locations)
.setNegativeButton(R.string.lbl_cancel, null)
.setPositiveButton(R.string.lbl_save) { _, _ ->
val newDirs = locationAdapter.locations
musicSettings.excludedLocations = newDirs
}
}
override fun onBindingCreated(
binding: DialogMusicLocationsBinding,
savedInstanceState: Bundle?
) {
openDocumentTreeLauncher =
registerForActivityResult(
ActivityResultContracts.OpenDocumentTree(), ::addDocumentTreeUriToDirs)
binding.locationsRecycler.apply {
adapter = ConcatAdapter(locationAdapter, locationFooterAdapter)
itemAnimator = null
}
val locations =
savedInstanceState?.getStringArrayList(KEY_PENDING_LOCATIONS)?.mapNotNull {
Location.from(requireContext(), it.toUri())
} ?: musicSettings.excludedLocations
locationAdapter.addAll(locations)
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putStringArrayList(
KEY_PENDING_LOCATIONS, ArrayList(locationAdapter.locations.map { it.uri.toString() }))
}
override fun onDestroyBinding(binding: DialogMusicLocationsBinding) {
super.onDestroyBinding(binding)
openDocumentTreeLauncher = null
binding.locationsRecycler.adapter = null
}
override fun onRemoveLocation(location: Location) {
locationAdapter.remove(location)
}
override fun onNewLocation() {
L.d("Opening launcher")
val launcher =
requireNotNull(openDocumentTreeLauncher) { "Document tree launcher was not available" }
try {
launcher.launch(null)
} catch (e: ActivityNotFoundException) {
// User doesn't have a capable file manager.
requireContext().showToast(R.string.err_no_app)
}
}
/**
* Add a Document Tree [Uri] chosen by the user to the current [Location]s.
*
* @param uri The document tree [Uri] to add, chosen by the user. Will do nothing if the [Uri]
* is null or not valid.
*/
private fun addDocumentTreeUriToDirs(uri: Uri?) {
if (uri == null) {
// A null URI means that the user left the file picker without picking a directory
L.d("No URI given (user closed the dialog)")
return
}
val context = requireContext()
val location = Location.from(context, uri)
if (location != null) {
locationAdapter.add(location)
} else {
requireContext().showToast(R.string.err_bad_location)
}
}
private companion object {
const val KEY_PENDING_LOCATIONS = BuildConfig.APPLICATION_ID + ".key.PENDING_EXCLUDED_LOCATIONS"
}
}
@@ -51,9 +51,15 @@ class RootPreferenceFragment : BasePreferenceFragment(R.xml.preferences_root) {
}
override fun onOpenDialogPreference(preference: WrappedDialogPreference) {
if (preference.key == getString(R.string.set_key_music_dirs)) {
findNavController()
.navigateSafe(RootPreferenceFragmentDirections.musicLocationsSettings())
when (preference.key) {
getString(R.string.set_key_music_dirs) -> {
findNavController()
.navigateSafe(RootPreferenceFragmentDirections.musicLocationsSettings())
}
getString(R.string.set_key_excluded_dirs) -> {
findNavController()
.navigateSafe(RootPreferenceFragmentDirections.excludedLocationsSettings())
}
}
}