Skip to content

Commit

Permalink
Allow shuffle sorting of the album list
Browse files Browse the repository at this point in the history
improve library refreshes to not lose selection in album view and tag view
  • Loading branch information
wakingrufus committed Jul 20, 2022
1 parent 45363ec commit 22564e8
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
package com.github.wakingrufus.jamm.common

class AlbumKey(val id: String?, val albumArtist: String, val albumName: String) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as AlbumKey

if (id != null && id == other.id) return true
if (albumArtist != other.albumArtist) return false
if (albumName != other.albumName) return false

return true
}

override fun hashCode(): Int {
return id?.hashCode() ?: (31 * albumArtist.hashCode() + albumName.hashCode())
}

override fun toString(): String {
return "AlbumKey(id=$id, albumArtist='$albumArtist', albumName='$albumName')"
}
data class AlbumKey(val albumArtist: String, val albumName: String) {
var id: String? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ fun buildTrack(rootFile: File, file: File, audioFile: AudioFile): ScanResult {
}
else null
val albumKey = AlbumKey(
id = releaseId,
albumArtist = albumArtist.name,
albumName = albumName
)
).apply {
id = releaseId
}
val originalYear = tag.getFirst(FieldKey.ORIGINAL_YEAR)
val originalDate = tag.getFirst(FieldKey.ORIGINALRELEASEDATE)
val year = tag.getFirst(FieldKey.YEAR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ class AlbumsView(val library: ObservableLibrary, val mediaPlayer: MediaPlayerCon
}
lateinit var yearSelection: ComboBox<String>
lateinit var albumListView: ListView<AlbumKey>
val albums = FXCollections.observableArrayList<AlbumKey>()

fun viewAlbum(albumKey: AlbumKey) {
selectedAlbum.set(albumKey)
}

fun applyFilter() {
fun applyFilter(selection: AlbumKey? = null) {
GlobalScope.launch(Dispatchers.Default) {
val newItems = library.tracks
.filtered { track ->
Expand All @@ -45,16 +46,22 @@ class AlbumsView(val library: ObservableLibrary, val mediaPlayer: MediaPlayerCon
.grouped { it.albumKey }
.sorted(Comparator.comparing { it.albumName })
withContext(Dispatchers.JavaFx) {
albumListView.items = newItems
albums.setAll(newItems)
if (selection != null) {
albumListView.selectionModel.select(selection)
}
}
}
}

init {
library.addListener { applyFilter() }
library.addListener {
val oldSelection = selectedAlbum.get()
applyFilter(selection = oldSelection)
}
top<HBox> {
button("Play Random Album") {
this.action {
action {
library.tracks.grouped { it.albumKey }.random().also { selectedAlbumKey ->
mediaPlayer.play(library.tracks.filtered { it.albumKey == selectedAlbumKey }
?.sortedBy { it.trackNumber }
Expand All @@ -75,15 +82,41 @@ class AlbumsView(val library: ObservableLibrary, val mediaPlayer: MediaPlayerCon
}
}

left<StackPane> {
albumListView = listview(library.tracks
.grouped { it.albumKey }
.sorted(Comparator.comparing { it.albumName })
) {
this.cellFactory = CustomStringCellFactory { it.albumName + " - " + it.albumArtist }
bindSelected(selectedAlbum)
yearSelection.selectionModel.selectedItemProperty().onChange {
applyFilter()
left<BorderPane> {
top<HBox> {
label("Sort:")
button("Alpha") {
action {
GlobalScope.launch(Dispatchers.Default) {
val newAlbums = library.tracks
.grouped { it.albumKey }
.sorted(Comparator.comparing { it.albumName })
withContext(Dispatchers.JavaFx) {
albums.setAll(newAlbums)
}
}
}
}
button("Random") {
action {
GlobalScope.launch(Dispatchers.Default) {
val newAlbums = library.tracks
.grouped { it.albumKey }
.shuffled()
withContext(Dispatchers.JavaFx) {
albums.setAll(newAlbums)
}
}
}
}
}
center<StackPane> {
albumListView = listview(albums) {
this.cellFactory = CustomStringCellFactory { it.albumName + " - " + it.albumArtist }
bindSelected(selectedAlbum)
yearSelection.selectionModel.selectedItemProperty().onChange {
applyFilter()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.wakingrufus.jamm.desktop

import com.github.wakingrufus.jamm.common.Track
import com.github.wakingrufus.jamm.library.LibraryListener
import com.github.wakingrufus.javafx.*
import javafx.beans.property.SimpleObjectProperty
import javafx.collections.FXCollections
Expand Down Expand Up @@ -30,13 +29,7 @@ class TagView(val library: ObservableLibrary, val mediaPlayer: MediaPlayerContro
init {
left<StackPane> {
val tagList = FXCollections.observableArrayList<String>()
library.addTagListener {
GlobalScope.launch(Dispatchers.JavaFx) {
tagList.clear()
tagList.addAll(library.tracks.flatMapUnique { it.tags }.sorted())
}
}
listview(tagList) {
val listView = listview(tagList) {
bindSelected(selectedTag)
contextMenu {
actionItem("Export") {
Expand All @@ -48,6 +41,16 @@ class TagView(val library: ObservableLibrary, val mediaPlayer: MediaPlayerContro
}
}
}
library.addTagListener {
val oldSelection = selectedTag.get()
GlobalScope.launch(Dispatchers.JavaFx) {
tagList.clear()
tagList.addAll(library.tracks.flatMapUnique { it.tags }.sorted())
if (oldSelection != null) {
listView.selectionModel.select(oldSelection)
}
}
}
}
center<StackPane> {
trackTable(tracks, library, mediaPlayer, listOf("Remove From Tag" to {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ fun CSVRecord.toTrack(baseDir: File): Track {
album = get("album"),
albumArtist = AlbumArtist(get("albumArtist")),
artist = Artist(get("artist")),
albumKey = AlbumKey(albumIdString.ifBlank { null }, get("albumArtist"), get("album")),
albumKey = AlbumKey(get("albumArtist"), get("album")).apply {
id = albumIdString.ifBlank { null }
},
trackNumber = get("trackNumber").toIntOrNull(),
discNumber = get("discNumber").toIntOrNull(),
path = get("path"),
Expand Down

0 comments on commit 22564e8

Please sign in to comment.