Converted implicit classes to implicit methods

This commit is contained in:
Rohan Sircar 2020-12-20 15:59:09 +05:30
parent 80b32b063e
commit 7aa80f54f7
8 changed files with 56 additions and 65 deletions

View File

@ -12,7 +12,7 @@ import monix.eval.Coeval
import monix.{eval => me}
import nova.monadic_sfx.executors.Schedulers
import nova.monadic_sfx.implicits.JFXButton
import nova.monadic_sfx.implicits.JavaFXMonixObservables._
import nova.monadic_sfx.implicits._
import nova.monadic_sfx.ui.MyFxApp
import nova.monadic_sfx.ui.components.router.FXRouter
import nova.monadic_sfx.ui.components.router.Page

View File

@ -37,6 +37,6 @@ class JFXButton(
}
def obsAction =
new ActionObservableBuilder(this.observableAction())
new ActionObservableBuilder(this.observableAction)
}

View File

@ -27,10 +27,32 @@ import scalafx.scene.Scene
import scalafx.scene.control.ButtonBase
import scalafx.scene.control.MenuItem
trait JavaFXMonixObservables {
import JavaFXMonixObservables._
implicit def extendedScene(scene: Scene) = new SceneExt(scene)
implicit def extendedProperty[T, J](
propery: Property[T, J]
): PropertyExt[T, J] =
new PropertyExt(propery)
implicit def extendedObjectPropety[A](prop: ObjectProperty[A]) =
new ObjectPropertyExt[A](prop)
implicit def extendedReadOnlyObjectPropety[T, J](
prop: ReadOnlyProperty[T, J]
) =
new ReadOnlyPropertyExt[T, J](prop)
implicit def extendedObservableList[A](
list: ObservableList[A]
) = new ObservableListExt(list)
implicit def extendedObjectPropertyObservableList[A](
prop: ObjectProperty[ObservableList[A]]
) = new ObjectPropertyObservableListExt(prop)
implicit def extendedButton(button: ButtonBase) = new ButtonBaseExt(button)
implicit def extendedMenuItem(item: MenuItem) = new MenuItemExt(item)
}
object JavaFXMonixObservables {
implicit final class SceneObservables(private val scene: Scene)
extends AnyVal {
final class SceneExt(private val scene: Scene) extends AnyVal {
def observableMousePressed(): Observable[jfxsi.MouseEvent] = {
import monix.execution.cancelables.SingleAssignCancelable
@ -75,7 +97,7 @@ object JavaFXMonixObservables {
}
}
implicit final class BindObs[T, J](private val prop: Property[T, J])
final class PropertyExt[T, J](private val prop: Property[T, J])
extends AnyVal {
def -->(op: Observer[T]) = {
op.onNext(prop.value)
@ -107,7 +129,7 @@ object JavaFXMonixObservables {
}
}
implicit final class BindObs2[A](private val prop: ObjectProperty[A])
final class ObjectPropertyExt[A](private val prop: ObjectProperty[A])
extends AnyVal {
def -->(sub: Observer[A]) =
@ -136,7 +158,7 @@ object JavaFXMonixObservables {
}
}
implicit final class ObservableListExt[A](
final class ObservableListExt[A](
private val buffer: ObservableList[A]
) extends AnyVal {
@ -189,8 +211,9 @@ object JavaFXMonixObservables {
} else Task.unit
}
implicit final class BindObs3[T, J](private val prop: ReadOnlyProperty[T, J])
extends AnyVal {
final class ReadOnlyPropertyExt[T, J](
private val prop: ReadOnlyProperty[T, J]
) extends AnyVal {
def -->(op: Observer[T]) = {
op.onNext(prop.value)
}
@ -214,7 +237,7 @@ object JavaFXMonixObservables {
}
}
implicit final class ObjectPropertyObservableListExt[A](
final class ObjectPropertyObservableListExt[A](
private val prop: ObjectProperty[ObservableList[A]]
) extends AnyVal {
def <--(obs: Observable[Seq[A]])(implicit s: Scheduler) = {
@ -259,7 +282,7 @@ object JavaFXMonixObservables {
}
implicit final class ObjectPropertyActionEvent(
final class ObjectPropertyActionEvent(
private val prop: ObjectProperty[EventHandler[ActionEvent]]
) extends AnyVal {
// def <--(obs: Observable[ActionEvent])(implicit s: Scheduler) = {
@ -273,7 +296,7 @@ object JavaFXMonixObservables {
// def emit(prop: ObjectProperty[EventHandler[ActionEvent]]) =
implicit final class OnActionObservable(
final class ButtonBaseExt(
private val button: ButtonBase
) extends AnyVal {
@ -299,7 +322,7 @@ object JavaFXMonixObservables {
}
}
implicit final class MenuItemActionObservable(
final class MenuItemExt(
private val item: MenuItem
) extends AnyVal {

View File

@ -1,9 +1,7 @@
package nova.monadic_sfx.implicits
import nova.monadic_sfx.implicits._
import scalafx.scene.{control => sfxc}
import JavaFXMonixObservables._
class MenuItem extends sfxc.MenuItem {
def obsAction = new ActionObservableBuilder(this.observableAction)
}

View File

@ -1,50 +1,20 @@
package nova.monadic_sfx
import javafx.event.ActionEvent
import monix.execution.Ack
import monix.execution.Cancelable
import monix.reactive.Observable
import monix.reactive.OverflowStrategy
import scalafx.scene.control._
package object implicits
extends MySfxObservableImplicits
with JavaFXMonixObservables
package object implicits extends MySfxObservableImplicits {
// implicit class NodeExt(val node: Node) {
// def lookup2[T <: SFXDelegate[_]](
// selector: String
// )(implicit c: ClassTag[T]) = {
// val t = c.runtimeClass
// Option(node.delegate.lookup(selector)) match {
// case Some(value) =>
// if (value.getClass == t) Some(value) else None
// case None => None
// }
// }
implicit class MyButtonExt(val button: Button) extends AnyVal {
def observableAction(): Observable[ActionEvent] = {
import monix.execution.cancelables.SingleAssignCancelable
Observable.create(OverflowStrategy.Unbounded) { sub =>
val c = SingleAssignCancelable()
val l = new javafx.event.EventHandler[ActionEvent] {
override def handle(event: ActionEvent): Unit = {
if (sub.onNext(event) == Ack.Stop) c.cancel()
}
}
button.onAction = l
c := Cancelable(() =>
button.removeEventHandler(
ActionEvent.ACTION,
l
)
)
c
}
}
}
// implicit class NodeExt(val node: Node) {
// def lookup2[T <: SFXDelegate[_]](
// selector: String
// )(implicit c: ClassTag[T]) = {
// val t = c.runtimeClass
// Option(node.delegate.lookup(selector)) match {
// case Some(value) =>
// if (value.getClass == t) Some(value) else None
// case None => None
// }
// }
// val x = node.lookup2("")
// }
}
// val x = node.lookup2("")
// }

View File

@ -13,7 +13,7 @@ import monix.reactive.Observer
import nova.monadic_sfx.implicits.FontIcon
import nova.monadic_sfx.implicits.IconLiteral
import nova.monadic_sfx.implicits.JFXListView
import nova.monadic_sfx.implicits.JavaFXMonixObservables._
import nova.monadic_sfx.implicits._
import nova.monadic_sfx.util.reactive._
import scalafx.Includes._
import scalafx.beans.property.StringProperty

View File

@ -8,8 +8,8 @@ import nova.monadic_sfx.implicits.IconLiteral
import nova.monadic_sfx.implicits.JFXButton
import nova.monadic_sfx.implicits.JFXListView
import nova.monadic_sfx.implicits.JFXTextField
import nova.monadic_sfx.implicits.JavaFXMonixObservables._
import nova.monadic_sfx.implicits.MenuItem
import nova.monadic_sfx.implicits._
import nova.monadic_sfx.util.reactive._
import org.gerweck.scalafx.util._
import scalafx.Includes._

View File

@ -19,7 +19,7 @@ class HomeScreen(
// onAction = () => Action.asyncT(onLogout())
}
val myObs = myButton.observableAction()
val myObs = myButton.observableAction
// myObs.foreachL(_ => ())
private lazy val root = Task.deferAction { implicit s =>
Task {