Skip to content

Commit

Permalink
Merge pull request #30 from ruslanys/search
Browse files Browse the repository at this point in the history
Search
  • Loading branch information
ruslanys authored Sep 1, 2018
2 parents 85ec8c6 + 230e40a commit 1d4b915
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 26 deletions.
60 changes: 50 additions & 10 deletions src/main/kotlin/me/ruslanys/vkmusic/controller/MainController.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package me.ruslanys.vkmusic.controller

import javafx.animation.PauseTransition
import javafx.collections.FXCollections
import javafx.collections.ObservableList
import javafx.fxml.FXML
import javafx.scene.control.MenuItem
import javafx.scene.control.SelectionMode
import javafx.scene.control.TableColumn
import javafx.scene.control.TableView
import javafx.scene.control.*
import javafx.scene.control.cell.PropertyValueFactory
import javafx.scene.image.Image
import javafx.scene.image.ImageView
import javafx.scene.layout.Pane
import javafx.stage.DirectoryChooser
import javafx.util.Duration
import me.ruslanys.vkmusic.annotation.FxmlController
import me.ruslanys.vkmusic.component.VkClient
import me.ruslanys.vkmusic.domain.Audio
Expand All @@ -32,17 +30,23 @@ class MainController(
private val vkClient: VkClient,
private val downloadService: DownloadService) : ApplicationListener<DownloadEvent>, BaseController() {

// @formatter:off
@FXML private lateinit var loadingView: Pane
@FXML private lateinit var mainView: Pane

@FXML private lateinit var loadingImageView: ImageView
@FXML private lateinit var tableView: TableView<Audio>
@FXML private lateinit var openFolderMenuItem: MenuItem
@FXML private lateinit var searchField: TextField
// @formatter:on

private val data: ObservableList<Audio> = FXCollections.observableArrayList(arrayListOf())
private val data = mutableListOf<Audio>()

@FXML
fun initialize() {
initLoading()
initTable()
initSearch()
}

private fun initLoading() {
Expand Down Expand Up @@ -88,7 +92,34 @@ class MainController(
tableView.selectionModel.selectedItemProperty().addListener { _, _, _ -> adjustMenuAvailability() }

// Data
tableView.items = data
tableView.items = FXCollections.observableArrayList(data)
}

private fun initSearch() {
val pauseTransition = PauseTransition(Duration.millis(300.0)) // debounce mechanism
pauseTransition.setOnFinished { _ -> search(searchField.text) }

searchField.textProperty().addListener { _, _, _ -> pauseTransition.playFromStart() }
}

private fun search(input: String) {
val argument = input.toLowerCase()
val list = ArrayList<Audio>(data)

CompletableFuture.supplyAsync {
val found = mutableListOf<Audio>()
list.forEach {
val artist = it.artist.toLowerCase()
val title = it.title.toLowerCase()

if (artist.contains(argument) || title.contains(argument)) {
found.add(it)
}
}
found
}.thenAccept {
setItems(it)
}
}

@FXML
Expand All @@ -98,14 +129,16 @@ class MainController(

@FXML
fun refreshTable() {
tableView.isVisible = false
mainView.isVisible = false

CompletableFuture.supplyAsync {
vkClient.getAudio()
}.thenAccept {
data.clear()
data.addAll(it)
tableView.isVisible = true

setItems(data)
mainView.isVisible = true
}
}

Expand All @@ -120,7 +153,7 @@ class MainController(
forEach { it.status = DownloadStatus.QUEUED }
tableView.refresh()

downloadService.download(selectedDirectory, this.toList())
downloadService.download(selectedDirectory, ArrayList<Audio>(this))
}
}

Expand All @@ -135,6 +168,13 @@ class MainController(
openFolderMenuItem.isDisable = !(selectedItems.size == 1 && selectedItems[0].file != null)
}

private fun setItems(list: List<Audio>) {
synchronized(tableView) {
tableView.items.clear()
tableView.items.addAll(list)
}
}

override fun onApplicationEvent(event: DownloadEvent) {
log.info("Event $event")

Expand Down
39 changes: 23 additions & 16 deletions src/main/resources/views/main.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,31 @@
<StackPane BorderPane.alignment="CENTER">
<BorderPane fx:id="loadingView" style="-fx-background-color: rgb(80, 114, 153);">
<center>
<ImageView fx:id="loadingImageView" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER" />
<ImageView fx:id="loadingImageView" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER"/>
</center>
</BorderPane>
<BorderPane fx:id="mainView">
<top>
<TextField fx:id="searchField" promptText="Поиск" BorderPane.alignment="CENTER"/>
</top>
<center>
<TableView fx:id="tableView" style="-fx-focus-color: -fx-control-inner-background; -fx-faint-focus-color: -fx-control-inner-background;" tableMenuButtonVisible="true" BorderPane.alignment="CENTER">
<contextMenu>
<ContextMenu>
<items>
<MenuItem mnemonicParsing="false" onAction="#selectAll" text="Выделить все"/>
<MenuItem mnemonicParsing="false" onAction="#downloadSelected" text="Скачать выделенные"/>
<MenuItem mnemonicParsing="false" onAction="#refreshTable" text="Обновить"/>
<MenuItem fx:id="openFolderMenuItem" disable="true" mnemonicParsing="false" onAction="#openFolder" text="Показать в папке"/>
</items>
</ContextMenu>
</contextMenu>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
</columnResizePolicy>
</TableView>
</center>
</BorderPane>
<TableView fx:id="tableView" style="-fx-focus-color: -fx-control-inner-background; -fx-faint-focus-color: -fx-control-inner-background;" tableMenuButtonVisible="true">
<contextMenu>
<ContextMenu>
<items>
<MenuItem mnemonicParsing="false" onAction="#selectAll" text="Выделить все" />
<MenuItem mnemonicParsing="false" onAction="#downloadSelected" text="Скачать выделенные" />
<MenuItem mnemonicParsing="false" onAction="#refreshTable" text="Обновить" />
<MenuItem fx:id="openFolderMenuItem" mnemonicParsing="false" onAction="#openFolder" text="Показать в папке" disable="true" />
</items>
</ContextMenu>
</contextMenu>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</StackPane>
</center>

Expand Down

0 comments on commit 1d4b915

Please sign in to comment.