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

package nova.monadic_sfx.pages
import nova.monadic_sfx.AppTypes
import scalafx.scene.control.TextField
import scalafx.scene.control._
import scalafx.scene.layout.VBox
import scalafx.scene.Node
import scalafx.Includes._
import scalafx.scene.Parent
import scalafx.application.JFXApp.PrimaryStage
import nova.monadic_sfx.http.requests.DummyRequest
import monix.eval.Task
import monix.execution.Scheduler
// import io.odin.syntax._
// import _root_.monix.eval.Task
// import io.odin.monix._
// import javafx.beans.property.ObjectProperty
// import javafx.event.{ActionEvent, EventHandler}
class LoginPage(
appStage: PrimaryStage,
backend: AppTypes.HttpBackend,
system: akka.actor.ActorSystem
) {
val dummyRequester = new DummyRequest(backend)
//pure function callbacks, but with side effects still
private def onLogout(stage: PrimaryStage) =
for {
_ <- Task { println("logging out") }
root <- render
_ <- Task(stage.scene().setRoot(root))
} yield ()
private def onLogin(stage: PrimaryStage) =
Task.deferAction { implicit Scheduler =>
for {
_ <- Task(println("logging in"))
root <- Task {
stage
.scene()
.setRoot(
HomePage(
backend,
system,
() => runFxTask(onLogout(appStage))
)
)
}
} yield ()
}
private lazy val root = Task.deferAction(implicit scheduler =>
Task {
new VBox {
children = Seq(
new Label {
text = "username"
},
new TextField(),
new Label {
text = "password"
},
new TextField(),
new Button {
text = "Login"
onAction = () =>
runFxTask {
Task
.parZip2(
dummyRequester
.send(),
// .executeOn(Scheduler.global)
onLogin(appStage)
)
}
}
)
}
}
)
def render: Task[Parent] = root
/**
* Implicitly runs monix task as fire and forget. \
* For use in ScalaFX callbacks.
*
* @param task
* @param s
*/
def runFxTask[T](task: => Task[T])(implicit s: Scheduler) = {
task.runAsyncAndForget
}
}
object LoginPage {
def apply(
appStage: PrimaryStage,
backend: AppTypes.HttpBackend,
system: akka.actor.ActorSystem
) = new LoginPage(appStage, backend, system).render
}