diff --git a/build.sbt b/build.sbt index eb0fe81..84fd0e8 100644 --- a/build.sbt +++ b/build.sbt @@ -102,7 +102,6 @@ lazy val trayUtil = (project in file("tray-util")) name := "LoctioStart", commonSettings, flyingSaucersSettings, - libraryDependencies += "com.typesafe.akka" %% "akka-actor-typed" % "2.7.0", libraryDependencies += "com.softwaremill.sttp.client3" %% "okhttp-backend" % "3.9.7", libraryDependencies += "com.twelvemonkeys.imageio" % "imageio-bmp" % "3.10.1", libraryDependencies += "org.scala-lang.modules" %% "scala-swing" % "3.0.0", diff --git a/tray-util/src/main/scala/com/github/opengrabeso/loctio/MouseIdleTime.scala b/tray-util/src/main/scala/com/github/opengrabeso/loctio/MouseIdleTime.scala index 3396183..af849a3 100644 --- a/tray-util/src/main/scala/com/github/opengrabeso/loctio/MouseIdleTime.scala +++ b/tray-util/src/main/scala/com/github/opengrabeso/loctio/MouseIdleTime.scala @@ -30,7 +30,7 @@ object MouseIdleTime { private var lastState: WatchedState = currentState() - Start.system.scheduler.scheduleAtFixedRate(Duration(5, duration.SECONDS), Duration(5, duration.SECONDS)) { () => + Start.scheduler.scheduleAtFixedRate(Duration(5, duration.SECONDS), Duration(5, duration.SECONDS)) { () => val s = currentState() synchronized { if (s != lastState) { diff --git a/tray-util/src/main/scala/com/github/opengrabeso/loctio/Start.scala b/tray-util/src/main/scala/com/github/opengrabeso/loctio/Start.scala index 082084e..06ac9fe 100644 --- a/tray-util/src/main/scala/com/github/opengrabeso/loctio/Start.scala +++ b/tray-util/src/main/scala/com/github/opengrabeso/loctio/Start.scala @@ -1,25 +1,23 @@ package com.github.opengrabeso.loctio -import java.awt.{AWTException, Desktop} +import java.awt.{Desktop, AWTException} import java.net.URL import java.time.{ZoneId, ZonedDateTime} import java.time.format._ -import java.util.Locale - -import akka.actor.{ActorSystem, Cancellable} +import java.util.{Locale, Timer, TimerTask} import com.github.opengrabeso.loctio.common.PublicIpAddress import com.github.opengrabeso.github.model.Notification import com.github.opengrabeso.github.rest.AuthorizedAPI import com.github.opengrabeso.github.{RestAPIClient => GitHubAPIClient} + import javax.swing.SwingUtilities import javax.imageio.ImageIO -import java.awt.{Image, SystemTray, TrayIcon} - +import java.awt.{TrayIcon, SystemTray, Image} import io.udash.rest.SttpRestClient import rest.{RestAPI, RestAPIClient} import scala.concurrent.duration.Duration -import scala.concurrent.{Await, Future, Promise, duration} +import scala.concurrent.{duration, Await, ExecutionContext, Future, Promise} import scala.swing._ import scala.swing.Swing._ import common.ChainingSyntax._ @@ -31,15 +29,39 @@ import scala.swing.event.MouseClicked object Start extends SimpleSwingApplication { - implicit val system: ActorSystem = ActorSystem() + type Task = TimerTask + + object scheduler { + private val timer = new Timer() - val exitEvent = Promise[Boolean]() + def scheduleOnce(delay: Duration)(task: => Unit)(implicit ec: ExecutionContext): TimerTask = { + val timerTask = new TimerTask { + override def run(): Unit = { + ec.future(task) + } + } + timer.schedule(timerTask, delay.toMillis) + timerTask + } + + def scheduleAtFixedRate(initialDelay: Duration, period: Duration)(task: () => Unit)(implicit ec: ExecutionContext): TimerTask = { + val timerTask = new TimerTask { + override def run(): Unit = { + ec.future { + task() + } + } + } + timer.scheduleAtFixedRate(timerTask, initialDelay.toMillis, period.toMillis) + timerTask + } + } private var cfg = Config.empty private var loginName = "" private var usersReady = false - private var updateSchedule: Cancellable = _ - private var notificationsSchedule: Cancellable = _ + private var updateSchedule: Task = _ + private var notificationsSchedule: Task = _ private var serverUrl: String = _ var lastNotifications = Option.empty[String] @@ -91,7 +113,7 @@ object Start extends SimpleSwingApplication { tryLocalServer(ServerLocal8080) } - system.scheduler.scheduleOnce(Duration(2000, duration.MILLISECONDS)) { + scheduler.scheduleOnce(Duration(2000, duration.MILLISECONDS)) { serverFound.trySuccess(ServerProduction) } @@ -134,7 +156,7 @@ object Start extends SimpleSwingApplication { cancelNotifications() def requestNextAfter(seconds: Int) = { - notificationsSchedule = system.scheduler.scheduleOnce(Duration(seconds, duration.SECONDS)){ + notificationsSchedule = scheduler.scheduleOnce(Duration(seconds, duration.SECONDS)){ requestNotifications(token) }(OnSwing) } @@ -166,7 +188,7 @@ object Start extends SimpleSwingApplication { loginName = s println(s"Login as $role done $s for") // request users regularly - updateSchedule = system.scheduler.scheduleAtFixedRate(Duration(0, duration.MINUTES), Duration(1, duration.MINUTES)) {() => + updateSchedule = scheduler.scheduleAtFixedRate(Duration(0, duration.MINUTES), Duration(1, duration.MINUTES)) {() => requestUsers.at(OnSwing).foreach { case (users, tooltip) => if (token == cfg.token) { // ignore any pending futures with a different token usersReady = true