music: implement music locations dialog
This commit is contained in:
@@ -306,9 +306,7 @@ class HomeFragment :
|
||||
if (unit == null) {
|
||||
return
|
||||
}
|
||||
findNavController().navigateSafe(
|
||||
HomeFragmentDirections.chooseLocations()
|
||||
)
|
||||
findNavController().navigateSafe(HomeFragmentDirections.chooseLocations())
|
||||
homeModel.chooseMusicLocations.consume()
|
||||
}
|
||||
|
||||
|
||||
@@ -83,9 +83,7 @@ class AlbumListFragment :
|
||||
|
||||
binding.homeNoMusicMsg.text = getString(R.string.lng_no_albums)
|
||||
|
||||
binding.homeChooseMusicSources.setOnClickListener {
|
||||
homeModel.startChooseMusicLocations()
|
||||
}
|
||||
binding.homeChooseMusicSources.setOnClickListener { homeModel.startChooseMusicLocations() }
|
||||
|
||||
collectImmediately(homeModel.albumList, musicModel.indexingState, ::updateAlbums)
|
||||
collectImmediately(listModel.selected, ::updateSelection)
|
||||
|
||||
@@ -78,9 +78,7 @@ class ArtistListFragment :
|
||||
|
||||
binding.homeNoMusicMsg.text = getString(R.string.lng_no_artists)
|
||||
|
||||
binding.homeChooseMusicSources.setOnClickListener {
|
||||
homeModel.startChooseMusicLocations()
|
||||
}
|
||||
binding.homeChooseMusicSources.setOnClickListener { homeModel.startChooseMusicLocations() }
|
||||
|
||||
collectImmediately(homeModel.artistList, musicModel.indexingState, ::updateArtists)
|
||||
collectImmediately(listModel.selected, ::updateSelection)
|
||||
|
||||
@@ -77,9 +77,7 @@ class GenreListFragment :
|
||||
|
||||
binding.homeNoMusicMsg.text = getString(R.string.lng_no_genres)
|
||||
|
||||
binding.homeChooseMusicSources.setOnClickListener {
|
||||
homeModel.startChooseMusicLocations()
|
||||
}
|
||||
binding.homeChooseMusicSources.setOnClickListener { homeModel.startChooseMusicLocations() }
|
||||
|
||||
collectImmediately(homeModel.genreList, musicModel.indexingState, ::updateGenres)
|
||||
collectImmediately(listModel.selected, ::updateSelection)
|
||||
|
||||
@@ -75,9 +75,7 @@ class PlaylistListFragment :
|
||||
|
||||
binding.homeNoMusicMsg.text = getString(R.string.lng_no_playlists)
|
||||
|
||||
binding.homeChooseMusicSources.setOnClickListener {
|
||||
homeModel.startChooseMusicLocations()
|
||||
}
|
||||
binding.homeChooseMusicSources.setOnClickListener { homeModel.startChooseMusicLocations() }
|
||||
|
||||
collectImmediately(homeModel.playlistList, musicModel.indexingState, ::updatePlaylists)
|
||||
collectImmediately(listModel.selected, ::updateSelection)
|
||||
@@ -126,7 +124,8 @@ class PlaylistListFragment :
|
||||
|
||||
private fun updatePlaylists(playlists: List<Playlist>, indexingState: IndexingState?) {
|
||||
requireBinding().apply {
|
||||
homeRecycler.isInvisible = indexingState is IndexingState.Indexing || playlists.isEmpty()
|
||||
homeRecycler.isInvisible =
|
||||
indexingState is IndexingState.Indexing || playlists.isEmpty()
|
||||
homeNoMusic.isInvisible = playlists.isEmpty()
|
||||
}
|
||||
playlistAdapter.update(playlists, homeModel.playlistInstructions.consume())
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
* 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.home.list
|
||||
|
||||
import android.os.Bundle
|
||||
@@ -81,15 +81,12 @@ class SongListFragment :
|
||||
|
||||
binding.homeNoMusicMsg.text = getString(R.string.lng_no_songs)
|
||||
|
||||
binding.homeChooseMusicSources.setOnClickListener {
|
||||
homeModel.startChooseMusicLocations()
|
||||
}
|
||||
binding.homeChooseMusicSources.setOnClickListener { homeModel.startChooseMusicLocations() }
|
||||
|
||||
collectImmediately(homeModel.songList, musicModel.indexingState, ::updateSongs)
|
||||
collectImmediately(listModel.selected, ::updateSelection)
|
||||
collectImmediately(
|
||||
playbackModel.song, playbackModel.parent, playbackModel.isPlaying, ::updatePlayback
|
||||
)
|
||||
playbackModel.song, playbackModel.parent, playbackModel.isPlaying, ::updatePlayback)
|
||||
}
|
||||
|
||||
override fun onDestroyBinding(binding: FragmentHomeListBinding) {
|
||||
@@ -127,12 +124,11 @@ class SongListFragment :
|
||||
val dateAddedMillis = song.dateAdded.secsToMs()
|
||||
formatterSb.setLength(0)
|
||||
DateUtils.formatDateRange(
|
||||
context,
|
||||
formatter,
|
||||
dateAddedMillis,
|
||||
dateAddedMillis,
|
||||
DateUtils.FORMAT_ABBREV_ALL
|
||||
)
|
||||
context,
|
||||
formatter,
|
||||
dateAddedMillis,
|
||||
dateAddedMillis,
|
||||
DateUtils.FORMAT_ABBREV_ALL)
|
||||
.toString()
|
||||
}
|
||||
|
||||
@@ -159,7 +155,6 @@ class SongListFragment :
|
||||
homeNoMusic.isInvisible = songs.isEmpty()
|
||||
}
|
||||
songAdapter.update(songs, homeModel.songInstructions.consume())
|
||||
|
||||
}
|
||||
|
||||
private fun updateSelection(selection: List<Music>) {
|
||||
|
||||
@@ -24,7 +24,6 @@ import androidx.core.content.edit
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import javax.inject.Inject
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.music.dirs.MusicDirectories
|
||||
import org.oxycblt.auxio.music.stack.explore.fs.DocumentPathFactory
|
||||
import org.oxycblt.auxio.settings.Settings
|
||||
import timber.log.Timber as L
|
||||
@@ -35,8 +34,7 @@ import timber.log.Timber as L
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
interface MusicSettings : Settings<MusicSettings.Listener> {
|
||||
/** The configuration on how to handle particular directories in the music library. */
|
||||
var musicDirs: MusicDirectories
|
||||
/** The locations of music to load. */
|
||||
var musicLocations: List<Uri>
|
||||
/** Whether to exclude non-music audio files from the music library. */
|
||||
val excludeNonMusic: Boolean
|
||||
@@ -61,25 +59,27 @@ constructor(
|
||||
@ApplicationContext context: Context,
|
||||
private val documentPathFactory: DocumentPathFactory
|
||||
) : Settings.Impl<MusicSettings.Listener>(context), MusicSettings {
|
||||
override var musicDirs: MusicDirectories
|
||||
get() {
|
||||
val dirs =
|
||||
(sharedPreferences.getStringSet(getString(R.string.set_key_music_dirs), null)
|
||||
?: emptySet())
|
||||
.mapNotNull(documentPathFactory::fromDocumentId)
|
||||
return MusicDirectories(
|
||||
dirs,
|
||||
sharedPreferences.getBoolean(getString(R.string.set_key_music_dirs_include), false))
|
||||
}
|
||||
set(value) {
|
||||
sharedPreferences.edit {
|
||||
putStringSet(
|
||||
getString(R.string.set_key_music_dirs),
|
||||
value.dirs.map(documentPathFactory::toDocumentId).toSet())
|
||||
putBoolean(getString(R.string.set_key_music_dirs_include), value.shouldInclude)
|
||||
apply()
|
||||
}
|
||||
}
|
||||
// override var musicDirs: MusicDirectories
|
||||
// get() {
|
||||
// val dirs =
|
||||
// (sharedPreferences.getStringSet(getString(R.string.set_key_music_dirs), null)
|
||||
// ?: emptySet())
|
||||
// .mapNotNull(documentPathFactory::fromDocumentId)
|
||||
// return MusicDirectories(
|
||||
// dirs,
|
||||
// sharedPreferences.getBoolean(getString(R.string.set_key_music_dirs_include),
|
||||
// false))
|
||||
// }
|
||||
// set(value) {
|
||||
// sharedPreferences.edit {
|
||||
// putStringSet(
|
||||
// getString(R.string.set_key_music_dirs),
|
||||
// value.dirs.map(documentPathFactory::toDocumentId).toSet())
|
||||
// putBoolean(getString(R.string.set_key_music_dirs_include),
|
||||
// value.shouldInclude)
|
||||
// apply()
|
||||
// }
|
||||
// }
|
||||
|
||||
override var musicLocations: List<Uri>
|
||||
get() {
|
||||
|
||||
@@ -1,122 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Auxio Project
|
||||
* DirectoryAdapter.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.dirs
|
||||
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.databinding.ItemMusicDirBinding
|
||||
import org.oxycblt.auxio.list.recycler.DialogRecyclerView
|
||||
import org.oxycblt.auxio.music.stack.explore.fs.Path
|
||||
import org.oxycblt.auxio.util.context
|
||||
import org.oxycblt.auxio.util.inflater
|
||||
import timber.log.Timber as L
|
||||
|
||||
/**
|
||||
* [RecyclerView.Adapter] that manages a list of [Path] music directory instances.
|
||||
*
|
||||
* @param listener A [DirectoryAdapter.Listener] to bind interactions to.
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
class DirectoryAdapter(private val listener: Listener) :
|
||||
RecyclerView.Adapter<MusicDirViewHolder>() {
|
||||
private val _dirs = mutableListOf<Path>()
|
||||
/** The current list of [Path]s, may not line up with [MusicDirectories] due to removals. */
|
||||
val dirs: List<Path> = _dirs
|
||||
|
||||
override fun getItemCount() = dirs.size
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
|
||||
MusicDirViewHolder.from(parent)
|
||||
|
||||
override fun onBindViewHolder(holder: MusicDirViewHolder, position: Int) =
|
||||
holder.bind(dirs[position], listener)
|
||||
|
||||
/**
|
||||
* Add a [Path] to the end of the list.
|
||||
*
|
||||
* @param path The [Path] to add.
|
||||
*/
|
||||
fun add(path: Path) {
|
||||
if (_dirs.contains(path)) return
|
||||
L.d("Adding $path")
|
||||
_dirs.add(path)
|
||||
notifyItemInserted(_dirs.lastIndex)
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a list of [Path] instances to the end of the list.
|
||||
*
|
||||
* @param path The [Path] instances to add.
|
||||
*/
|
||||
fun addAll(path: List<Path>) {
|
||||
L.d("Adding ${path.size} directories")
|
||||
val oldLastIndex = path.lastIndex
|
||||
_dirs.addAll(path)
|
||||
notifyItemRangeInserted(oldLastIndex, path.size)
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a [Path] from the list.
|
||||
*
|
||||
* @param path The [Path] to remove. Must exist in the list.
|
||||
*/
|
||||
fun remove(path: Path) {
|
||||
L.d("Removing $path")
|
||||
val idx = _dirs.indexOf(path)
|
||||
_dirs.removeAt(idx)
|
||||
notifyItemRemoved(idx)
|
||||
}
|
||||
|
||||
/** A Listener for [DirectoryAdapter] interactions. */
|
||||
interface Listener {
|
||||
/** Called when the delete button on a directory item is clicked. */
|
||||
fun onRemoveDirectory(dir: Path)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A [RecyclerView.Recycler] that displays a [Path]. Use [from] to create an instance.
|
||||
*
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
class MusicDirViewHolder private constructor(private val binding: ItemMusicDirBinding) :
|
||||
DialogRecyclerView.ViewHolder(binding.root) {
|
||||
/**
|
||||
* Bind new data to this instance.
|
||||
*
|
||||
* @param path The new [Path] to bind.
|
||||
* @param listener A [DirectoryAdapter.Listener] to bind interactions to.
|
||||
*/
|
||||
fun bind(path: Path, listener: DirectoryAdapter.Listener) {
|
||||
binding.dirPath.text = path.resolve(binding.context)
|
||||
binding.dirDelete.setOnClickListener { listener.onRemoveDirectory(path) }
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Create a new instance.
|
||||
*
|
||||
* @param parent The parent to inflate this instance from.
|
||||
* @return A new instance.
|
||||
*/
|
||||
fun from(parent: View) =
|
||||
MusicDirViewHolder(ItemMusicDirBinding.inflate(parent.context.inflater))
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Auxio Project
|
||||
* MusicDirectories.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.dirs
|
||||
|
||||
import org.oxycblt.auxio.music.stack.explore.fs.Path
|
||||
|
||||
/**
|
||||
* Represents the configuration for specific directories to filter to/from when loading music.
|
||||
*
|
||||
* @param dirs A list of directory [Path] instances. How these are interpreted depends on
|
||||
* [shouldInclude]
|
||||
* @param shouldInclude True if the library should only load from the [Path] instances, false if the
|
||||
* library should not load from the [Path] instances.
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
data class MusicDirectories(val dirs: List<Path>, val shouldInclude: Boolean)
|
||||
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Auxio Project
|
||||
* LocationAdapter.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.net.Uri
|
||||
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.music.stack.explore.fs.Path
|
||||
import org.oxycblt.auxio.util.context
|
||||
import org.oxycblt.auxio.util.inflater
|
||||
import timber.log.Timber as L
|
||||
|
||||
/**
|
||||
* [RecyclerView.Adapter] that manages a list of [MusicLocation] music directory instances.
|
||||
*
|
||||
* @param listener A [LocationAdapter.Listener] to bind interactions to.
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
class LocationAdapter(private val listener: Listener) : RecyclerView.Adapter<MusicDirViewHolder>() {
|
||||
private val _locations = mutableListOf<MusicLocation>()
|
||||
/**
|
||||
* The current list of [MusicLocation]s, may not line up with [MusicLocation]s due to removals.
|
||||
*/
|
||||
val locations: List<MusicLocation> = _locations
|
||||
|
||||
override fun getItemCount() = locations.size
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
|
||||
MusicDirViewHolder.from(parent)
|
||||
|
||||
override fun onBindViewHolder(holder: MusicDirViewHolder, position: Int) =
|
||||
holder.bind(locations[position], listener)
|
||||
|
||||
/**
|
||||
* Add a [MusicLocation] to the end of the list.
|
||||
*
|
||||
* @param location The [MusicLocation] to add.
|
||||
*/
|
||||
fun add(location: MusicLocation) {
|
||||
if (_locations.contains(location)) return
|
||||
L.d("Adding $location")
|
||||
_locations.add(location)
|
||||
notifyItemInserted(_locations.lastIndex)
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a list of [MusicLocation] instances to the end of the list.
|
||||
*
|
||||
* @param locations The [MusicLocation] instances to add.
|
||||
*/
|
||||
fun addAll(locations: List<MusicLocation>) {
|
||||
L.d("Adding ${locations.size} locations")
|
||||
val oldLastIndex = locations.lastIndex
|
||||
_locations.addAll(locations)
|
||||
notifyItemRangeInserted(oldLastIndex, locations.size)
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a [MusicLocation] from the list.
|
||||
*
|
||||
* @param location The [MusicLocation] to remove. Must exist in the list.
|
||||
*/
|
||||
fun remove(location: MusicLocation) {
|
||||
L.d("Removing $location")
|
||||
val idx = _locations.indexOf(location)
|
||||
_locations.removeAt(idx)
|
||||
notifyItemRemoved(idx)
|
||||
}
|
||||
|
||||
/** A Listener for [LocationAdapter] interactions. */
|
||||
interface Listener {
|
||||
/** Called when the delete button on a directory item is clicked. */
|
||||
fun onRemoveLocation(location: MusicLocation)
|
||||
}
|
||||
}
|
||||
|
||||
data class MusicLocation(val uri: Uri, val path: Path)
|
||||
|
||||
/**
|
||||
* A [RecyclerView.Recycler] that displays a [MusicLocation]. Use [from] to create an instance.
|
||||
*
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
class MusicDirViewHolder private constructor(private val binding: ItemMusicLocationBinding) :
|
||||
DialogRecyclerView.ViewHolder(binding.root) {
|
||||
/**
|
||||
* Bind new data to this instance.
|
||||
*
|
||||
* @param location The new [MusicLocation] to bind.
|
||||
* @param listener A [LocationAdapter.Listener] to bind interactions to.
|
||||
*/
|
||||
fun bind(location: MusicLocation, listener: LocationAdapter.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: View) =
|
||||
MusicDirViewHolder(ItemMusicLocationBinding.inflate(parent.context.inflater))
|
||||
}
|
||||
}
|
||||
+42
-73
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Auxio Project
|
||||
* MusicDirsDialog.kt is part of Auxio.
|
||||
* MusicSourcesDialog.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
|
||||
@@ -16,7 +16,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.oxycblt.auxio.music.dirs
|
||||
package org.oxycblt.auxio.music.locations
|
||||
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.net.Uri
|
||||
@@ -31,49 +31,51 @@ import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
import org.oxycblt.auxio.BuildConfig
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.DialogMusicDirsBinding
|
||||
import org.oxycblt.auxio.databinding.DialogMusicLocationsBinding
|
||||
import org.oxycblt.auxio.music.MusicSettings
|
||||
import org.oxycblt.auxio.music.stack.explore.fs.DocumentPathFactory
|
||||
import org.oxycblt.auxio.music.stack.explore.fs.Path
|
||||
import org.oxycblt.auxio.ui.ViewBindingMaterialDialogFragment
|
||||
import org.oxycblt.auxio.util.showToast
|
||||
import timber.log.Timber as L
|
||||
|
||||
/**
|
||||
* Dialog that manages the music dirs setting.
|
||||
* Dialog that manages the music locations setting.
|
||||
*
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
@AndroidEntryPoint
|
||||
class MusicDirsDialog :
|
||||
ViewBindingMaterialDialogFragment<DialogMusicDirsBinding>(), DirectoryAdapter.Listener {
|
||||
private val dirAdapter = DirectoryAdapter(this)
|
||||
class MusicSourcesDialog :
|
||||
ViewBindingMaterialDialogFragment<DialogMusicLocationsBinding>(), LocationAdapter.Listener {
|
||||
private val locationAdapter = LocationAdapter(this)
|
||||
private var openDocumentTreeLauncher: ActivityResultLauncher<Uri?>? = null
|
||||
@Inject lateinit var documentPathFactory: DocumentPathFactory
|
||||
@Inject lateinit var musicSettings: MusicSettings
|
||||
|
||||
override fun onCreateBinding(inflater: LayoutInflater) =
|
||||
DialogMusicDirsBinding.inflate(inflater)
|
||||
DialogMusicLocationsBinding.inflate(inflater)
|
||||
|
||||
override fun onConfigDialog(builder: AlertDialog.Builder) {
|
||||
builder
|
||||
.setTitle(R.string.set_dirs)
|
||||
.setTitle(R.string.set_locations)
|
||||
.setNegativeButton(R.string.lbl_cancel, null)
|
||||
.setPositiveButton(R.string.lbl_save) { _, _ ->
|
||||
val newDirs = MusicDirectories(dirAdapter.dirs, isUiModeInclude(requireBinding()))
|
||||
if (musicSettings.musicDirs != newDirs) {
|
||||
val newDirs = locationAdapter.locations.map { it.uri }
|
||||
if (musicSettings.musicLocations != newDirs) {
|
||||
L.d("Committing changes")
|
||||
musicSettings.musicDirs = newDirs
|
||||
musicSettings.musicLocations = newDirs
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBindingCreated(binding: DialogMusicDirsBinding, savedInstanceState: Bundle?) {
|
||||
override fun onBindingCreated(
|
||||
binding: DialogMusicLocationsBinding,
|
||||
savedInstanceState: Bundle?
|
||||
) {
|
||||
openDocumentTreeLauncher =
|
||||
registerForActivityResult(
|
||||
ActivityResultContracts.OpenDocumentTree(), ::addDocumentTreeUriToDirs)
|
||||
|
||||
binding.dirsAdd.apply {
|
||||
binding.locationsAdd.apply {
|
||||
ViewCompat.setTooltipText(this, contentDescription)
|
||||
setOnClickListener {
|
||||
L.d("Opening launcher")
|
||||
@@ -91,98 +93,65 @@ class MusicDirsDialog :
|
||||
}
|
||||
}
|
||||
|
||||
binding.dirsRecycler.apply {
|
||||
adapter = dirAdapter
|
||||
binding.locationsRecycler.apply {
|
||||
adapter = locationAdapter
|
||||
itemAnimator = null
|
||||
}
|
||||
|
||||
var dirs = musicSettings.musicDirs
|
||||
if (savedInstanceState != null) {
|
||||
val pendingDirs = savedInstanceState.getStringArrayList(KEY_PENDING_DIRS)
|
||||
if (pendingDirs != null) {
|
||||
dirs =
|
||||
MusicDirectories(
|
||||
pendingDirs.mapNotNull(documentPathFactory::fromDocumentId),
|
||||
savedInstanceState.getBoolean(KEY_PENDING_MODE))
|
||||
val locationUris =
|
||||
savedInstanceState?.getStringArrayList(KEY_PENDING_LOCATIONS)?.map { Uri.parse(it) }
|
||||
?: musicSettings.musicLocations
|
||||
val locations =
|
||||
locationUris.mapNotNull {
|
||||
MusicLocation(
|
||||
it, documentPathFactory.unpackDocumentTreeUri(it) ?: return@mapNotNull null)
|
||||
}
|
||||
}
|
||||
|
||||
dirAdapter.addAll(dirs.dirs)
|
||||
requireBinding().dirsEmpty.isVisible = dirs.dirs.isEmpty()
|
||||
|
||||
binding.folderModeGroup.apply {
|
||||
check(
|
||||
if (dirs.shouldInclude) {
|
||||
R.id.dirs_mode_include
|
||||
} else {
|
||||
R.id.dirs_mode_exclude
|
||||
})
|
||||
|
||||
updateMode()
|
||||
addOnButtonCheckedListener { _, _, _ -> updateMode() }
|
||||
}
|
||||
locationAdapter.addAll(locations)
|
||||
requireBinding().locationsEmpty.isVisible = locations.isEmpty()
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
super.onSaveInstanceState(outState)
|
||||
outState.putStringArrayList(
|
||||
KEY_PENDING_DIRS, ArrayList(dirAdapter.dirs.map(documentPathFactory::toDocumentId)))
|
||||
outState.putBoolean(KEY_PENDING_MODE, isUiModeInclude(requireBinding()))
|
||||
KEY_PENDING_LOCATIONS, ArrayList(locationAdapter.locations.map { it.uri.toString() }))
|
||||
}
|
||||
|
||||
override fun onDestroyBinding(binding: DialogMusicDirsBinding) {
|
||||
override fun onDestroyBinding(binding: DialogMusicLocationsBinding) {
|
||||
super.onDestroyBinding(binding)
|
||||
openDocumentTreeLauncher = null
|
||||
binding.dirsRecycler.adapter = null
|
||||
binding.locationsRecycler.adapter = null
|
||||
}
|
||||
|
||||
override fun onRemoveDirectory(dir: Path) {
|
||||
dirAdapter.remove(dir)
|
||||
requireBinding().dirsEmpty.isVisible = dirAdapter.dirs.isEmpty()
|
||||
override fun onRemoveLocation(location: MusicLocation) {
|
||||
locationAdapter.remove(location)
|
||||
requireBinding().locationsEmpty.isVisible = locationAdapter.locations.isEmpty()
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a Document Tree [Uri] chosen by the user to the current [MusicDirectories] instance.
|
||||
* Add a Document Tree [Uri] chosen by the user to the current [MusicLocation]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
|
||||
// A null URI means that the user left the file picker without picking a locationectory
|
||||
L.d("No URI given (user closed the dialog)")
|
||||
return
|
||||
}
|
||||
|
||||
val dir = documentPathFactory.unpackDocumentTreeUri(uri)
|
||||
val path = documentPathFactory.unpackDocumentTreeUri(uri)
|
||||
|
||||
if (dir != null) {
|
||||
dirAdapter.add(dir)
|
||||
requireBinding().dirsEmpty.isVisible = false
|
||||
if (path != null) {
|
||||
locationAdapter.add(MusicLocation(uri, path))
|
||||
requireBinding().locationsEmpty.isVisible = false
|
||||
} else {
|
||||
requireContext().showToast(R.string.err_bad_dir)
|
||||
requireContext().showToast(R.string.err_bad_location)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateMode() {
|
||||
val binding = requireBinding()
|
||||
if (isUiModeInclude(binding)) {
|
||||
binding.dirsModeExclude.icon = null
|
||||
binding.dirsModeInclude.setIconResource(R.drawable.ic_check_24)
|
||||
binding.dirsModeDesc.setText(R.string.set_dirs_mode_include_desc)
|
||||
} else {
|
||||
binding.dirsModeExclude.setIconResource(R.drawable.ic_check_24)
|
||||
binding.dirsModeInclude.icon = null
|
||||
binding.dirsModeDesc.setText(R.string.set_dirs_mode_exclude_desc)
|
||||
}
|
||||
}
|
||||
|
||||
/** Get if the UI has currently configured [MusicDirectories.shouldInclude] to be true. */
|
||||
private fun isUiModeInclude(binding: DialogMusicDirsBinding) =
|
||||
binding.folderModeGroup.checkedButtonId == R.id.dirs_mode_include
|
||||
|
||||
private companion object {
|
||||
const val KEY_PENDING_DIRS = BuildConfig.APPLICATION_ID + ".key.PENDING_DIRS"
|
||||
const val KEY_PENDING_MODE = BuildConfig.APPLICATION_ID + ".key.SHOULD_INCLUDE"
|
||||
const val KEY_PENDING_LOCATIONS = BuildConfig.APPLICATION_ID + ".key.PENDING_LOCATIONS"
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ import org.oxycblt.auxio.music.Library
|
||||
import org.oxycblt.auxio.music.Music
|
||||
import org.oxycblt.auxio.music.Playlist
|
||||
import org.oxycblt.auxio.music.Song
|
||||
import org.oxycblt.auxio.music.stack.explore.fs.Pathi
|
||||
import org.oxycblt.auxio.music.stack.explore.fs.Path
|
||||
|
||||
interface MutableLibrary : Library {
|
||||
suspend fun createPlaylist(name: String, songs: List<Song>): MutableLibrary
|
||||
|
||||
@@ -52,7 +52,8 @@ 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.musicDirsSettings())
|
||||
findNavController()
|
||||
.navigateSafe(RootPreferenceFragmentDirections.musicLocationsSettings())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user