music: fix broken location updates

This commit is contained in:
Alexander Capehart 2024-12-13 10:54:14 -07:00
parent 59df1c3d57
commit 5fae4601de
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
3 changed files with 23 additions and 25 deletions

View file

@ -58,9 +58,11 @@ class MusicSettingsImpl @Inject constructor(@ApplicationContext private val cont
override var musicLocations: List<MusicLocation> override var musicLocations: List<MusicLocation>
get() { get() {
val dirs = val dirs =
sharedPreferences.getStringSet(getString(R.string.set_key_music_locations), null) sharedPreferences.getStringSet(getString(R.string.set_key_music_locations), null) ?:
?: emptySet() emptySet()
return dirs.mapNotNull { MusicLocation.existing(context, Uri.parse(it)) } return dirs.mapNotNull {
MusicLocation.existing(context, Uri.parse(it))
}
} }
set(value) { set(value) {
sharedPreferences.edit { sharedPreferences.edit {

View file

@ -31,7 +31,7 @@ import timber.log.Timber as L
* *
* @author Alexander Capehart (OxygenCobalt) * @author Alexander Capehart (OxygenCobalt)
*/ */
interface Settings<L> { interface Settings<Listener> {
/** /**
* Migrate any settings fields from older versions into their new counterparts. * Migrate any settings fields from older versions into their new counterparts.
* *
@ -46,41 +46,45 @@ interface Settings<L> {
* *
* @param listener The listener to add. * @param listener The listener to add.
*/ */
fun registerListener(listener: L) fun registerListener(listener: Listener)
/** /**
* Unregister a listener, preventing any further settings updates from being sent to it. * Unregister a listener, preventing any further settings updates from being sent to it.
* *
* @param listener The listener to unregister, must be the same as the current listener. * @param listener The listener to unregister, must be the same as the current listener.
*/ */
fun unregisterListener(listener: L) fun unregisterListener(listener: Listener)
/** /**
* A framework-backed [Settings] implementation. * A framework-backed [Settings] implementation.
* *
* @param context [Context] required. * @param context [Context] required.
*/ */
abstract class Impl<L>(private val context: Context) : abstract class Impl<Listener>(private val context: Context) :
Settings<L>, SharedPreferences.OnSharedPreferenceChangeListener { Settings<Listener>, SharedPreferences.OnSharedPreferenceChangeListener {
init {
L.d(this::class.simpleName)
}
protected val sharedPreferences: SharedPreferences = protected val sharedPreferences: SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context.applicationContext) PreferenceManager.getDefaultSharedPreferences(context.applicationContext)
/** @see [Context.getString] */ /** @see [Context.getString] */
protected fun getString(@StringRes stringRes: Int) = context.getString(stringRes) protected fun getString(@StringRes stringRes: Int) = context.getString(stringRes)
private var listener: L? = null private var listener: Listener? = null
override fun registerListener(listener: L) { override fun registerListener(listener: Listener) {
if (this.listener == null) { if (this.listener == null) {
// Registering a listener when it was null prior, attach the callback. // Registering a listener when it was null prior, attach the callback.
L.d("Registering shared preference listener") L.d("Registering shared preference listener for ${this::class.simpleName}")
sharedPreferences.registerOnSharedPreferenceChangeListener(this) sharedPreferences.registerOnSharedPreferenceChangeListener(this)
} }
L.d("Registering listener $listener") L.d("Registering listener $listener for ${this::class.simpleName}")
this.listener = listener this.listener = listener
} }
override fun unregisterListener(listener: L) { override fun unregisterListener(listener: Listener) {
if (this.listener !== listener) { if (this.listener !== listener) {
L.w("Given listener was not the current listener.") L.w("Given listener was not the current listener.")
return return
@ -106,6 +110,6 @@ interface Settings<L> {
* @param key The key of the changed setting. * @param key The key of the changed setting.
* @param listener The implementation's listener that updates should be applied to. * @param listener The implementation's listener that updates should be applied to.
*/ */
protected open fun onSettingChanged(key: String, listener: L) {} protected open fun onSettingChanged(key: String, listener: Listener) {}
} }
} }

View file

@ -26,19 +26,11 @@ import org.oxycblt.musikr.fs.path.DocumentPathFactory
import org.oxycblt.musikr.fs.query.contentResolverSafe import org.oxycblt.musikr.fs.query.contentResolverSafe
class MusicLocation internal constructor(val uri: Uri, val path: Path) { class MusicLocation internal constructor(val uri: Uri, val path: Path) {
override fun equals(other: Any?) = override fun equals(other: Any?) = other is MusicLocation && uri == other.uri
other is MusicLocation && uri == other.uri && path == other.path
override fun hashCode() = 31 * uri.hashCode() + path.hashCode() override fun hashCode() = 31 * uri.hashCode()
override fun toString(): String { override fun toString(): String = uri.toString()
val volumeId =
when (path.volume) {
is Volume.Internal -> VOLUME_INTERNAL
is Volume.External -> path.volume.id
}
return "$uri=${volumeId}:${path.components.unixString}"
}
companion object { companion object {
fun new(context: Context, uri: Uri): MusicLocation? { fun new(context: Context, uri: Uri): MusicLocation? {