musikr: refactor musiclocation api into location
Improves the API surface pretty heavily. Assisted by Claude 4 Sonnet/Claude 3.5 Haiku.
This commit is contained in:
@@ -38,6 +38,7 @@ import org.oxycblt.musikr.Music
|
||||
import org.oxycblt.musikr.Musikr
|
||||
import org.oxycblt.musikr.MutableLibrary
|
||||
import org.oxycblt.musikr.Playlist
|
||||
import org.oxycblt.musikr.Query
|
||||
import org.oxycblt.musikr.Song
|
||||
import org.oxycblt.musikr.Storage
|
||||
import org.oxycblt.musikr.cache.MutableCache
|
||||
@@ -396,10 +397,11 @@ constructor(
|
||||
val fileTreeCache =
|
||||
if (withCache) FileTreeCache.from(context)
|
||||
else WriteOnlyFileTreeCache(FileTreeCache.from(context))
|
||||
val query = Query(source = locations, exclude = emptyList())
|
||||
val storage = Storage(cache, covers, storedPlaylists, fileTreeCache)
|
||||
val interpretation = Interpretation(nameFactory, separators, withHidden)
|
||||
val result =
|
||||
Musikr.new(context, storage, interpretation).run(locations, ::emitIndexingProgress)
|
||||
Musikr.new(context, storage, interpretation).run(query, ::emitIndexingProgress)
|
||||
// Music loading completed, update the revision right now so we re-use this work
|
||||
// later.
|
||||
musicSettings.revision = newRevision
|
||||
|
||||
@@ -20,12 +20,14 @@ package org.oxycblt.auxio.music
|
||||
|
||||
import android.content.Context
|
||||
import androidx.core.content.edit
|
||||
import androidx.core.net.toUri
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.settings.Settings
|
||||
import org.oxycblt.musikr.fs.MusicLocation
|
||||
import org.oxycblt.musikr.fs.Location
|
||||
import org.oxycblt.musikr.fs.OpenedLocation
|
||||
import timber.log.Timber as L
|
||||
|
||||
/**
|
||||
@@ -37,7 +39,7 @@ interface MusicSettings : Settings<MusicSettings.Listener> {
|
||||
/** The current library revision. */
|
||||
var revision: UUID?
|
||||
/** The locations of music to load. */
|
||||
var musicLocations: List<MusicLocation>
|
||||
var musicLocations: List<OpenedLocation>
|
||||
/** Whether to exclude non-music audio files from the music library. */
|
||||
val excludeNonMusic: Boolean
|
||||
/** Whether to ignore hidden files and directories during music loading. */
|
||||
@@ -74,17 +76,17 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont
|
||||
}
|
||||
}
|
||||
|
||||
override var musicLocations: List<MusicLocation>
|
||||
override var musicLocations: List<OpenedLocation>
|
||||
get() {
|
||||
val locations =
|
||||
sharedPreferences.getString(getString(R.string.set_key_music_locations), null)
|
||||
?: return emptyList()
|
||||
return MusicLocation.existing(context, locations)
|
||||
return locations.toOpenedLocations()
|
||||
}
|
||||
set(value) {
|
||||
sharedPreferences.edit {
|
||||
putString(
|
||||
getString(R.string.set_key_music_locations), MusicLocation.toString(value))
|
||||
getString(R.string.set_key_music_locations), value.stringify())
|
||||
commit()
|
||||
// Sometimes changing this setting just won't actually trigger the listener.
|
||||
// Only this one. No idea why.
|
||||
@@ -136,4 +138,49 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun List<OpenedLocation>.stringify(): String =
|
||||
joinToString(separator = ";") { it.uri.toString().replace(";", "\\;") }
|
||||
|
||||
private fun String.toOpenedLocations(): List<OpenedLocation> =
|
||||
splitEscaped { it == ';' }.mapNotNull { Location.from(context, it.toUri())?.open(context) }
|
||||
|
||||
private inline fun String.splitEscaped(selector: (Char) -> Boolean): List<String> {
|
||||
val split = mutableListOf<String>()
|
||||
var currentString = ""
|
||||
var i = 0
|
||||
|
||||
while (i < length) {
|
||||
val a = get(i)
|
||||
val b = getOrNull(i + 1)
|
||||
|
||||
if (selector(a)) {
|
||||
// Non-escaped separator, split the string here, making sure any stray whitespace
|
||||
// is removed.
|
||||
split.add(currentString)
|
||||
currentString = ""
|
||||
i++
|
||||
continue
|
||||
}
|
||||
|
||||
if (b != null && a == '\\' && selector(b)) {
|
||||
// Is an escaped character, add the non-escaped variant and skip two
|
||||
// characters to move on to the next one.
|
||||
currentString += b
|
||||
i += 2
|
||||
} else {
|
||||
// Non-escaped, increment normally.
|
||||
currentString += a
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
if (currentString.isNotEmpty()) {
|
||||
// Had an in-progress split string that is now terminated, add it.
|
||||
split.add(currentString)
|
||||
}
|
||||
|
||||
return split
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,21 +25,22 @@ 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.MusicLocation
|
||||
import org.oxycblt.musikr.fs.Location
|
||||
import org.oxycblt.musikr.fs.OpenedLocation
|
||||
import timber.log.Timber as L
|
||||
|
||||
/**
|
||||
* [RecyclerView.Adapter] that manages a list of [MusicLocation] music directory instances.
|
||||
* [RecyclerView.Adapter] that manages a list of [OpenedLocation] 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>()
|
||||
private val _locations = mutableListOf<OpenedLocation>()
|
||||
/**
|
||||
* The current list of [MusicLocation]s, may not line up with [MusicLocation]s due to removals.
|
||||
* The current list of [OpenedLocation]s, may not line up with [OpenedLocation]s due to removals.
|
||||
*/
|
||||
val locations: List<MusicLocation> = _locations
|
||||
val locations: List<OpenedLocation> = _locations
|
||||
|
||||
override fun getItemCount() = locations.size
|
||||
|
||||
@@ -50,11 +51,11 @@ class LocationAdapter(private val listener: Listener) : RecyclerView.Adapter<Mus
|
||||
holder.bind(locations[position], listener)
|
||||
|
||||
/**
|
||||
* Add a [MusicLocation] to the end of the list.
|
||||
* Add a [OpenedLocation] to the end of the list.
|
||||
*
|
||||
* @param location The [MusicLocation] to add.
|
||||
* @param location The [OpenedLocation] to add.
|
||||
*/
|
||||
fun add(location: MusicLocation) {
|
||||
fun add(location: OpenedLocation) {
|
||||
if (_locations.contains(location)) return
|
||||
L.d("Adding $location")
|
||||
_locations.add(location)
|
||||
@@ -62,11 +63,11 @@ class LocationAdapter(private val listener: Listener) : RecyclerView.Adapter<Mus
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a list of [MusicLocation] instances to the end of the list.
|
||||
* Add a list of [OpenedLocation] instances to the end of the list.
|
||||
*
|
||||
* @param locations The [MusicLocation] instances to add.
|
||||
* @param locations The [OpenedLocation] instances to add.
|
||||
*/
|
||||
fun addAll(locations: List<MusicLocation>) {
|
||||
fun addAll(locations: List<OpenedLocation>) {
|
||||
L.d("Adding ${locations.size} locations")
|
||||
val oldLastIndex = locations.lastIndex
|
||||
_locations.addAll(locations)
|
||||
@@ -74,11 +75,11 @@ class LocationAdapter(private val listener: Listener) : RecyclerView.Adapter<Mus
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a [MusicLocation] from the list.
|
||||
* Remove a [OpenedLocation] from the list.
|
||||
*
|
||||
* @param location The [MusicLocation] to remove. Must exist in the list.
|
||||
* @param location The [OpenedLocation] to remove. Must exist in the list.
|
||||
*/
|
||||
fun remove(location: MusicLocation) {
|
||||
fun remove(location: OpenedLocation) {
|
||||
L.d("Removing $location")
|
||||
val idx = _locations.indexOf(location)
|
||||
_locations.removeAt(idx)
|
||||
@@ -88,12 +89,12 @@ class LocationAdapter(private val listener: Listener) : RecyclerView.Adapter<Mus
|
||||
/** A Listener for [LocationAdapter] interactions. */
|
||||
interface Listener {
|
||||
/** Called when the delete button on a directory item is clicked. */
|
||||
fun onRemoveLocation(location: MusicLocation)
|
||||
fun onRemoveLocation(location: OpenedLocation)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A [RecyclerView.Recycler] that displays a [MusicLocation]. Use [from] to create an instance.
|
||||
* A [RecyclerView.Recycler] that displays a [OpenedLocation]. Use [from] to create an instance.
|
||||
*
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
@@ -102,10 +103,10 @@ class MusicDirViewHolder private constructor(private val binding: ItemMusicLocat
|
||||
/**
|
||||
* Bind new data to this instance.
|
||||
*
|
||||
* @param location The new [MusicLocation] to bind.
|
||||
* @param location The new [OpenedLocation] to bind.
|
||||
* @param listener A [LocationAdapter.Listener] to bind interactions to.
|
||||
*/
|
||||
fun bind(location: MusicLocation, listener: LocationAdapter.Listener) {
|
||||
fun bind(location: OpenedLocation, listener: LocationAdapter.Listener) {
|
||||
binding.locationPath.text = location.path.resolve(binding.context)
|
||||
binding.locationDelete.setOnClickListener { listener.onRemoveLocation(location) }
|
||||
}
|
||||
|
||||
@@ -35,7 +35,8 @@ 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.MusicLocation
|
||||
import org.oxycblt.musikr.fs.Location
|
||||
import org.oxycblt.musikr.fs.OpenedLocation
|
||||
import timber.log.Timber as L
|
||||
|
||||
/**
|
||||
@@ -81,7 +82,8 @@ class MusicSourcesDialog :
|
||||
|
||||
val locations =
|
||||
savedInstanceState?.getStringArrayList(KEY_PENDING_LOCATIONS)?.mapNotNull {
|
||||
MusicLocation.existing(requireContext(), it.toUri())
|
||||
val context = requireContext()
|
||||
Location.from(context, it.toUri())?.open(context)
|
||||
} ?: musicSettings.musicLocations
|
||||
|
||||
locationAdapter.addAll(locations)
|
||||
@@ -99,7 +101,7 @@ class MusicSourcesDialog :
|
||||
binding.locationsRecycler.adapter = null
|
||||
}
|
||||
|
||||
override fun onRemoveLocation(location: MusicLocation) {
|
||||
override fun onRemoveLocation(location: OpenedLocation) {
|
||||
locationAdapter.remove(location)
|
||||
}
|
||||
|
||||
@@ -117,7 +119,7 @@ class MusicSourcesDialog :
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a Document Tree [Uri] chosen by the user to the current [MusicLocation]s.
|
||||
* Add a Document Tree [Uri] chosen by the user to the current [OpenedLocation]s.
|
||||
*
|
||||
* @param uri The document tree [Uri] to add, chosen by the user. Will do nothing if the [Uri]
|
||||
* is null or not valid.
|
||||
@@ -128,7 +130,8 @@ class MusicSourcesDialog :
|
||||
L.d("No URI given (user closed the dialog)")
|
||||
return
|
||||
}
|
||||
val location = MusicLocation.new(requireContext(), uri)
|
||||
val context = requireContext()
|
||||
val location = Location.from(context, uri)?.open(context)
|
||||
|
||||
if (location != null) {
|
||||
locationAdapter.add(location)
|
||||
|
||||
@@ -36,7 +36,7 @@ import org.oxycblt.auxio.music.shim.UpdateTrackerFactory
|
||||
import org.oxycblt.auxio.playback.state.PlaybackStateManager
|
||||
import org.oxycblt.auxio.util.getSystemServiceCompat
|
||||
import org.oxycblt.musikr.MusicParent
|
||||
import org.oxycblt.musikr.fs.MusicLocation
|
||||
import org.oxycblt.musikr.fs.OpenedLocation
|
||||
import org.oxycblt.musikr.track.UpdateTracker
|
||||
import timber.log.Timber as L
|
||||
|
||||
@@ -168,7 +168,7 @@ private constructor(
|
||||
}
|
||||
}
|
||||
|
||||
override fun onUpdate(location: MusicLocation) {
|
||||
override fun onUpdate(location: OpenedLocation) {
|
||||
if (musicSettings.shouldBeObserving) {
|
||||
musicRepository.requestIndex(true)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user