Skip to content
This repository has been archived by the owner on Dec 12, 2020. It is now read-only.

Rework Genre Tab #31

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/main/xerus/monstercat/Sheets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ object Sheets {
.build()
.spreadsheets()
}

const val MCATALOG = "116LycNEkWChmHmDK2HM2WV85fO3p3YTYDATpAthL8_g"
const val GENRESHEET = "1xZUWWnll7HzDVmNj_W7cBfz9TTkl-fMMqHZ8derG-Dg"

val mcatalog = "116LycNEkWChmHmDK2HM2WV85fO3p3YTYDATpAthL8_g"
val genreSheet = "1xZUWWnll7HzDVmNj_W7cBfz9TTkl-fMMqHZ8derG-Dg"
fun fetchMCatalogTab(tab: String, range: String? = null): MutableList<List<String>>? {
fun fetchSheet(sheet: String, tab: String, range: String? = null): MutableList<List<String>>? {
var requestRange = tab
if (!range.isNullOrEmpty())
requestRange += "!$range"
return try {
val request = values.get(mcatalog, requestRange).setKey(getResource("sheets-api-key")?.readText())
val request = values.get(sheet, requestRange).setKey(getResource("sheets-api-key")?.readText())
val result = request.execute()
@Suppress("Unchecked_cast")
result.getValues() as MutableList<List<String>>
Expand Down
45 changes: 19 additions & 26 deletions src/main/xerus/monstercat/tabs/FetchTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,37 @@ import xerus.ktutil.javafx.ui.controls.Snackbar
import xerus.ktutil.readToObject
import xerus.ktutil.writeToFile
import xerus.monstercat.*
import xerus.monstercat.Sheets.fetchMCatalogTab
import xerus.monstercat.Sheets.fetchSheet
import xerus.monstercat.api.Releases
import java.io.*
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException

abstract class FetchTab : VTab() {
abstract class FetchTab(val sheet: String, val request: String = "") : VTab() {

val cols = RoughMap<Int>()
val data: ObservableList<List<String>> = FXCollections.observableArrayList()

protected open val request: String = ""
private val retryButton: Button = createButton("Try again") {
setPlaceholder(Label("Fetching..."))
sheetFetcher()
}

val sheetFetcher = SimpleRefresher {
if (this::class != TabGenres::class) {
onFx { setPlaceholder(Label("Fetching...")) }
logger.debug("Fetching $tabName")
val sheet = fetchMCatalogTab(tabName, request)
if (sheet != null) {
readSheet(sheet)
writeCache(sheet)
} else if (data.isEmpty())
restoreCache()
onFx {
if (data.isEmpty()) {
logger.debug("Showing retry button for $tabName because data is empty")
setPlaceholder(retryButton)
} else
setPlaceholder(Label("No matches found!"))
}
} else {
// todo use Genre sheet
onFx { setPlaceholder(Label("No matches found!")) }
logger.debug("Loading $tabName")
@Suppress("UNCHECKED_CAST")
readSheet(ObjectInputStream(TabGenres::class.java.getResourceAsStream("/Genres")).readObject() as MutableList<List<String>>)
onFx { setPlaceholder(Label("Fetching...")) }
logger.debug("Fetching $tabName")
val sheet = fetchSheet(sheet, tabName, request)
if (sheet != null) {
readSheet(sheet)
writeCache(sheet)
} else if (data.isEmpty())
restoreCache()
onFx {
if (data.isEmpty()) {
logger.debug("Showing retry button for $tabName because data is empty")
setPlaceholder(retryButton)
} else
setPlaceholder(Label("No matches found!"))
}
}

Expand Down
77 changes: 27 additions & 50 deletions src/main/xerus/monstercat/tabs/TabGenres.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package xerus.monstercat.tabs
import javafx.beans.InvalidationListener
import javafx.geometry.Insets
import javafx.scene.Node
import javafx.scene.control.*
import javafx.scene.control.TextField
import javafx.scene.control.TreeCell
import javafx.scene.control.TreeView
import javafx.scene.layout.VBox
import xerus.ktutil.helpers.RoughMap
import xerus.ktutil.helpers.Row
import xerus.ktutil.javafx.*
import xerus.ktutil.javafx.properties.listen
import xerus.ktutil.javafx.ui.FilterableTreeItem
import xerus.monstercat.Settings.GENRECOLORINTENSITY
import xerus.monstercat.Sheets

val genreColors = RoughMap<String>()
val genreColor = { item: String? ->
Expand All @@ -19,92 +21,67 @@ val genreColor = { item: String? ->
}
}

class TabGenres : FetchTab() {
class TabGenres : FetchTab(Sheets.GENRESHEET, "A:H") {

private val view = TreeTableView<Row>().apply {
columnResizePolicy = TreeTableView.CONSTRAINED_RESIZE_POLICY
}
private val view = TreeView<String>()

init {
styleClass("tab-genres")
val searchField = TextField()
VBox.setMargin(searchField, Insets(0.0, 0.0, 6.0, 0.0)) // apparently can't set this in css
val root = FilterableTreeItem(Row())
root.bindPredicate(searchField.textProperty()) { row, text -> row.subList(0, 3).any { it.contains(text, true) } }
VBox.setMargin(searchField, Insets(0.0, 0.0, 6.0, 0.0))
val root = FilterableTreeItem("")
root.bindPredicate(searchField.textProperty()) { text, filter -> text.contains(filter, true) }
view.isShowRoot = false
data.addListener(InvalidationListener {
view.root = root
root.internalChildren.clear()
var cur = root
var curLevel = 0

val hex = cols.find("Hex")
if (hex == null) logger.warn("No hex Column found!")

var style = ""
for (list in data) {
if (list.isEmpty())
continue
val row = Row(10, *list.toTypedArray())
val nextLevel = row.indexOfFirst { it.isNotEmpty() }
if (nextLevel < curLevel)
repeat(curLevel - nextLevel) { cur = cur.parent as? FilterableTreeItem<Row> ?: cur.also { logger.warn("$cur should have a parent!") } }
repeat(curLevel - nextLevel) { cur = cur.parent as? FilterableTreeItem<String> ?: cur.also { logger.warn("$cur should have a parent!") } }

if (hex != null) {
/*if (hex != null) {
if (nextLevel == 0) {
style = row[hex]
genreColors.put(row[0], style)
}
row[hex] = style
}
}*/

val new = FilterableTreeItem(row)
val new = FilterableTreeItem(row[nextLevel])
cur.internalChildren.add(new)

curLevel = nextLevel + 1
cur = new
}
if (hex != null)
refreshViews()
})

view.setRowFactory {
/*view.setRowFactory {
TreeTableRow<Row>().apply {
if (GENRECOLORINTENSITY() > 0) {
val hex = cols.find("Hex") ?: return@apply
itemProperty().listen { style = genreColor(it?.get(hex)) }
}
}
}

val columns = arrayOf<TreeTableColumn<Row, String?>>(
TreeTableColumn("Genre") { it.value.value.firstOrNull { it.isNotEmpty() } },
TreeTableColumn("Typical BPM") { it.value.value[cols.findUnsafe("BPM")] },
TreeTableColumn("Typical Beat") { it.value.value[cols.findUnsafe("Beat")] })
}*/

view.columns.addAll(*columns)
columns.forEach {
it.isSortable = false
it.setCellFactory {
object : TreeTableCell<Row, String?>() {
override fun updateItem(item: String?, empty: Boolean) {
super.updateItem(item, empty)
if (item == null || empty) {
text = null
style = ""
} else {
text = item
val treeItem = this.treeTableRow.treeItem
font = if (treeItem != null && treeItem.parent === root) {
font.bold()
} else {
style = ""
if (item.contains("listed in", true))
font.italic()
else
font.format()
}
}
view.setCellFactory {
object : TreeCell<String>() {
override fun updateItem(item: String, empty: Boolean) {
super.updateItem(item, empty)
text = item
val treeItem = this.treeItem
font = if (treeItem != null && treeItem.parent === root) {
font.bold()
} else {
style = ""
font.format()
}
}
}
Expand All @@ -114,7 +91,7 @@ class TabGenres : FetchTab() {
fill(view)
}

override fun setPlaceholder(n: Node) = view.setPlaceholder(n)
override fun setPlaceholder(n: Node) {}

override fun refreshView() {
view.refresh()
Expand Down
3 changes: 2 additions & 1 deletion src/main/xerus/monstercat/tabs/TableTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package xerus.monstercat.tabs
import javafx.scene.Node
import xerus.ktutil.javafx.styleClass
import xerus.ktutil.javafx.ui.controls.FilteredTableView
import xerus.monstercat.Sheets

open class TableTab : FetchTab() {
open class TableTab : FetchTab(Sheets.MCATALOG) {

val table = FilteredTableView(data, true)
val predicate = table.predicate.apply { addListener { _ -> showFoundSnackbar() } }
Expand Down