Skip to content

Commit

Permalink
add TAG continuous mode
Browse files Browse the repository at this point in the history
  • Loading branch information
wakingrufus committed Mar 25, 2022
1 parent eebca2a commit e920a6b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Jamm : Application(), Logging {
top<VBox> {
menuBar {
menu("Settings") {

actionItem("Music Library...") {
DirectoryChooser().apply {
this.initialDirectory = File(libraryPath.get())
Expand Down Expand Up @@ -84,19 +85,40 @@ class Jamm : Application(), Logging {
putPreference(Preference.LASTFM_KEY, "")
}
}
checkItem("Continuous Play") {
this.onAction = EventHandler {
putPreference(Preference.CONTINUOUS_PLAY, this.isSelected.toString())
}
this.selectedProperty()
.set(getPreference(Preference.CONTINUOUS_PLAY, "false").toBoolean())
}
checkItem("Dark Mode") {
this.onAction = EventHandler {
setOnAction {
putPreference(Preference.DARK_MODE, this.isSelected.toString())
}
this.selectedProperty().set(getPreference(Preference.DARK_MODE, "true").toBoolean())
}
separator {

}
subMenu("Continuous Mode") {
toggleGroup {
radio(ContinuousMode.OFF.toString()) {
setOnAction {
putPreference(Preference.CONTINUOUS_MODE, ContinuousMode.OFF.toString())
}
selectedProperty()
.set(getPreference(Preference.CONTINUOUS_MODE, ContinuousMode.OFF) == ContinuousMode.OFF)
}
radio(ContinuousMode.TAG.toString()) {
setOnAction {
putPreference(Preference.CONTINUOUS_MODE, ContinuousMode.TAG.toString())
}
selectedProperty()
.set(getPreference(Preference.CONTINUOUS_MODE, ContinuousMode.OFF) == ContinuousMode.TAG)
}
radio(ContinuousMode.RANDOM.toString()) {
setOnAction {
putPreference(Preference.CONTINUOUS_MODE, ContinuousMode.RANDOM.toString())
}
selectedProperty()
.set(getPreference(Preference.CONTINUOUS_MODE, ContinuousMode.OFF) == ContinuousMode.RANDOM)
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,16 @@ class JfxMediaPlayerController(
nowPlayingProperty.set(track)
updateProgress(track)
queue.remove(track)
if (queue.isEmpty() && getPreference(Preference.CONTINUOUS_PLAY, "false").toBoolean()) {
queue.add(library.tracks.random())
if (queue.isEmpty()) {
val continuousMode = getPreference(Preference.CONTINUOUS_MODE, ContinuousMode.OFF)
if (continuousMode == ContinuousMode.RANDOM) {
queue.add(library.tracks.random())
} else if (continuousMode == ContinuousMode.TAG) {
queue.add(library.tracks
.filter { it.tags.any { track.tags.contains(it) } }
.randomOrNull()
?: library.tracks.random())
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import java.io.FileReader
import java.io.FileWriter
import java.util.logging.Level


class ObservableLibrary(val rootDir: File) : Logging {
val playlists: ObservableList<Playlist> = FXCollections.observableArrayList()
val trackPaths: ObservableMap<String, Track> = FXCollections.observableHashMap()
Expand Down Expand Up @@ -68,10 +67,10 @@ class ObservableLibrary(val rootDir: File) : Logging {
})
}

fun importCsv(){
fun importCsv() {
GlobalScope.launch(Dispatchers.Default) {
val csvFile = rootDir.resolve("jamm.csv")
val reader = FileReader(csvFile)
val reader = FileReader(csvFile)
val headers = trackCsvFields.map { it.header }
val records: Iterable<CSVRecord> = CSVFormat.DEFAULT
.withHeader(*headers.toTypedArray())
Expand Down Expand Up @@ -153,17 +152,17 @@ class ObservableLibrary(val rootDir: File) : Logging {
GlobalScope.launch(Dispatchers.Default) {
val newTrackScans = files
.filter { Extensions.music.contains(it.extension.toLowerCase()) }
.chunked(1_000)
.map {
it.map { readTrack(rootDir, it) }
}
.flatten()
newTrackScans.filterIsInstance<ScanResult.ScanFailure>().forEach { logger().error(it.error) }
val newTracks = newTrackScans.filterIsInstance<ScanResult.TrackSuccess>().map { it.track }
.map { readTrack(rootDir, it) }
newTrackScans.filterIsInstance<ScanResult.ScanFailure>().forEach { logger().error(it.error) }
val newTracks = newTrackScans.filterIsInstance<ScanResult.TrackSuccess>().map { it.track }
withContext(Dispatchers.JavaFx) {
trackPaths.clear()
tracks.clear()
newTracks.forEach { importTrack(it) }
}
newTracks.chunked(500).forEach {
withContext(Dispatchers.JavaFx) {
it.forEach { importTrack(it) }
}
}
listeners.forEach {
it.loadComplete()
Expand All @@ -173,11 +172,11 @@ class ObservableLibrary(val rootDir: File) : Logging {
}
}

fun addListener(listener: LibraryListener){
fun addListener(listener: LibraryListener) {
listeners.add(listener)
}

fun addTagListener(listener: LibraryListener){
fun addTagListener(listener: LibraryListener) {
tagListeners.add(listener)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.github.wakingrufus.jamm.desktop

import java.util.*
import java.util.prefs.Preferences

enum class Preference(val prefName: String) {
LIBRARY_PATH("library.path"),
CONTINUOUS_PLAY("continuous"),
LASTFM_KEY("lastfm.session_key"),
DARK_MODE("dark_mode")
DARK_MODE("dark_mode"),
CONTINUOUS_MODE("continuous.mode")
}

fun getPreference(name: Preference, default: String): String {
Expand All @@ -15,4 +16,13 @@ fun getPreference(name: Preference, default: String): String {

fun putPreference(name: Preference, value: String) {
Preferences.userNodeForPackage(Jamm::class.java).put(name.prefName, value)
}

inline fun <reified T : Enum<T>> getPreference(name: Preference, default: T): T {
val valueString = getPreference(name, default.toString())
return EnumSet.allOf(T::class.java).firstOrNull { it.toString() == valueString } ?: default
}

enum class ContinuousMode {
RANDOM, TAG, OFF
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,28 @@ fun MenuBar.menu(name: String, menu: Menu.() -> Unit): Menu {
return Menu(name).apply(menu).also { this.menus.add(it) }
}

fun Menu.subMenu(name: String, menu: Menu.() -> Unit): Menu {
return Menu(name).apply(menu).also { this.items.add(it) }
}
class TOGGLE(val menu: Menu, val toggleGroup: ToggleGroup){
fun radio(name: String, radio: RadioMenuItem.() -> Unit){
RadioMenuItem(name).apply(radio).apply {
menu.items.add(this)
this.toggleGroup = toggleGroup
}
}
}
fun Menu.toggleGroup(toggleGroup: TOGGLE.() -> Unit){
TOGGLE(this, ToggleGroup()).apply(toggleGroup)
}
fun Menu.item(name: String, item: MenuItem.() -> Unit): MenuItem {
return MenuItem(name).apply(item).also { this.items.add(it) }
}

fun Menu.separator(item: SeparatorMenuItem.() -> Unit): MenuItem {
return SeparatorMenuItem().apply(item).also { this.items.add(it) }
}

fun Menu.checkItem(name: String, item: CheckMenuItem.() -> Unit): CheckMenuItem {
return CheckMenuItem(name).apply(item).also { this.items.add(it) }
}
Expand Down

0 comments on commit e920a6b

Please sign in to comment.