-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
70 additions
and
17 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
pillarbox-demo-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/ui/DurationFormatter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (c) SRG SSR. All rights reserved. | ||
* License information is available from the LICENSE file. | ||
*/ | ||
package ch.srgssr.pillarbox.demo.shared.ui | ||
|
||
import kotlin.time.Duration | ||
|
||
/** | ||
* @return a formatter function for displaying the duration based on its length. | ||
*/ | ||
fun Duration.getFormatter(): (Duration) -> String { | ||
return if (inWholeHours <= 0) DurationFormatter::formatMinutesSeconds else DurationFormatter::formatHourMinutesSeconds | ||
} | ||
|
||
/** | ||
* Duration formatter | ||
*/ | ||
object DurationFormatter { | ||
private const val FORMAT_HOURS_MINUTES_SECONDS = "%02d:%02d:%02d" | ||
private const val FORMAT_MINUTES_SECONDS = "%02d:%02d" | ||
|
||
/** | ||
* @param duration The duration to format. | ||
* @return Format hour minutes seconds | ||
*/ | ||
fun formatHourMinutesSeconds(duration: Duration): String { | ||
return duration.toComponents { hours, minutes, seconds, _ -> | ||
FORMAT_HOURS_MINUTES_SECONDS.format(hours, minutes, seconds) | ||
} | ||
} | ||
|
||
/** | ||
* @param duration The duration to format. | ||
* @return Format minutes seconds | ||
*/ | ||
fun formatMinutesSeconds(duration: Duration): String { | ||
return duration.toComponents { _, minutes, seconds, _ -> | ||
FORMAT_MINUTES_SECONDS.format(minutes, seconds) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters