X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fnotification%2FSoneLockedHandler.kt;h=cb799a8a7743dfd352b57b943336c5c7f8e97078;hp=8bfbda43b96db59d0fd9f47637d82f831ee6604e;hb=438378deab1514f0f608d975ef65f5b7aea44ccb;hpb=b2952fda6d34528489d7fa4e26e09133099c978f diff --git a/src/main/kotlin/net/pterodactylus/sone/web/notification/SoneLockedHandler.kt b/src/main/kotlin/net/pterodactylus/sone/web/notification/SoneLockedHandler.kt index 8bfbda4..cb799a8 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/SoneLockedHandler.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/SoneLockedHandler.kt @@ -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 @@ -24,37 +24,41 @@ import net.pterodactylus.sone.notify.* import net.pterodactylus.util.notify.* import java.util.concurrent.* import java.util.concurrent.atomic.* +import javax.inject.* /** * Handler for [SoneLockedEvent]s and [SoneUnlockedEvent]s that can schedule notifications after * a certain timeout. */ -class SoneLockedHandler(private val notificationManager: NotificationManager, private val notification: ListNotification, private val executor: ScheduledExecutorService) { +class SoneLockedHandler @Inject constructor( + private val notificationManager: NotificationManager, + @Named("soneLocked") private val notification: ListNotification, + @Named("notification") private val executor: ScheduledExecutorService) { private val future: AtomicReference> = 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) }