Skip to content

Commit

Permalink
refactor: drop usages of java.util.Calendar
Browse files Browse the repository at this point in the history
In favor of timezone agnostic `kotlin.time.Duration`. It simplifies implementation and removes a need to declare a timezone, which might be confusing.
  • Loading branch information
wzieba committed Nov 13, 2023
1 parent 47d2303 commit 5cbea9f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.parsely.parselyandroid

import java.util.Calendar
import java.util.TimeZone
import kotlin.time.Duration
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -29,20 +28,18 @@ internal class EngagementManager(
) {
private var job: Job? = null
private var totalTime: Long = 0
private var startTime: Calendar
private var startTime: Duration
private var nextScheduledExecution: Long = 0

init {
startTime = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
startTime = clock.now
}

val isRunning: Boolean
get() = job?.isActive ?: false

fun start() {
startTime = Calendar.getInstance(TimeZone.getTimeZone("UTC")).apply {
timeInMillis = clock.now.inWholeMilliseconds
}
startTime = clock.now
job = coroutineScope.launch {
while (isActive) {
latestDelayMillis = intervalCalculator.calculate(startTime)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.parsely.parselyandroid

import java.util.Calendar
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

internal open class HeartbeatIntervalCalculator(private val clock: Clock) {

open fun calculate(startTime: Calendar): Long {
val startTimeDuration = startTime.time.time.milliseconds
open fun calculate(startTime: Duration): Long {
val nowDuration = clock.now

val totalTrackedTime = nowDuration - startTimeDuration
val totalTrackedTime = nowDuration - startTime
val totalWithOffset = totalTrackedTime + OFFSET_MATCHING_BASE_INTERVAL
val newInterval = totalWithOffset * BACKOFF_PROPORTION
val clampedNewInterval = minOf(MAX_TIME_BETWEEN_HEARTBEATS, newInterval)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.parsely.parselyandroid

import androidx.test.core.app.ApplicationProvider
import java.util.Calendar
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
Expand Down Expand Up @@ -205,7 +204,7 @@ internal class EngagementManagerTest {
}

class FakeIntervalCalculator : HeartbeatIntervalCalculator(Clock()) {
override fun calculate(startTime: Calendar): Long {
override fun calculate(startTime: Duration): Long {
return DEFAULT_INTERVAL.inWholeMilliseconds
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ internal class HeartbeatIntervalCalculatorTest {
fun `given the same time of start and current time, when calculating interval, return offset times backoff proportion`() {
// given
fakeClock.fakeNow = Duration.ZERO
val startTime = Calendar.getInstance().apply {
timeInMillis = 0
}
val startTime = Duration.ZERO

// when
val result = sut.calculate(startTime)
Expand All @@ -45,9 +43,7 @@ internal class HeartbeatIntervalCalculatorTest {
// (15 minutes / 0.3) - 35 seconds = 2965 seconds. Add 1 second to be over the limit
val excessiveTime = 2965.seconds + 1.seconds
fakeClock.fakeNow = excessiveTime
val startTime = Calendar.getInstance().apply {
timeInMillis = 0
}
val startTime = Duration.ZERO

// when
val result = sut.calculate(startTime)
Expand All @@ -59,9 +55,7 @@ internal class HeartbeatIntervalCalculatorTest {
@Test
fun `given a specific time point, when updating latest interval, it correctly calculates the interval`() {
// given
val startTime = Calendar.getInstance().apply {
timeInMillis = 0
}
val startTime = Duration.ZERO
fakeClock.fakeNow = 2.seconds

// when
Expand Down

0 comments on commit 5cbea9f

Please sign in to comment.