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.

102 lines
2.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package nova.monadic_sfx.pages
  2. import nova.monadic_sfx.AppTypes
  3. import scalafx.scene.control.TextField
  4. import scalafx.scene.control._
  5. import scalafx.scene.layout.VBox
  6. import scalafx.scene.Node
  7. import scalafx.Includes._
  8. import scalafx.scene.Parent
  9. import scalafx.application.JFXApp.PrimaryStage
  10. import nova.monadic_sfx.http.requests.DummyRequest
  11. import monix.eval.Task
  12. import monix.execution.Scheduler
  13. // import io.odin.syntax._
  14. // import _root_.monix.eval.Task
  15. // import io.odin.monix._
  16. // import javafx.beans.property.ObjectProperty
  17. // import javafx.event.{ActionEvent, EventHandler}
  18. class LoginPage(
  19. appStage: PrimaryStage,
  20. backend: AppTypes.HttpBackend,
  21. system: akka.actor.ActorSystem
  22. ) {
  23. val dummyRequester = new DummyRequest(backend)
  24. //pure function callbacks, but with side effects still
  25. private def onLogout(stage: PrimaryStage) =
  26. for {
  27. _ <- Task { println("logging out") }
  28. root <- render
  29. _ <- Task(stage.scene().setRoot(root))
  30. } yield ()
  31. private def onLogin(stage: PrimaryStage) =
  32. Task.deferAction { implicit Scheduler =>
  33. for {
  34. _ <- Task(println("logging in"))
  35. root <- Task {
  36. stage
  37. .scene()
  38. .setRoot(
  39. HomePage(
  40. backend,
  41. system,
  42. () => runFxTask(onLogout(appStage))
  43. )
  44. )
  45. }
  46. } yield ()
  47. }
  48. private lazy val root = Task.deferAction(implicit scheduler =>
  49. Task {
  50. new VBox {
  51. children = Seq(
  52. new Label {
  53. text = "username"
  54. },
  55. new TextField(),
  56. new Label {
  57. text = "password"
  58. },
  59. new TextField(),
  60. new Button {
  61. text = "Login"
  62. onAction = () =>
  63. runFxTask {
  64. Task
  65. .parZip2(
  66. dummyRequester
  67. .send(),
  68. // .executeOn(Scheduler.global)
  69. onLogin(appStage)
  70. )
  71. }
  72. }
  73. )
  74. }
  75. }
  76. )
  77. def render: Task[Parent] = root
  78. /**
  79. * Implicitly runs monix task as fire and forget. \
  80. * For use in ScalaFX callbacks.
  81. *
  82. * @param task
  83. * @param s
  84. */
  85. def runFxTask[T](task: => Task[T])(implicit s: Scheduler) = {
  86. task.runAsyncAndForget
  87. }
  88. }
  89. object LoginPage {
  90. def apply(
  91. appStage: PrimaryStage,
  92. backend: AppTypes.HttpBackend,
  93. system: akka.actor.ActorSystem
  94. ) = new LoginPage(appStage, backend, system).render
  95. }