1
1
package com .github .opengrabeso .loctio
2
2
3
- import java .awt .{AWTException , Desktop }
3
+ import java .awt .{Desktop , AWTException }
4
4
import java .net .URL
5
5
import java .time .{ZoneId , ZonedDateTime }
6
6
import java .time .format ._
7
- import java .util .Locale
8
-
9
- import akka .actor .{ActorSystem , Cancellable }
7
+ import java .util .{Locale , Timer , TimerTask }
10
8
import com .github .opengrabeso .loctio .common .PublicIpAddress
11
9
import com .github .opengrabeso .github .model .Notification
12
10
import com .github .opengrabeso .github .rest .AuthorizedAPI
13
11
import com .github .opengrabeso .github .{RestAPIClient => GitHubAPIClient }
12
+
14
13
import javax .swing .SwingUtilities
15
14
import javax .imageio .ImageIO
16
- import java .awt .{Image , SystemTray , TrayIcon }
17
-
15
+ import java .awt .{TrayIcon , SystemTray , Image }
18
16
import io .udash .rest .SttpRestClient
19
17
import rest .{RestAPI , RestAPIClient }
20
18
21
19
import scala .concurrent .duration .Duration
22
- import scala .concurrent .{Await , Future , Promise , duration }
20
+ import scala .concurrent .{duration , Await , ExecutionContext , Future , Promise }
23
21
import scala .swing ._
24
22
import scala .swing .Swing ._
25
23
import common .ChainingSyntax ._
@@ -31,15 +29,39 @@ import scala.swing.event.MouseClicked
31
29
32
30
object Start extends SimpleSwingApplication {
33
31
34
- implicit val system : ActorSystem = ActorSystem ()
32
+ type Task = TimerTask
33
+
34
+ object scheduler {
35
+ private val timer = new Timer ()
35
36
36
- val exitEvent = Promise [Boolean ]()
37
+ def scheduleOnce (delay : Duration )(task : => Unit )(implicit ec : ExecutionContext ): TimerTask = {
38
+ val timerTask = new TimerTask {
39
+ override def run (): Unit = {
40
+ ec.future(task)
41
+ }
42
+ }
43
+ timer.schedule(timerTask, delay.toMillis)
44
+ timerTask
45
+ }
46
+
47
+ def scheduleAtFixedRate (initialDelay : Duration , period : Duration )(task : () => Unit )(implicit ec : ExecutionContext ): TimerTask = {
48
+ val timerTask = new TimerTask {
49
+ override def run (): Unit = {
50
+ ec.future {
51
+ task()
52
+ }
53
+ }
54
+ }
55
+ timer.scheduleAtFixedRate(timerTask, initialDelay.toMillis, period.toMillis)
56
+ timerTask
57
+ }
58
+ }
37
59
38
60
private var cfg = Config .empty
39
61
private var loginName = " "
40
62
private var usersReady = false
41
- private var updateSchedule : Cancellable = _
42
- private var notificationsSchedule : Cancellable = _
63
+ private var updateSchedule : Task = _
64
+ private var notificationsSchedule : Task = _
43
65
private var serverUrl : String = _
44
66
45
67
var lastNotifications = Option .empty[String ]
@@ -91,7 +113,7 @@ object Start extends SimpleSwingApplication {
91
113
tryLocalServer(ServerLocal8080 )
92
114
}
93
115
94
- system. scheduler.scheduleOnce(Duration (2000 , duration.MILLISECONDS )) {
116
+ scheduler.scheduleOnce(Duration (2000 , duration.MILLISECONDS )) {
95
117
serverFound.trySuccess(ServerProduction )
96
118
}
97
119
@@ -134,7 +156,7 @@ object Start extends SimpleSwingApplication {
134
156
cancelNotifications()
135
157
136
158
def requestNextAfter (seconds : Int ) = {
137
- notificationsSchedule = system. scheduler.scheduleOnce(Duration (seconds, duration.SECONDS )){
159
+ notificationsSchedule = scheduler.scheduleOnce(Duration (seconds, duration.SECONDS )){
138
160
requestNotifications(token)
139
161
}(OnSwing )
140
162
}
@@ -166,7 +188,7 @@ object Start extends SimpleSwingApplication {
166
188
loginName = s
167
189
println(s " Login as $role done $s for " )
168
190
// request users regularly
169
- updateSchedule = system. scheduler.scheduleAtFixedRate(Duration (0 , duration.MINUTES ), Duration (1 , duration.MINUTES )) {() =>
191
+ updateSchedule = scheduler.scheduleAtFixedRate(Duration (0 , duration.MINUTES ), Duration (1 , duration.MINUTES )) {() =>
170
192
requestUsers.at(OnSwing ).foreach { case (users, tooltip) =>
171
193
if (token == cfg.token) { // ignore any pending futures with a different token
172
194
usersReady = true
0 commit comments