diff --git a/app/src/main/java/io/github/zyrouge/symphony/services/groove/Album.kt b/app/src/main/java/io/github/zyrouge/symphony/services/groove/Album.kt index 86a4d1ae..2f8fd3fc 100644 --- a/app/src/main/java/io/github/zyrouge/symphony/services/groove/Album.kt +++ b/app/src/main/java/io/github/zyrouge/symphony/services/groove/Album.kt @@ -8,6 +8,8 @@ data class Album( val id: String, val name: String, val artists: MutableSet, + var startYear: Int?, + var endYear: Int?, var numberOfTracks: Int, ) { fun createArtworkImageRequest(symphony: Symphony) = diff --git a/app/src/main/java/io/github/zyrouge/symphony/services/groove/repositories/AlbumRepository.kt b/app/src/main/java/io/github/zyrouge/symphony/services/groove/repositories/AlbumRepository.kt index 4009325d..18a67a7d 100644 --- a/app/src/main/java/io/github/zyrouge/symphony/services/groove/repositories/AlbumRepository.kt +++ b/app/src/main/java/io/github/zyrouge/symphony/services/groove/repositories/AlbumRepository.kt @@ -14,6 +14,8 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import java.util.concurrent.ConcurrentHashMap +import kotlin.math.max +import kotlin.math.min class AlbumRepository(private val symphony: Symphony) { enum class SortBy { @@ -21,6 +23,7 @@ class AlbumRepository(private val symphony: Symphony) { ALBUM_NAME, ARTIST_NAME, TRACKS_COUNT, + YEAR, } private val cache = ConcurrentHashMap() @@ -50,6 +53,10 @@ class AlbumRepository(private val symphony: Symphony) { cache.compute(albumId) { _, value -> value?.apply { artists.addAll(song.artists) + song.year?.let { + startYear = startYear?.let { old -> min(old, it) } ?: it + endYear = endYear?.let { old -> max(old, it) } ?: it + } numberOfTracks++ } ?: run { _all.update { @@ -60,6 +67,8 @@ class AlbumRepository(private val symphony: Symphony) { id = albumId, name = song.album!!, artists = song.artists.toMutableSet(), + startYear = song.year, + endYear = song.year, numberOfTracks = 1, ) } @@ -100,6 +109,7 @@ class AlbumRepository(private val symphony: Symphony) { SortBy.ALBUM_NAME -> albumIds.sortedBy { get(it)?.name } SortBy.ARTIST_NAME -> albumIds.sortedBy { get(it)?.artists?.joinToStringIfNotEmpty() } SortBy.TRACKS_COUNT -> albumIds.sortedBy { get(it)?.numberOfTracks } + SortBy.YEAR -> albumIds.sortedBy { get(it)?.startYear } } return if (reverse) sorted.reversed() else sorted } diff --git a/app/src/main/java/io/github/zyrouge/symphony/ui/components/AlbumGrid.kt b/app/src/main/java/io/github/zyrouge/symphony/ui/components/AlbumGrid.kt index 6b9f2c3c..ab5d4832 100644 --- a/app/src/main/java/io/github/zyrouge/symphony/ui/components/AlbumGrid.kt +++ b/app/src/main/java/io/github/zyrouge/symphony/ui/components/AlbumGrid.kt @@ -3,7 +3,6 @@ package io.github.zyrouge.symphony.ui.components import androidx.compose.foundation.lazy.grid.itemsIndexed import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Album -import androidx.compose.material3.Button import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Icon import androidx.compose.material3.ModalBottomSheet @@ -111,4 +110,5 @@ private fun AlbumRepository.SortBy.label(context: ViewContext) = when (this) { AlbumRepository.SortBy.ALBUM_NAME -> context.symphony.t.Album AlbumRepository.SortBy.ARTIST_NAME -> context.symphony.t.Artist AlbumRepository.SortBy.TRACKS_COUNT -> context.symphony.t.TrackCount + AlbumRepository.SortBy.YEAR -> context.symphony.t.Year }