mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Fix timezone bug in MidnightTimer; increase logging
This commit is contained in:
@@ -227,7 +227,7 @@ abstract class DateUtils {
|
|||||||
fun getStartOfTodayWithOffset(): Long = getStartOfDayWithOffset(getLocalTime())
|
fun getStartOfTodayWithOffset(): Long = getStartOfDayWithOffset(getLocalTime())
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun millisecondsUntilTomorrowWithOffset(): Long = getStartOfTomorrowWithOffset() - getLocalTime()
|
fun millisecondsUntilTomorrowWithOffset(): Long = getStartOfTomorrowWithOffset() - applyTimezone(getLocalTime())
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun getStartOfTodayCalendar(): GregorianCalendar = getCalendar(getStartOfToday())
|
fun getStartOfTodayCalendar(): GregorianCalendar = getCalendar(getStartOfToday())
|
||||||
|
|||||||
@@ -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()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import kotlinx.coroutines.asCoroutineDispatcher
|
|||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import org.isoron.uhabits.core.BaseUnitTest
|
import org.isoron.uhabits.core.BaseUnitTest
|
||||||
|
import org.isoron.uhabits.core.io.StandardLogging
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import java.util.Calendar
|
import java.util.Calendar
|
||||||
import java.util.TimeZone
|
import java.util.TimeZone
|
||||||
@@ -34,7 +35,7 @@ class MidnightTimerTest : BaseUnitTest() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
val suspendedListener = suspendCoroutine<Boolean> { continuation ->
|
val suspendedListener = suspendCoroutine<Boolean> { continuation ->
|
||||||
MidnightTimer().apply {
|
MidnightTimer(StandardLogging()).apply {
|
||||||
addListener { continuation.resume(true) }
|
addListener { continuation.resume(true) }
|
||||||
// When
|
// When
|
||||||
onResume(1, executor)
|
onResume(1, executor)
|
||||||
|
|||||||
Reference in New Issue
Block a user