Show tooltips instead of Toasts when long-pressing attachment images (#4382)

- Use `TooltipCompat.setTooltipText()` instead of setting an
`OnLongClickListener` showing a Toast, to show the attachment
description. This method will display native tooltips on API 26+, and
set an `OnLongClickListener` on older versions to display a special
Toast anchored to the view. In both cases this provides a better user
experience.
- Simplify `Attachment.getFormattedDescription()` by using Kotlin's
`Duration`. Since it's an inline class, no extra memory will be
allocated on the heap. Also, ensure that the calculation of minutes and
hours use the rounded number of seconds instead of the non-rounded one.
This commit is contained in:
Christophe Beyls 2024-05-03 13:21:02 +02:00 committed by GitHub
commit 76c6ec5510
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 21 additions and 31 deletions

View file

@ -6,24 +6,22 @@ import android.content.Context
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.entity.Attachment
import kotlin.math.roundToInt
import kotlin.time.Duration.Companion.seconds
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)
val durationInSeconds = meta?.duration ?: 0f
val duration = if (durationInSeconds > 0f) {
durationInSeconds.roundToInt().seconds.toComponents { hours, minutes, seconds, _ ->
"%d:%02d:%02d ".format(hours, minutes, seconds)
}
} else {
duration + description
""
}
return duration + if (description.isNullOrEmpty()) {
context.getString(R.string.description_post_media_no_description_placeholder)
} else {
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(): List<Double> {