Improve credit line in image list (#6295)
Some checks are pending
Android CI / Run tests and generate APK (push) Waiting to run

- When author is not uploader, show both.
- When failing to parse author from HTML, use structured data.
This commit is contained in:
Yusuke Matsubara 2025-04-23 23:23:09 +09:00 committed by GitHub
parent 30762971db
commit 329a68216e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 363 additions and 81 deletions

View file

@ -0,0 +1,39 @@
package fr.free.nrw.commons.utils
import android.content.Context
import android.icu.text.ListFormatter
import android.os.Build
import fr.free.nrw.commons.Media
import fr.free.nrw.commons.R
import fr.free.nrw.commons.media.IdAndLabels
import java.util.Locale
object MediaAttributionUtil {
fun getTagLine(media: Media, context: Context): String {
val uploader = media.user
val author = media.getAttributedAuthor()
return if (author.isNullOrEmpty()) {
context.getString(R.string.image_uploaded_by, uploader)
} else if (author == uploader) {
context.getString(R.string.image_tag_line_created_and_uploaded_by, author)
} else {
context.getString(
R.string.image_tag_line_created_by_and_uploaded_by,
author,
uploader
)
}
}
fun getCreatorName(idAndLabels: List<IdAndLabels>): String? {
val locale = Locale.getDefault()
val names = idAndLabels.map{ x -> x.getLocalizedLabel(locale.language)}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val formatter = ListFormatter.getInstance(locale)
return formatter.format(names)
} else {
return names.joinToString(", ")
}
}
}

View file

@ -1,9 +1,8 @@
package fr.free.nrw.commons.utils
import android.os.Build
import android.text.Html
import android.text.Spanned
import android.text.SpannedString
import androidx.core.text.HtmlCompat
object StringUtil {
@ -26,12 +25,6 @@ object StringUtil {
.replace("&#8207;", "\u200F")
.replace("&amp;", "&")
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(processedSource, Html.FROM_HTML_MODE_LEGACY)
} else {
//noinspection deprecation
@Suppress("DEPRECATION")
Html.fromHtml(processedSource)
}
return HtmlCompat.fromHtml(processedSource, HtmlCompat.FROM_HTML_MODE_LEGACY)
}
}