Skip to content

Commit

Permalink
add approximation sign on record duration when seconds not shown
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Mar 31, 2024
1 parent fc31ad0 commit b1b1fb8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ class RecordViewDataMapper @Inject constructor(
useMilitaryTime = useMilitaryTime,
showSeconds = showSeconds,
),
duration = timeMapper.formatInterval(
interval = record.duration,
forceSeconds = showSeconds,
duration = timeMapper.formatIntervalAdjusted(
timeStarted = record.timeStarted,
timeEnded = record.timeEnded,
showSeconds = showSeconds,
useProportionalMinutes = useProportionalMinutes,
),
iconId = iconMapper.mapIcon(recordType.icon),
Expand Down Expand Up @@ -82,9 +83,10 @@ class RecordViewDataMapper @Inject constructor(
showSeconds = showSeconds,
),
timeEndedTimestamp = timeEnded,
duration = timeMapper.formatInterval(
interval = timeEnded - timeStarted,
forceSeconds = showSeconds,
duration = timeMapper.formatIntervalAdjusted(
timeStarted = timeStarted,
timeEnded = timeEnded,
showSeconds = showSeconds,
useProportionalMinutes = useProportionalMinutes,
),
iconId = RecordTypeIcon.Image(R.drawable.unknown),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,36 @@ class TimeMapper @Inject constructor(
return res
}

fun formatIntervalAdjusted(
timeStarted: Long,
timeEnded: Long,
showSeconds: Boolean,
useProportionalMinutes: Boolean,
): String {
val interval = formatInterval(
interval = timeEnded - timeStarted,
forceSeconds = showSeconds,
useProportionalMinutes = useProportionalMinutes,
)

return if (showSeconds || useProportionalMinutes) {
interval
} else {
val adjustedTimeStarted = timeStarted / MINUTES_IN_MILLIS * MINUTES_IN_MILLIS
val adjustedTimeEnded = timeEnded / MINUTES_IN_MILLIS * MINUTES_IN_MILLIS
val adjustedInterval = formatInterval(
interval = adjustedTimeEnded - adjustedTimeStarted,
forceSeconds = false,
useProportionalMinutes = false,
)
if (adjustedInterval != interval) {
"~$adjustedInterval"
} else {
interval
}
}
}

private fun formatIntervalProportional(hr: Long, min: Long): String {
val hourString = resourceRepo.getString(R.string.time_hour)
val minutesProportion = min / 60f
Expand Down Expand Up @@ -601,4 +631,8 @@ class TimeMapper @Inject constructor(
return calendar.get(Calendar.WEEK_OF_YEAR) == 1 &&
calendar.get(Calendar.MONTH) == calendar.getActualMaximum(Calendar.MONTH)
}

companion object {
private const val MINUTES_IN_MILLIS = 60_000
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ class ChangeRecordViewDataMapper @Inject constructor(
useMilitaryTime = useMilitaryTime,
showSeconds = showSeconds,
),
duration = timeMapper.formatInterval(
interval = record.duration,
forceSeconds = showSeconds,
duration = timeMapper.formatIntervalAdjusted(
timeStarted = record.timeStarted,
timeEnded = record.timeEnded,
showSeconds = showSeconds,
useProportionalMinutes = useProportionalMinutes,
),
iconId = recordType?.icon.orEmpty()
Expand Down

0 comments on commit b1b1fb8

Please sign in to comment.