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 8571f2681..d1c57b8cf 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/MusicSettings.kt @@ -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() } diff --git a/app/src/main/java/org/oxycblt/auxio/music/locations/ExcludedLocationAdapter.kt b/app/src/main/java/org/oxycblt/auxio/music/locations/ExcludedLocationAdapter.kt new file mode 100644 index 000000000..754ce915a --- /dev/null +++ b/app/src/main/java/org/oxycblt/auxio/music/locations/ExcludedLocationAdapter.kt @@ -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 . + */ + +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() { + private val _locations = mutableListOf() + /** + * The current list of [Location]s, may not line up with [Location]s due to + * removals. + */ + val locations: List = _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) { + 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)) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/oxycblt/auxio/music/locations/ExcludedLocationsDialog.kt b/app/src/main/java/org/oxycblt/auxio/music/locations/ExcludedLocationsDialog.kt new file mode 100644 index 000000000..16eb9b4e3 --- /dev/null +++ b/app/src/main/java/org/oxycblt/auxio/music/locations/ExcludedLocationsDialog.kt @@ -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 . + */ + +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(), + ExcludedLocationAdapter.Listener, + NewLocationFooterAdapter.Listener { + private val locationAdapter = ExcludedLocationAdapter(this) + private val locationFooterAdapter = NewLocationFooterAdapter(this) + private var openDocumentTreeLauncher: ActivityResultLauncher? = 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" + } +} \ No newline at end of file diff --git a/app/src/main/java/org/oxycblt/auxio/settings/RootPreferenceFragment.kt b/app/src/main/java/org/oxycblt/auxio/settings/RootPreferenceFragment.kt index 39a523c0c..791ba5b53 100644 --- a/app/src/main/java/org/oxycblt/auxio/settings/RootPreferenceFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/settings/RootPreferenceFragment.kt @@ -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()) + } } } diff --git a/app/src/main/res/navigation/outer.xml b/app/src/main/res/navigation/outer.xml index 0d15cbc8a..4f75640ec 100644 --- a/app/src/main/res/navigation/outer.xml +++ b/app/src/main/res/navigation/outer.xml @@ -36,6 +36,9 @@ + + auxio_rescan auxio_observing auxio_music_dirs + auxio_excluded_dirs 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 0b5830db5..26d456242 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 + Excluded folders + Manage folders to exclude from music loading Refresh music Reload the music library, using cached tags when possible diff --git a/app/src/main/res/xml/preferences_root.xml b/app/src/main/res/xml/preferences_root.xml index 5c004cb4c..12f8fb94b 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" /> + +