Skip to content

Commit

Permalink
round delay/relativeTime to nearest minute instead of flooring, fix f…
Browse files Browse the repository at this point in the history
…ormatDelay tests
  • Loading branch information
ialokim authored and grote committed Oct 14, 2020
1 parent 77b501a commit dc489d8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 28 deletions.
24 changes: 18 additions & 6 deletions app/src/main/java/de/grobox/transportr/utils/DateUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ import java.util.*
import kotlin.math.abs

object DateUtils {
fun millisToMinutes(millis: Long): Long {
val seconds = millis / 1000
return seconds / 60 + when {
seconds % 60 >= 30 -> 1
seconds % 60 <= -30 -> -1
else -> 0
}
}

fun formatDate(context: Context, date: Date): String {
val df = DateFormat.getDateFormat(context)
return df.format(date)
Expand All @@ -48,7 +57,7 @@ object DateUtils {

fun formatDuration(duration: Long): String {
// get duration in minutes
val durationMinutes = duration / 1000 / 60
val durationMinutes = millisToMinutes(duration)
val m = durationMinutes % 60
val h = durationMinutes / 60
return "$h:${m.toString().padStart(2, '0')}"
Expand All @@ -58,10 +67,13 @@ object DateUtils {
return formatDuration(end.time - start.time)
}

fun formatDelay(context: Context, delay: Long) = Delay(
delay = "${if (delay >= 0) '+' else ""}${delay / 1000 / 60}",
color = ContextCompat.getColor(context, if (delay <= 0) R.color.md_green_500 else R.color.md_red_500)
)
fun formatDelay(context: Context, delay: Long): Delay {
val delayMinutes = millisToMinutes(delay)
return Delay(
delay = "${if (delayMinutes >= 0) '+' else ""}$delayMinutes",
color = ContextCompat.getColor(context, if (delayMinutes > 0) R.color.md_red_500 else R.color.md_green_500)
)
}

data class Delay(
val delay: String,
Expand Down Expand Up @@ -101,7 +113,7 @@ object DateUtils {
* Returns difference in minutes
*/
private fun getDifferenceInMinutes(d1: Date, d2: Date): Long {
return (d2.time - d1.time) / 1000 / 60
return millisToMinutes(d2.time - d1.time)
}

private fun getDifferenceInMinutes(date: Date): Long {
Expand Down
93 changes: 71 additions & 22 deletions app/src/test/java/de/grobox/transportr/utils/DateUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package de.grobox.transportr.utils

import android.content.Context
import android.content.res.Resources
import android.view.View
import de.grobox.transportr.R
import org.junit.Assert
Expand All @@ -35,16 +36,27 @@ class DateUtilsTest {

@Mock
lateinit var context: Context
@Mock
lateinit var resources: Resources

private fun minuteToMillis(minutes: Float): Long {
return (minutes * android.text.format.DateUtils.MINUTE_IN_MILLIS).toLong()
}

@Test
fun millisToMinutes() {
Assert.assertEquals(0, DateUtils.millisToMinutes(minuteToMillis(0f)))
Assert.assertEquals(0, DateUtils.millisToMinutes(minuteToMillis(0.3f)))
Assert.assertEquals(0, DateUtils.millisToMinutes(minuteToMillis(-0.3f)))
Assert.assertEquals(1, DateUtils.millisToMinutes(minuteToMillis(0.8f)))
Assert.assertEquals(-1, DateUtils.millisToMinutes(minuteToMillis(-0.8f)))
}

@Test
fun formatDuration() {
Assert.assertEquals("0:05", DateUtils.formatDuration(minuteToMillis(5f)))
Assert.assertEquals("0:05", DateUtils.formatDuration(minuteToMillis(5.3f)))
//Assert.assertEquals("0:06", DateUtils.formatDuration(minuteToMillis(5.8f))) //todo: round to nearest minute
Assert.assertEquals("0:06", DateUtils.formatDuration(minuteToMillis(5.8f)))
Assert.assertEquals("0:15", DateUtils.formatDuration(minuteToMillis(15f)))
Assert.assertEquals("1:05", DateUtils.formatDuration(minuteToMillis(65f)))
Assert.assertEquals("1:05", DateUtils.formatDuration(minuteToMillis(65.3f)))
Expand All @@ -53,33 +65,72 @@ class DateUtilsTest {
Assert.assertEquals("1:05", DateUtils.formatDuration(Date(), Date().apply { time += minuteToMillis(65f) }))
}

@Test
fun formatDelay() {
Assert.assertEquals("+0", DateUtils.formatDelay(minuteToMillis(0f)))
Assert.assertEquals("+0", DateUtils.formatDelay(minuteToMillis(0.3f)))
//Assert.assertEquals("+0", DateUtils.formatDelay(minuteToMillis(-0.3f))) //todo: round to nearest minute
//Assert.assertEquals("+1", DateUtils.formatDelay(minuteToMillis(0.8f))) //todo: round to nearest minute
//Assert.assertEquals("-1", DateUtils.formatDelay(minuteToMillis(-0.8f))) //todo: round to nearest minute
Assert.assertEquals("+1", DateUtils.formatDelay(minuteToMillis(1f)))
Assert.assertEquals("-1", DateUtils.formatDelay(minuteToMillis(-1f)))
//Assert.assertEquals("+10", DateUtils.formatDelay(minuteToMillis(9.8f))) //todo: round to nearest minute
//Assert.assertEquals("-10", DateUtils.formatDelay(minuteToMillis(-9.8f))) //todo: round to nearest minute
Assert.assertEquals("+100", DateUtils.formatDelay(minuteToMillis(100f)))
Assert.assertEquals("-100", DateUtils.formatDelay(minuteToMillis(-100f)))
}

private val GREEN = 0
private val RED = 1
private fun getNow() = "now"
private fun getIn(difference: Any) = "in $difference"
private fun getAgo(difference: Any) = "$difference ago"

@Before
fun initMocks() {
MockitoAnnotations.initMocks(this)
`when`(context.resources).thenReturn(resources)
`when`(resources.getColor(R.color.md_green_500)).thenReturn(GREEN)
`when`(resources.getColor(R.color.md_red_500)).thenReturn(RED)

`when`(context.getString(R.string.now_small)).thenReturn(getNow())
`when`(context.getString(eq(R.string.in_x_minutes), anyLong())).thenAnswer { i -> getIn(i.arguments[1]) }
`when`(context.getString(eq(R.string.x_minutes_ago), anyLong())).thenAnswer { i -> getAgo(i.arguments[1]) }
}

@Test
fun formatDelay() {
Assert.assertEquals(
DateUtils.Delay("+0", GREEN),
DateUtils.formatDelay(context, minuteToMillis(0f))
)
Assert.assertEquals(
DateUtils.Delay("+0", GREEN),
DateUtils.formatDelay(context, minuteToMillis(0.3f))
)
Assert.assertEquals(
DateUtils.Delay("+0", GREEN),
DateUtils.formatDelay(context, minuteToMillis(-0.3f))
)
Assert.assertEquals(
DateUtils.Delay("+1", RED),
DateUtils.formatDelay(context, minuteToMillis(0.8f))
)
Assert.assertEquals(
DateUtils.Delay("-1", GREEN),
DateUtils.formatDelay(context, minuteToMillis(-0.8f))
)
Assert.assertEquals(
DateUtils.Delay("+1", RED),
DateUtils.formatDelay(context, minuteToMillis(1f))
)
Assert.assertEquals(
DateUtils.Delay("-1", GREEN),
DateUtils.formatDelay(context, minuteToMillis(-1f))
)
Assert.assertEquals(
DateUtils.Delay("+10", RED),
DateUtils.formatDelay(context, minuteToMillis(9.8f))
)
Assert.assertEquals(
DateUtils.Delay("-10", GREEN),
DateUtils.formatDelay(context, minuteToMillis(-9.8f))
)
Assert.assertEquals(
DateUtils.Delay("+100", RED),
DateUtils.formatDelay(context, minuteToMillis(100f))
)
Assert.assertEquals(
DateUtils.Delay("-100", GREEN),
DateUtils.formatDelay(context, minuteToMillis(-100f))
)
}

@Test
fun formatRelativeTime() {
Assert.assertEquals(
Expand All @@ -94,16 +145,14 @@ class DateUtilsTest {
DateUtils.RelativeTime(getNow(), View.VISIBLE),
DateUtils.formatRelativeTime(context, Date().apply { time -= minuteToMillis(0.4f) })
)
//todo: round to nearest minute?
/*Assert.assertEquals(
Assert.assertEquals(
DateUtils.RelativeTime(getIn(1), View.VISIBLE),
DateUtils.formatRelativeTime(context, Date().apply { time += minuteToMillis(0.8f) })
)*/
//todo: round to nearest minute?
/*Assert.assertEquals(
)
Assert.assertEquals(
DateUtils.RelativeTime(getAgo(1), View.VISIBLE),
DateUtils.formatRelativeTime(context, Date().apply { time -= minuteToMillis(0.8f) })
)*/
)
Assert.assertEquals(
DateUtils.RelativeTime(getIn(5), View.VISIBLE),
DateUtils.formatRelativeTime(context, Date().apply { time += minuteToMillis(5f) })
Expand Down

0 comments on commit dc489d8

Please sign in to comment.