refactor(android): add shared formatDuration + ms/sec conversion helpers
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package com.fabledsword.minstrel.shared
|
||||
|
||||
const val MILLIS_PER_SECOND = 1000
|
||||
|
||||
private const val SECONDS_PER_MINUTE = 60
|
||||
|
||||
/**
|
||||
* Formats [seconds] as m:ss. Returns [zero] when [seconds] <= 0 — pass
|
||||
* "--:--" for unknown track durations, "0:00" (default) for playback
|
||||
* position/duration. The single mm:ss formatter for the client.
|
||||
*/
|
||||
fun formatDuration(seconds: Int, zero: String = "0:00"): String =
|
||||
if (seconds <= 0) {
|
||||
zero
|
||||
} else {
|
||||
"%d:%02d".format(seconds / SECONDS_PER_MINUTE, seconds % SECONDS_PER_MINUTE)
|
||||
}
|
||||
|
||||
fun Int.secondsToMillis(): Int = this * MILLIS_PER_SECOND
|
||||
|
||||
fun Int.millisToSeconds(): Int = this / MILLIS_PER_SECOND
|
||||
|
||||
fun Long.millisToSeconds(): Int = (this / MILLIS_PER_SECOND).toInt()
|
||||
Reference in New Issue
Block a user