Wrapped login screen code in Task

This commit is contained in:
Rohan Sircar 2020-08-29 13:28:15 +05:30
parent fe92f4edfb
commit af065a7589
2 changed files with 90 additions and 39 deletions

View File

@ -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,24 +72,28 @@ 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() println(
} s"Application started successfully in ${duration.toSeconds} seconds"
case Success((duration, _)) => )
println(
s"Application started successfully in ${duration.toSeconds} seconds"
)
} }
)
override def onError(e: Throwable): Unit = {
println("Application start failed. Reason -")
e.printStackTrace()
}
}
)
// Task // Task
// .suspend { // .suspend {

View File

@ -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 ()
stage
.scene() private def onLogin(stage: PrimaryStage) =
.setRoot(HomePage(backend, system, () => onLogout(appStage))) Task.deferAction { implicit Scheduler =>
} for {
private lazy val root = new VBox { _ <- Task(println("logging in"))
children = Seq( root <- Task {
new TextField { stage
text = "username" .scene()
editable = true .setRoot(
}, HomePage(
new TextField { backend,
text = "password" system,
}, () => runFxTask(onLogout(appStage))
new Button { )
text = "Login" )
onAction = () => onLogin(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
} }
def render: Parent = root
} }