You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.3 KiB

  1. package nova.monadic_sfx.ui.components.todo
  2. import monix.bio.Task
  3. import monix.execution.cancelables.CompositeCancelable
  4. import monix.{eval => me}
  5. import nova.monadic_sfx.util.controls.FontIcon
  6. import nova.monadic_sfx.util.controls.IconLiteral
  7. import nova.monadic_sfx.util.controls.JFXButton
  8. import nova.monadic_sfx.util.controls.JFXListView
  9. import nova.monadic_sfx.util.controls.JFXTextField
  10. import nova.monadic_sfx.util.controls.MenuItem
  11. import nova.monadic_sfx.implicits._
  12. import nova.monadic_sfx.util.reactive.store._
  13. import org.gerweck.scalafx.util._
  14. import scalafx.Includes._
  15. import scalafx.beans.property.ObjectProperty
  16. import scalafx.beans.property.StringProperty
  17. import scalafx.geometry.Insets
  18. import scalafx.scene.Parent
  19. import scalafx.scene.control.ContextMenu
  20. import scalafx.scene.control.ListCell
  21. import scalafx.scene.control.SelectionMode
  22. import scalafx.scene.layout.HBox
  23. import scalafx.scene.text.Text
  24. object TodoListView {
  25. def apply(
  26. store: Store[TodoListStore.Action, TodoListStore.State]
  27. ): Task[Parent] =
  28. Task.deferAction(implicit s =>
  29. Task {
  30. val cc = CompositeCancelable()
  31. val todos = store.map { case (_, state) => state.todos }
  32. val _selectedItems = ObjectProperty(Seq.empty[Todo])
  33. new HBox {
  34. padding = Insets(5)
  35. val _content = StringProperty("")
  36. children = Seq(
  37. new JFXTextField {
  38. text ==> _content
  39. },
  40. new JFXListView[Todo] {
  41. def selectedItems = selectionModel().selectedItems.view
  42. selectionModel().selectionMode = SelectionMode.Multiple
  43. selectionModel().selectedItems.observableSeqValue ==> _selectedItems
  44. cc += items <-- todos
  45. val emptyCell = ObjectProperty(new HBox)
  46. cellFactory = _ =>
  47. new ListCell[Todo] {
  48. val _text = StringProperty("")
  49. val _graphic = ObjectProperty(
  50. new HBox {
  51. children = Seq(
  52. new FontIcon {
  53. iconSize = 10
  54. iconLiteral = IconLiteral.Gmi10k
  55. },
  56. new Text {
  57. text <== _text
  58. }
  59. )
  60. }
  61. )
  62. item.asOption.map(
  63. _.fold("")(todo => s"${todo.id} - ${todo.content}")
  64. ) ==> _text
  65. graphic <== item.asOption.flatMap(
  66. _.fold(emptyCell)(_ => _graphic)
  67. )
  68. }
  69. contextMenu = new ContextMenu {
  70. items ++= Seq(
  71. new MenuItem {
  72. text = "Add"
  73. // obsAction.useLazyEval(TodoListStore.Add("blah3")) --> store
  74. },
  75. new MenuItem {
  76. text = "Delete"
  77. obsAction.useIterableEval(_ =>
  78. selectedItems
  79. .map(todo => TodoListStore.Delete(todo.id))
  80. .toList
  81. ) --> store
  82. // obsAction.split(
  83. // _.useLazyEval(me.Task(TodoListStore.Delete(0))) --> store,
  84. // _.useLazyEval(me.Task(TodoListStore.Delete(0))) --> store,
  85. // _.useLazyEval(me.Task(TodoListStore.Delete(0))) --> store
  86. // )
  87. },
  88. new MenuItem {
  89. text = "Edit"
  90. }
  91. )
  92. }
  93. },
  94. new JFXButton {
  95. text = "Add"
  96. // disable <== _selectedItems.map(_.length > 0)
  97. obsAction
  98. .useLazyEval(me.Task(TodoListStore.Add(_content()))) --> store
  99. },
  100. new JFXButton {
  101. text = "Edit"
  102. disable <== _selectedItems.map(_.length > 1)
  103. obsAction.useLazyEval(
  104. me.Task(
  105. TodoListStore.Edit(
  106. _selectedItems
  107. .map(_.headOption.map(_.id).getOrElse(-1))
  108. .value,
  109. _content()
  110. )
  111. )
  112. ) --> store
  113. }
  114. )
  115. }
  116. }
  117. )
  118. }