Added proper todo buttons reactively connected

This commit is contained in:
Rohan Sircar 2020-12-17 12:20:42 +05:30
parent f95ecc2cb3
commit f95f50574e

View File

@ -1,22 +1,27 @@
package nova.monadic_sfx.ui.components.todo package nova.monadic_sfx.ui.components.todo
import monix.bio.Task import monix.bio.Task
import monix.execution.cancelables.CompositeCancelable
import nova.monadic_sfx.implicits.FontIcon import nova.monadic_sfx.implicits.FontIcon
import nova.monadic_sfx.implicits.IconLiteral import nova.monadic_sfx.implicits.IconLiteral
import nova.monadic_sfx.implicits.JFXButton
import nova.monadic_sfx.implicits.JFXListView import nova.monadic_sfx.implicits.JFXListView
import nova.monadic_sfx.implicits.JFXTextField
import nova.monadic_sfx.implicits.JavaFXMonixObservables._ import nova.monadic_sfx.implicits.JavaFXMonixObservables._
import nova.monadic_sfx.implicits.MenuItem
import nova.monadic_sfx.util.reactive._
import org.gerweck.scalafx.util._
import scalafx.Includes._ import scalafx.Includes._
import scalafx.beans.property.ObjectProperty
import scalafx.beans.property.StringProperty import scalafx.beans.property.StringProperty
import scalafx.geometry.Insets
import scalafx.scene.Node
import scalafx.scene.control.ContextMenu import scalafx.scene.control.ContextMenu
import scalafx.scene.control.ListCell import scalafx.scene.control.ListCell
import scalafx.scene.control.SelectionMode import scalafx.scene.control.SelectionMode
import scalafx.scene.layout.HBox import scalafx.scene.layout.HBox
import scalafx.scene.text.Text import scalafx.scene.text.Text
import nova.monadic_sfx.util.reactive._ import monix.{eval => me}
import org.gerweck.scalafx.util._
import scalafx.beans.property.ObjectProperty
import nova.monadic_sfx.implicits.MenuItem
import monix.execution.cancelables.CompositeCancelable
object TodoListView { object TodoListView {
def apply( def apply(
@ -24,67 +29,99 @@ object TodoListView {
TodoListStore.Command, TodoListStore.Command,
(TodoListStore.Command, TodoListStore.State) (TodoListStore.Command, TodoListStore.State)
] ]
): Task[JFXListView[Todo]] = ): Task[Node] =
Task.deferAction(implicit s => Task.deferAction(implicit s =>
Task { Task {
val cc = CompositeCancelable() val cc = CompositeCancelable()
val todos = val todos =
store.map { case (_, state) => state.todos } store.map { case (_, state) => state.todos }
// Todo(-1, "").some
val _selectedItems = ObjectProperty(Seq.empty[Todo])
new JFXListView[Todo] { new HBox {
def selectedItems = selectionModel().selectedItems.view padding = Insets(5)
val _content = StringProperty("")
children = Seq(
new JFXTextField {
text ==> _content
},
new JFXListView[Todo] {
def selectedItems = selectionModel().selectedItems.view
cc += items <-- todos cc += items <-- todos
val emptyCell = ObjectProperty(new HBox) val emptyCell = ObjectProperty(new HBox)
cellFactory = _ => cellFactory = _ =>
new ListCell[Todo] { new ListCell[Todo] {
val _text = StringProperty("") val _text = StringProperty("")
val _graphic = ObjectProperty( val _graphic = ObjectProperty(
new HBox { new HBox {
children = Seq( children = Seq(
new FontIcon { new FontIcon {
iconSize = 10 iconSize = 10
iconLiteral = IconLiteral.Gmi10k iconLiteral = IconLiteral.Gmi10k
}, },
new Text { new Text {
text <== _text text <== _text
}
)
} }
) )
item.asOption.map(
_.fold("")(todo => s"${todo.id} - ${todo.content}")
) ==> _text
graphic <== item.asOption.flatMap(
_.fold(emptyCell)(_ => _graphic)
)
} }
)
item.asOption.map( selectionModel().selectionMode = SelectionMode.Multiple
_.fold("")(todo => s"${todo.id} - ${todo.content}") selectionModel().selectedItems.observableSeqValue ==> _selectedItems
) --> _text
graphic <== item.asOption.flatMap( contextMenu = new ContextMenu {
_.fold(emptyCell)(_ => _graphic) items ++= Seq(
) new MenuItem {
text = "Add"
} // obsAction.useLazyEval(TodoListStore.Add("blah3")) --> store
},
selectionModel().selectionMode = SelectionMode.Multiple new MenuItem {
text = "Delete"
contextMenu = new ContextMenu { obsAction.useIterableEval(_ =>
items ++= Seq( selectedItems
new MenuItem { .map(todo => TodoListStore.Delete(todo.id))
text = "Add" .toList
obsAction.useLazy(TodoListStore.Add("blah3")) --> store ) --> store
}, },
new MenuItem { new MenuItem {
text = "Delete" text = "Edit"
obsAction.useIterable(_ => }
selectedItems )
.map(todo => TodoListStore.Delete(todo.id))
.toList
) --> store
},
new MenuItem {
text = "Edit"
} }
) },
} new JFXButton {
text = "Add"
disable <== _selectedItems.map(_.length > 0)
obsAction
.useLazyEval(me.Task(TodoListStore.Add(_content()))) --> store
},
new JFXButton {
text = "Edit"
disable <== _selectedItems.map(_.length > 1)
obsAction.useLazyEval(
me.Task(
TodoListStore.Edit(
_selectedItems
.map(_.headOption.map(_.id).getOrElse(-1))
.value,
_content()
)
)
) --> store
}
)
} }
} }
) )