music: re-add library find functionality

This commit is contained in:
Alexander Capehart 2024-11-26 10:08:07 -07:00
parent 9d9f810356
commit 0ba5ddce51
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
6 changed files with 83 additions and 89 deletions

View file

@ -34,7 +34,6 @@ import org.oxycblt.auxio.music.metadata.Separators
import org.oxycblt.auxio.music.stack.Indexer import org.oxycblt.auxio.music.stack.Indexer
import org.oxycblt.auxio.music.stack.interpret.Interpretation import org.oxycblt.auxio.music.stack.interpret.Interpretation
import org.oxycblt.auxio.music.stack.interpret.model.MutableLibrary import org.oxycblt.auxio.music.stack.interpret.model.MutableLibrary
import kotlin.math.exp
import timber.log.Timber as L import timber.log.Timber as L
/** /**
@ -366,7 +365,8 @@ constructor(private val indexer: Indexer, private val musicSettings: MusicSettin
var explored = 0 var explored = 0
var loaded = 0 var loaded = 0
val newLibrary = indexer.run(listOf(), Interpretation(nameFactory, separators)) { val newLibrary =
indexer.run(listOf(), Interpretation(nameFactory, separators)) {
when (it) { when (it) {
is Indexer.Event.Discovered -> { is Indexer.Event.Discovered -> {
explored = it.amount explored = it.amount

View file

@ -31,27 +31,31 @@ import org.oxycblt.auxio.music.stack.interpret.Interpreter
import org.oxycblt.auxio.music.stack.interpret.model.MutableLibrary import org.oxycblt.auxio.music.stack.interpret.model.MutableLibrary
interface Indexer { interface Indexer {
suspend fun run(uris: List<Uri>, interpretation: Interpretation, eventHandler: suspend (Event) -> Unit = {}): MutableLibrary suspend fun run(
uris: List<Uri>,
interpretation: Interpretation,
eventHandler: suspend (Event) -> Unit = {}
): MutableLibrary
sealed interface Event { sealed interface Event {
data class Discovered( data class Discovered(
val amount: Int, val amount: Int,
) : Event ) : Event
data class Extracted( data class Extracted(val amount: Int) : Event
val amount: Int
) : Event
data class Interpret( data class Interpret(val amount: Int) : Event
val amount: Int
) : Event
} }
} }
class IndexerImpl class IndexerImpl
@Inject @Inject
constructor(private val explorer: Explorer, private val interpreter: Interpreter) : Indexer { constructor(private val explorer: Explorer, private val interpreter: Interpreter) : Indexer {
override suspend fun run(uris: List<Uri>, interpretation: Interpretation, eventHandler: suspend (Event) -> Unit) = coroutineScope { override suspend fun run(
uris: List<Uri>,
interpretation: Interpretation,
eventHandler: suspend (Event) -> Unit
) = coroutineScope {
val files = explorer.explore(uris, eventHandler) val files = explorer.explore(uris, eventHandler)
val audioFiles = files.audios.flowOn(Dispatchers.IO).buffer() val audioFiles = files.audios.flowOn(Dispatchers.IO).buffer()
val playlistFiles = files.playlists.flowOn(Dispatchers.IO).buffer() val playlistFiles = files.playlists.flowOn(Dispatchers.IO).buffer()

View file

@ -60,7 +60,9 @@ constructor(
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
override fun explore(uris: List<Uri>, eventHandler: suspend (Indexer.Event) -> Unit): Files { override fun explore(uris: List<Uri>, eventHandler: suspend (Indexer.Event) -> Unit): Files {
var discovered = 0 var discovered = 0
val deviceFiles = deviceFiles.explore(uris.asFlow()) val deviceFiles =
deviceFiles
.explore(uris.asFlow())
.onEach { .onEach {
discovered++ discovered++
eventHandler(Indexer.Event.Discovered(discovered)) eventHandler(Indexer.Event.Discovered(discovered))
@ -77,7 +79,8 @@ constructor(
.flattenMerge() .flattenMerge()
val writtenAudioFiles = tagCache.write(extractedAudioFiles).flowOn(Dispatchers.IO).buffer() val writtenAudioFiles = tagCache.write(extractedAudioFiles).flowOn(Dispatchers.IO).buffer()
var loaded = 0 var loaded = 0
val audioFiles = merge(cachedAudioFiles, writtenAudioFiles).onEach { val audioFiles =
merge(cachedAudioFiles, writtenAudioFiles).onEach {
loaded++ loaded++
eventHandler(Indexer.Event.Extracted(loaded)) eventHandler(Indexer.Event.Extracted(loaded))
} }
@ -97,8 +100,7 @@ constructor(
val indexed = withIndex() val indexed = withIndex()
val shared = val shared =
indexed.shareIn( indexed.shareIn(
CoroutineScope(Dispatchers.Main), SharingStarted.WhileSubscribed(), replay = 0 CoroutineScope(Dispatchers.Main), SharingStarted.WhileSubscribed(), replay = 0)
)
return Array(n) { shared.filter { it.index % n == 0 }.map { it.value } } return Array(n) { shared.filter { it.index % n == 0 }.map { it.value } }
} }
} }

View file

@ -83,7 +83,7 @@ class InterpreterImpl @Inject constructor(private val preparer: Preparer) : Inte
.register(artistLinkedSongs) .register(artistLinkedSongs)
.flowOn(Dispatchers.Main) .flowOn(Dispatchers.Main)
.onEach { .onEach {
interpreted++; interpreted++
eventHandler(Indexer.Event.Interpret(interpreted)) eventHandler(Indexer.Event.Interpret(interpreted))
} }
.map { LinkedSongImpl(it) } .map { LinkedSongImpl(it) }
@ -91,7 +91,8 @@ class InterpreterImpl @Inject constructor(private val preparer: Preparer) : Inte
val albums = albumLinker.resolve() val albums = albumLinker.resolve()
val uidMap = mutableMapOf<Music.UID, SongImpl>() val uidMap = mutableMapOf<Music.UID, SongImpl>()
val songs = albumLinkedSongs.mapNotNull { val songs =
albumLinkedSongs.mapNotNull {
val uid = it.preSong.computeUid() val uid = it.preSong.computeUid()
val other = uidMap[uid] val other = uidMap[uid]
if (other == null) { if (other == null) {

View file

@ -18,9 +18,6 @@
package org.oxycblt.auxio.music.stack.interpret.model package org.oxycblt.auxio.music.stack.interpret.model
import org.oxycblt.auxio.music.Album
import org.oxycblt.auxio.music.Artist
import org.oxycblt.auxio.music.Genre
import org.oxycblt.auxio.music.Library import org.oxycblt.auxio.music.Library
import org.oxycblt.auxio.music.Music import org.oxycblt.auxio.music.Music
import org.oxycblt.auxio.music.Playlist import org.oxycblt.auxio.music.Playlist
@ -47,53 +44,43 @@ class LibraryImpl(
) : MutableLibrary { ) : MutableLibrary {
override val playlists = emptySet<Playlist>() override val playlists = emptySet<Playlist>()
private val songUidMap = songs.associ { it.uid } private val songUidMap = songs.associateBy { it.uid }
private val albumUidMap = albums.associateBy { it.uid }
private val artistUidMap = artists.associateBy { it.uid }
private val genreUidMap = genres.associateBy { it.uid }
private val playlistUidMap = playlists.associateBy { it.uid }
override fun findSong(uid: Music.UID): Song? { override fun findSong(uid: Music.UID) = songUidMap[uid]
TODO("Not yet implemented")
}
override fun findSongByPath(path: Path): Song? { override fun findSongByPath(path: Path) = songs.find { it.path == path }
TODO("Not yet implemented")
}
override fun findAlbum(uid: Music.UID): Album? { override fun findAlbum(uid: Music.UID) = albumUidMap[uid]
TODO("Not yet implemented")
}
override fun findArtist(uid: Music.UID): Artist? { override fun findArtist(uid: Music.UID) = artistUidMap[uid]
TODO("Not yet implemented")
}
override fun findGenre(uid: Music.UID): Genre? { override fun findGenre(uid: Music.UID) = genreUidMap[uid]
TODO("Not yet implemented")
}
override fun findPlaylist(uid: Music.UID): Playlist? { override fun findPlaylist(uid: Music.UID) = playlistUidMap[uid]
TODO("Not yet implemented")
}
override fun findPlaylistByName(name: String): Playlist? { override fun findPlaylistByName(name: String) = playlists.find { it.name.raw == name }
TODO("Not yet implemented")
}
override suspend fun createPlaylist(name: String, songs: List<Song>): MutableLibrary { override suspend fun createPlaylist(name: String, songs: List<Song>): MutableLibrary {
TODO("Not yet implemented") return this
} }
override suspend fun renamePlaylist(playlist: Playlist, name: String): MutableLibrary { override suspend fun renamePlaylist(playlist: Playlist, name: String): MutableLibrary {
TODO("Not yet implemented") return this
} }
override suspend fun addToPlaylist(playlist: Playlist, songs: List<Song>): MutableLibrary { override suspend fun addToPlaylist(playlist: Playlist, songs: List<Song>): MutableLibrary {
TODO("Not yet implemented") return this
} }
override suspend fun rewritePlaylist(playlist: Playlist, songs: List<Song>): MutableLibrary { override suspend fun rewritePlaylist(playlist: Playlist, songs: List<Song>): MutableLibrary {
TODO("Not yet implemented") return this
} }
override suspend fun deletePlaylist(playlist: Playlist): MutableLibrary { override suspend fun deletePlaylist(playlist: Playlist): MutableLibrary {
TODO("Not yet implemented") return this
} }
} }