|
|
@ -19,6 +19,7 @@
|
|
|
|
package org.isoron.uhabits.core.utils
|
|
|
|
package org.isoron.uhabits.core.utils
|
|
|
|
|
|
|
|
|
|
|
|
import org.isoron.uhabits.core.AppScope
|
|
|
|
import org.isoron.uhabits.core.AppScope
|
|
|
|
|
|
|
|
import org.isoron.uhabits.core.io.Logging
|
|
|
|
import java.util.LinkedList
|
|
|
|
import java.util.LinkedList
|
|
|
|
import java.util.concurrent.Executors
|
|
|
|
import java.util.concurrent.Executors
|
|
|
|
import java.util.concurrent.ScheduledExecutorService
|
|
|
|
import java.util.concurrent.ScheduledExecutorService
|
|
|
@ -29,9 +30,10 @@ import javax.inject.Inject
|
|
|
|
* A class that emits events when a new day starts.
|
|
|
|
* A class that emits events when a new day starts.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@AppScope
|
|
|
|
@AppScope
|
|
|
|
open class MidnightTimer @Inject constructor() {
|
|
|
|
open class MidnightTimer @Inject constructor(logging: Logging) {
|
|
|
|
private val listeners: MutableList<MidnightListener> = LinkedList()
|
|
|
|
private val listeners: MutableList<MidnightListener> = LinkedList()
|
|
|
|
private lateinit var executor: ScheduledExecutorService
|
|
|
|
private lateinit var executor: ScheduledExecutorService
|
|
|
|
|
|
|
|
private val logger = logging.getLogger("MidnightTimer")
|
|
|
|
|
|
|
|
|
|
|
|
@Synchronized
|
|
|
|
@Synchronized
|
|
|
|
fun addListener(listener: MidnightListener) {
|
|
|
|
fun addListener(listener: MidnightListener) {
|
|
|
@ -39,7 +41,10 @@ open class MidnightTimer @Inject constructor() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Synchronized
|
|
|
|
@Synchronized
|
|
|
|
fun onPause(): MutableList<Runnable>? = executor.shutdownNow()
|
|
|
|
fun onPause(): MutableList<Runnable>? {
|
|
|
|
|
|
|
|
logger.info("Pausing timer")
|
|
|
|
|
|
|
|
return executor.shutdownNow()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Synchronized
|
|
|
|
@Synchronized
|
|
|
|
fun onResume(
|
|
|
|
fun onResume(
|
|
|
@ -47,9 +52,11 @@ open class MidnightTimer @Inject constructor() {
|
|
|
|
testExecutor: ScheduledExecutorService? = null
|
|
|
|
testExecutor: ScheduledExecutorService? = null
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
executor = testExecutor ?: Executors.newSingleThreadScheduledExecutor()
|
|
|
|
executor = testExecutor ?: Executors.newSingleThreadScheduledExecutor()
|
|
|
|
|
|
|
|
val initialDelay = DateUtils.millisecondsUntilTomorrowWithOffset() + delayOffsetInMillis
|
|
|
|
|
|
|
|
logger.info("Scheduling refresh for $initialDelay ms from now")
|
|
|
|
executor.scheduleAtFixedRate(
|
|
|
|
executor.scheduleAtFixedRate(
|
|
|
|
{ notifyListeners() },
|
|
|
|
{ notifyListeners() },
|
|
|
|
DateUtils.millisecondsUntilTomorrowWithOffset() + delayOffsetInMillis,
|
|
|
|
initialDelay,
|
|
|
|
DateUtils.DAY_LENGTH,
|
|
|
|
DateUtils.DAY_LENGTH,
|
|
|
|
TimeUnit.MILLISECONDS
|
|
|
|
TimeUnit.MILLISECONDS
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -60,6 +67,7 @@ open class MidnightTimer @Inject constructor() {
|
|
|
|
|
|
|
|
|
|
|
|
@Synchronized
|
|
|
|
@Synchronized
|
|
|
|
private fun notifyListeners() {
|
|
|
|
private fun notifyListeners() {
|
|
|
|
|
|
|
|
logger.info("Midnight refresh")
|
|
|
|
for (l in listeners) {
|
|
|
|
for (l in listeners) {
|
|
|
|
l.atMidnight()
|
|
|
|
l.atMidnight()
|
|
|
|
}
|
|
|
|
}
|
|
|
|