Skip to content

Commit

Permalink
feat: visible album year & sorting (#674)
Browse files Browse the repository at this point in the history
* Feat: Album year + sorting

* Feat: Album year + sorting
  • Loading branch information
sevonj authored Nov 21, 2024
1 parent abcdadb commit 7baaaad
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ data class Album(
val id: String,
val name: String,
val artists: MutableSet<String>,
var startYear: Int?,
var endYear: Int?,
var numberOfTracks: Int,
) {
fun createArtworkImageRequest(symphony: Symphony) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ 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 {
CUSTOM,
ALBUM_NAME,
ARTIST_NAME,
TRACKS_COUNT,
YEAR,
}

private val cache = ConcurrentHashMap<String, Album>()
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
)
}
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 7baaaad

Please sign in to comment.