Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cats.*
- import cats.effect.*
- import cats.effect.kernel.*
- import cats.effect.std.Dispatcher
- import cats.effect.unsafe.Scheduler
- import cats.syntax.all.*
- import scala.concurrent.duration.*
- import scala.language.adhocExtensions
- import scala.swing.*
- import scala.swing.event.*
- type DependencyF[F[_], G[_[_]]] = DeferredSource[F, G[F]]
- transparent trait Dependent[F[_]]:
- type Dependency[G[_[_]]] = DependencyF[F, G]
- protected def dispatcher: Dispatcher[F]
- inline final protected def withDependency[G[_[_]]]: Applicator[G] = new Applicator[G]
- protected final class Applicator[G[_[_]]]:
- inline def apply[R](f: G[F] => F[R])
- (ui: R => Unit)
- (using dependency: Dependency[G], ev: Monad[F]): Unit =
- dispatcher.unsafeRunAndForget {
- for {
- value <- dependency.get
- result <- f(value)
- } yield Swing.onEDT(ui(result))
- }
- end apply
- end Applicator
- end Dependent
- class Editor[F[_]: Monad](
- scheduler: Scheduler,
- val dispatcher: Dispatcher[F]
- )(using DependencyF[F, DishDao]) extends BoxPanel(Orientation.Vertical), Dependent[F]:
- private val mainDish = new TextField() {
- columns = 5
- }
- private val auxDishes = new TextArea() {
- columns = 15
- }
- private val done = Label("")
- private val buttonAdd = Button("Add") {
- val mainName = mainDish.text
- val auxNames = auxDishes.text.split(", ") to Set
- val dish = Dish.Main(mainName, auxNames.map(Dish.Aux.apply))
- withDependency[DishDao](_.addDish(dish)) { _ =>
- done.text = "Done!"
- scheduler.sleep(
- 800 millis span,
- () => Swing.onEDT {
- done.text = ""
- }
- )
- }
- }
- contents.addOne(
- FlowPanel(FlowPanel.Alignment.Right)(
- Label("Main dish"), mainDish,
- Label("Aux dishes"), auxDishes,
- done, buttonAdd,
- ),
- )
- end Editor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement