mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-07 01:28:52 -06:00
Merge pull request #1108 from sgallese/feature/midnight-timer-remove-jvm
Place MidnightTimer under test
This commit is contained in:
@@ -36,10 +36,15 @@ abstract class DateUtils {
|
||||
private var startDayHourOffset: Int = 0
|
||||
private var startDayMinuteOffset: Int = 0
|
||||
|
||||
/**
|
||||
* Number of milliseconds in one second.
|
||||
*/
|
||||
const val SECOND_LENGTH: Long = 1000
|
||||
|
||||
/**
|
||||
* Number of milliseconds in one minute.
|
||||
*/
|
||||
const val MINUTE_LENGTH: Long = 60 * 1000
|
||||
const val MINUTE_LENGTH: Long = 60 * SECOND_LENGTH
|
||||
|
||||
/**
|
||||
* Number of milliseconds in one hour.
|
||||
|
||||
@@ -33,31 +33,39 @@ open class MidnightTimer @Inject constructor() {
|
||||
private val listeners: MutableList<MidnightListener> = LinkedList()
|
||||
private lateinit var executor: ScheduledExecutorService
|
||||
|
||||
@Synchronized fun addListener(listener: MidnightListener) {
|
||||
@Synchronized
|
||||
fun addListener(listener: MidnightListener) {
|
||||
this.listeners.add(listener)
|
||||
}
|
||||
|
||||
@Synchronized fun onPause(): MutableList<Runnable>? = executor.shutdownNow()
|
||||
@Synchronized
|
||||
fun onPause(): MutableList<Runnable>? = executor.shutdownNow()
|
||||
|
||||
@Synchronized fun onResume() {
|
||||
executor = Executors.newSingleThreadScheduledExecutor()
|
||||
@Synchronized
|
||||
fun onResume(
|
||||
delayOffsetInMillis: Long = DateUtils.SECOND_LENGTH,
|
||||
testExecutor: ScheduledExecutorService? = null
|
||||
) {
|
||||
executor = testExecutor ?: Executors.newSingleThreadScheduledExecutor()
|
||||
executor.scheduleAtFixedRate(
|
||||
{ notifyListeners() },
|
||||
DateUtils.millisecondsUntilTomorrowWithOffset() + 1000,
|
||||
DateUtils.millisecondsUntilTomorrowWithOffset() + delayOffsetInMillis,
|
||||
DateUtils.DAY_LENGTH,
|
||||
TimeUnit.MILLISECONDS
|
||||
)
|
||||
}
|
||||
|
||||
@Synchronized fun removeListener(listener: MidnightListener) = this.listeners.remove(listener)
|
||||
@Synchronized
|
||||
fun removeListener(listener: MidnightListener) = this.listeners.remove(listener)
|
||||
|
||||
@Synchronized private fun notifyListeners() {
|
||||
@Synchronized
|
||||
private fun notifyListeners() {
|
||||
for (l in listeners) {
|
||||
l.atMidnight()
|
||||
}
|
||||
}
|
||||
|
||||
interface MidnightListener {
|
||||
fun interface MidnightListener {
|
||||
fun atMidnight()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user