music: add excluded folders configuration
Co-authored by Claude.
This commit is contained in:
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,9 @@
|
||||
<action
|
||||
android:id="@+id/music_locations_settings"
|
||||
app:destination="@id/music_locations_dialog" />
|
||||
<action
|
||||
android:id="@+id/excluded_locations_settings"
|
||||
app:destination="@id/excluded_locations_dialog" />
|
||||
</fragment>
|
||||
|
||||
<fragment
|
||||
@@ -99,6 +102,11 @@
|
||||
android:name="org.oxycblt.auxio.music.interpret.SeparatorsDialog"
|
||||
android:label="separators_dialog"
|
||||
tools:layout="@layout/dialog_separators" />
|
||||
<dialog
|
||||
android:id="@+id/excluded_locations_dialog"
|
||||
android:name="org.oxycblt.auxio.music.locations.ExcludedLocationsDialog"
|
||||
android:label="excluded_locations_dialog"
|
||||
tools:layout="@layout/dialog_music_locations" />
|
||||
|
||||
<fragment
|
||||
android:id="@+id/about_fragment"
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
<string name="set_key_rescan" translatable="false">auxio_rescan</string>
|
||||
<string name="set_key_observing" translatable="false">auxio_observing</string>
|
||||
<string name="set_key_music_dirs" translatable="false">auxio_music_dirs</string>
|
||||
<string name="set_key_excluded_dirs" translatable="false">auxio_excluded_dirs</string>
|
||||
<string name="set_key_cover_mode" translatable="false">auxio_cover_mode2</string>
|
||||
<string name="set_key_square_covers" translatable="false">auxio_square_covers</string>
|
||||
<string name="set_key_exclude_non_music" translatable="false">auxio_exclude_non_music</string>
|
||||
|
||||
@@ -315,6 +315,8 @@
|
||||
<string name="set_locations">Music folders</string>
|
||||
<string name="set_locations_desc">Manage where music should be loaded from</string>
|
||||
<string name="set_locations_new">New folder</string>
|
||||
<string name="set_excluded_locations">Excluded folders</string>
|
||||
<string name="set_excluded_locations_desc">Manage folders to exclude from music loading</string>
|
||||
<string name="set_reindex">Refresh music</string>
|
||||
<string name="set_reindex_desc">Reload the music library, using cached tags when possible</string>
|
||||
<!-- Different from "Reload music" -->
|
||||
|
||||
@@ -33,6 +33,11 @@
|
||||
app:summary="@string/set_locations_desc"
|
||||
app:title="@string/set_locations" />
|
||||
|
||||
<org.oxycblt.auxio.settings.ui.WrappedDialogPreference
|
||||
app:key="@string/set_key_excluded_dirs"
|
||||
app:summary="@string/set_excluded_locations_desc"
|
||||
app:title="@string/set_excluded_locations" />
|
||||
|
||||
<Preference
|
||||
app:key="@string/set_key_reindex"
|
||||
app:summary="@string/set_reindex_desc"
|
||||
|
||||
Reference in New Issue
Block a user