This repository was archived by the owner on May 23, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathAttachmentHelper.kt
More file actions
36 lines (31 loc) · 1.33 KB
/
AttachmentHelper.kt
File metadata and controls
36 lines (31 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@file:JvmName("AttachmentHelper")
package com.keylesspalace.tusky.util
import android.content.Context
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.entity.Attachment
import kotlin.math.roundToInt
fun Attachment.getFormattedDescription(context: Context): CharSequence {
var duration = ""
if (meta?.duration != null && meta.duration > 0) {
duration = formatDuration(meta.duration.toDouble()) + " "
}
return if (description.isNullOrEmpty()) {
duration + context.getString(R.string.description_post_media_no_description_placeholder)
} else {
duration + description
}
}
private fun formatDuration(durationInSeconds: Double): String {
val seconds = durationInSeconds.roundToInt() % 60
val minutes = durationInSeconds.toInt() % 3600 / 60
val hours = durationInSeconds.toInt() / 3600
return "%d:%02d:%02d".format(hours, minutes, seconds)
}
fun List<Attachment>.aspectRatios(minAspect: Double, maxAspect: Double): List<Double> {
return map { attachment ->
// clamp ratio between min & max, defaulting to 16:9 if there is no metadata
val size = (attachment.meta?.small ?: attachment.meta?.original) ?: return@map 1.7778
val aspect = if (size.aspect > 0) size.aspect else size.width.toDouble() / size.height
aspect.coerceIn(minAspect, maxAspect)
}
}