image: optimize grouping routine

Turns out the image grouping routine runs on the main thread and is
slow enough to cause massive freezing. Attempt to resolve this.
This commit is contained in:
Alexander Capehart 2023-06-01 12:37:47 -06:00
parent 5f70ce8870
commit 46fb33de59
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
3 changed files with 17 additions and 10 deletions

View file

@ -322,7 +322,11 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
*
* @param song The [Song] to bind to the view.
*/
fun bind(song: Song) = bind(song.album)
fun bind(song: Song) =
bind(
listOf(song),
context.getString(R.string.desc_album_cover, song.album.name),
R.drawable.ic_album_24)
/**
* Bind an [Album]'s image to this view.

View file

@ -117,13 +117,17 @@ constructor(
* by their names. "Representation" is defined by how many [Song]s were found to be linked to
* the given [Album] in the given [Song] list.
*/
fun computeCoverOrdering(songs: List<Song>) =
Sort(Sort.Mode.ByAlbum, Sort.Direction.ASCENDING)
.songs(songs)
.groupBy { it.album }
.entries
.sortedByDescending { it.value.size }
.map { it.key }
fun computeCoverOrdering(songs: List<Song>): List<Album> {
if (songs.isEmpty()) return listOf()
if (songs.size == 1) return listOf(songs.first().album)
val sortedMap =
sortedMapOf<Album, Int>(Sort.Mode.ByName.getAlbumComparator(Sort.Direction.ASCENDING))
for (song in songs) {
sortedMap[song.album] = (sortedMap[song.album] ?: 0) + 1
}
return sortedMap.keys.sortedByDescending { sortedMap[it] }
}
private suspend fun openCoverInputStream(album: Album) =
try {
@ -205,7 +209,7 @@ constructor(
withContext(Dispatchers.IO) { context.contentResolver.openInputStream(album.coverUri) }
/** Derived from phonograph: https://github.com/kabouzeid/Phonograph */
private suspend fun createMosaic(streams: List<InputStream>, size: Size): FetchResult {
private fun createMosaic(streams: List<InputStream>, size: Size): FetchResult {
// Use whatever size coil gives us to create the mosaic.
val mosaicSize = AndroidSize(size.width.mosaicSize(), size.height.mosaicSize())
val mosaicFrameSize =

View file

@ -23,7 +23,6 @@ import android.content.Context
import android.content.Intent
import android.content.res.ColorStateList
import android.content.res.Configuration
import android.os.Build
import android.util.TypedValue
import android.view.LayoutInflater
import android.widget.Toast