🔀 Merge branch 'release/v82'
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / notification / SoneLockedHandler.kt
index 583601b..cb799a8 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Sone - SoneLockedHandler.kt - Copyright Â© 2019 David â€˜Bombe’ Roden
+ * Sone - SoneLockedHandler.kt - Copyright Â© 2019–2020 David â€˜Bombe’ Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -33,32 +33,32 @@ import javax.inject.*
 class SoneLockedHandler @Inject constructor(
                private val notificationManager: NotificationManager,
                @Named("soneLocked") private val notification: ListNotification<Sone>,
-               private val executor: ScheduledExecutorService) {
+               @Named("notification") private val executor: ScheduledExecutorService) {
 
        private val future: AtomicReference<ScheduledFuture<*>> = AtomicReference()
 
        @Subscribe
        fun soneLocked(soneLockedEvent: SoneLockedEvent) {
                synchronized(future) {
-                       future.get()?.also { cancelPreviousFuture(it, soneLockedEvent.sone) }
-                       future.set(executor.schedule(showNotification(soneLockedEvent.sone), 5, TimeUnit.MINUTES))
+                       notification.add(soneLockedEvent.sone)
+                       future.get()?.also(this::cancelPreviousFuture)
+                       future.set(executor.schedule(::showNotification, 5, TimeUnit.MINUTES))
                }
        }
 
        @Subscribe
        fun soneUnlocked(soneUnlockedEvent: SoneUnlockedEvent) {
                synchronized(future) {
-                       future.get()?.also { cancelPreviousFuture(it, soneUnlockedEvent.sone) }
+                       notification.remove(soneUnlockedEvent.sone)
+                       future.get()?.also(::cancelPreviousFuture)
                }
        }
 
-       private fun cancelPreviousFuture(future: ScheduledFuture<*>, sone: Sone) {
-               notification.remove(sone)
+       private fun cancelPreviousFuture(future: ScheduledFuture<*>) {
                future.cancel(true)
        }
 
-       private fun showNotification(sone: Sone): () -> Unit = {
-               notification.add(sone)
+       private fun showNotification() {
                notificationManager.addNotification(notification)
        }