Skip to content
This repository has been archived by the owner on Aug 24, 2023. It is now read-only.

Commit

Permalink
MediaStoreReader: Make getAllAlbums sorted
Browse files Browse the repository at this point in the history
Signed-off-by: Shinjo Akane <akane@akanework.org>
  • Loading branch information
AkaneTan committed Jul 31, 2023
1 parent 202f196 commit 4ba22d2
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import org.akanework.symphonica.logic.data.Song
* This is called when first booting up.
*
* @param externalSongList
* @return
* @return sorted list of [Album]s based on title's first letter
*/
fun getAllAlbums(externalSongList: List<Song>): List<Album> {
val albumsMap = mutableMapOf<String, MutableList<Song>>()
Expand All @@ -52,12 +52,28 @@ fun getAllAlbums(externalSongList: List<Song>): List<Album> {
val (albumTitle, albumArtist) = albumKey.split("_")
val cover = null
val album = Album(albumTitle, albumArtist, cover, songList)
albums.add(album)
insertSorted(albums, album) // Insert the album in the sorted order
}

return albums
}

/**
* Helper function to insert an Album into the sorted position in a list.
* @param albums The list of albums where the album should be inserted.
* @param album The album to be inserted.
*/
private fun insertSorted(albums: MutableList<Album>, album: Album) {
val insertionIndex = albums.binarySearchBy(album.title, selector = { it.title })

if (insertionIndex < 0) {
albums.add(-(insertionIndex + 1), album)
} else {
albums.add(insertionIndex, album)
}
}


/**
* [sortAlbumListByTrackNumber]:
* Since re-ordering the albumList at libraryAlbumDisplayFragment
Expand Down

0 comments on commit 4ba22d2

Please sign in to comment.