Wrapped login screen code in Task
This commit is contained in:
parent
fe92f4edfb
commit
af065a7589
@ -30,6 +30,7 @@ import io.odin.syntax._
|
|||||||
// import io.odin._
|
// import io.odin._
|
||||||
import io.odin.monix._
|
import io.odin.monix._
|
||||||
import nova.monadic_sfx.pages.HomePage
|
import nova.monadic_sfx.pages.HomePage
|
||||||
|
import monix.execution.Callback
|
||||||
|
|
||||||
object Main extends JFXApp with MainModule {
|
object Main extends JFXApp with MainModule {
|
||||||
|
|
||||||
@ -60,9 +61,10 @@ object Main extends JFXApp with MainModule {
|
|||||||
stage = appStage
|
stage = appStage
|
||||||
}
|
}
|
||||||
// wait 2 seconds before showing home screen
|
// wait 2 seconds before showing home screen
|
||||||
d <- deps
|
// d <- deps
|
||||||
fib1 <- d.send().start
|
// fib1 <- d.send().start
|
||||||
_ <- Task.sleep(2.seconds)
|
_ <- Task.sleep(2.seconds)
|
||||||
|
loginScene <- LoginPage(appStage, backend, actorSystem)
|
||||||
_ <- Task {
|
_ <- Task {
|
||||||
// appStage.maximized = true
|
// appStage.maximized = true
|
||||||
appStage.height = 800
|
appStage.height = 800
|
||||||
@ -70,23 +72,27 @@ object Main extends JFXApp with MainModule {
|
|||||||
appStage
|
appStage
|
||||||
.scene()
|
.scene()
|
||||||
.setRoot(
|
.setRoot(
|
||||||
LoginPage(appStage, backend, actorSystem)
|
loginScene
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// _ <- fib1.join
|
// _ <- fib1.join
|
||||||
} yield ()
|
} yield ()
|
||||||
application.timed.runToFuture
|
application.timed.runAsync(
|
||||||
.onComplete(res =>
|
new Callback[Throwable, (FiniteDuration, Unit)] {
|
||||||
res match {
|
|
||||||
case Failure(exception) => {
|
override def onSuccess(value: (FiniteDuration, Unit)): Unit = {
|
||||||
println("Application start failed. Reason -")
|
val (duration, _) = value
|
||||||
exception.printStackTrace()
|
|
||||||
}
|
|
||||||
case Success((duration, _)) =>
|
|
||||||
println(
|
println(
|
||||||
s"Application started successfully in ${duration.toSeconds} seconds"
|
s"Application started successfully in ${duration.toSeconds} seconds"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override def onError(e: Throwable): Unit = {
|
||||||
|
println("Application start failed. Reason -")
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Task
|
// Task
|
||||||
|
@ -7,6 +7,9 @@ import scalafx.scene.Node
|
|||||||
import scalafx.Includes._
|
import scalafx.Includes._
|
||||||
import scalafx.scene.Parent
|
import scalafx.scene.Parent
|
||||||
import scalafx.application.JFXApp.PrimaryStage
|
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 io.odin.syntax._
|
||||||
// import _root_.monix.eval.Task
|
// import _root_.monix.eval.Task
|
||||||
// import io.odin.monix._
|
// import io.odin.monix._
|
||||||
@ -18,33 +21,75 @@ class LoginPage(
|
|||||||
system: akka.actor.ActorSystem
|
system: akka.actor.ActorSystem
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
val dummyRequester = new DummyRequest(backend)
|
||||||
|
|
||||||
//pure function callbacks, but with side effects still
|
//pure function callbacks, but with side effects still
|
||||||
private def onLogout(stage: PrimaryStage) = {
|
private def onLogout(stage: PrimaryStage) =
|
||||||
println("logging out")
|
for {
|
||||||
stage.scene().setRoot(render)
|
_ <- Task { println("logging out") }
|
||||||
}
|
root <- render
|
||||||
private def onLogin(stage: PrimaryStage) = {
|
_ <- Task(stage.scene().setRoot(root))
|
||||||
println("logging in")
|
} yield ()
|
||||||
|
|
||||||
|
private def onLogin(stage: PrimaryStage) =
|
||||||
|
Task.deferAction { implicit Scheduler =>
|
||||||
|
for {
|
||||||
|
_ <- Task(println("logging in"))
|
||||||
|
root <- Task {
|
||||||
stage
|
stage
|
||||||
.scene()
|
.scene()
|
||||||
.setRoot(HomePage(backend, system, () => onLogout(appStage)))
|
.setRoot(
|
||||||
|
HomePage(
|
||||||
|
backend,
|
||||||
|
system,
|
||||||
|
() => runFxTask(onLogout(appStage))
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
private lazy val root = new VBox {
|
} yield ()
|
||||||
|
}
|
||||||
|
|
||||||
|
private lazy val root = Task.deferAction(implicit scheduler =>
|
||||||
|
Task {
|
||||||
|
new VBox {
|
||||||
children = Seq(
|
children = Seq(
|
||||||
new TextField {
|
new Label {
|
||||||
text = "username"
|
text = "username"
|
||||||
editable = true
|
|
||||||
},
|
},
|
||||||
new TextField {
|
new TextField(),
|
||||||
|
new Label {
|
||||||
text = "password"
|
text = "password"
|
||||||
},
|
},
|
||||||
|
new TextField(),
|
||||||
new Button {
|
new Button {
|
||||||
text = "Login"
|
text = "Login"
|
||||||
onAction = () => onLogin(appStage)
|
onAction = () =>
|
||||||
|
runFxTask {
|
||||||
|
Task
|
||||||
|
.parZip2(
|
||||||
|
dummyRequester
|
||||||
|
.send(),
|
||||||
|
// .executeOn(Scheduler.global)
|
||||||
|
onLogin(appStage)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
def render: Parent = root
|
}
|
||||||
|
)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user