mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
This commit is contained in:
parent
e2c8f85a5b
commit
a59bf64677
1 changed files with 40 additions and 20 deletions
|
|
@ -4,45 +4,65 @@ import android.net.Uri
|
|||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
* shows where file is being used on Commons and other wikis.
|
||||
* Data model for displaying file usage information in the UI, including the title and link to the page.
|
||||
*/
|
||||
data class FileUsagesUiModel(
|
||||
val title: String,
|
||||
val link: String?
|
||||
)
|
||||
|
||||
/**
|
||||
* Converts a FileUsage object to a UI model for Commons file usages.
|
||||
* Creates a link to the file's page on Commons.
|
||||
*/
|
||||
fun FileUsage.toUiModel(): FileUsagesUiModel {
|
||||
// Replace spaces with underscores and URL-encode the title for the link
|
||||
val encodedTitle = Uri.encode(title.replace(" ", "_"))
|
||||
return FileUsagesUiModel(
|
||||
title = title,
|
||||
link = "https://commons.wikimedia.org/wiki/${Uri.encode(title.replace(" ", "_"))}"
|
||||
link = "https://commons.wikimedia.org/wiki/$encodedTitle"
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a GlobalFileUsage object to a UI model for file usages on other wikis.
|
||||
* Generates a link to the page and prefixes the title with the wiki code (e.g., "(en) Title").
|
||||
*/
|
||||
fun GlobalFileUsage.toUiModel(): FileUsagesUiModel {
|
||||
Timber.d("GlobalFileUsage: wiki=%s, title=%s", wiki, title)
|
||||
// Log input values for debugging
|
||||
Timber.d("Converting GlobalFileUsage: wiki=$wiki, title=$title")
|
||||
|
||||
// handles the empty or invalid wiki/title
|
||||
if (wiki.isEmpty() || title.isEmpty()) {
|
||||
Timber.w("Invalid GlobalFileUsage: wiki=%s, title=%s", wiki, title)
|
||||
// Check for invalid or empty inputs
|
||||
if (wiki.isBlank() || title.isBlank()) {
|
||||
Timber.w("Invalid input: wiki=$wiki, title=$title")
|
||||
return FileUsagesUiModel(title = title, link = null)
|
||||
}
|
||||
|
||||
// determines the domain
|
||||
val domain = when {
|
||||
wiki.contains(".") -> wiki // Already a full domain like "en.wikipedia.org"
|
||||
wiki == "commonswiki" -> "commons.wikimedia.org"
|
||||
wiki.endsWith("wiki") -> {
|
||||
val code = wiki.removeSuffix("wiki")
|
||||
"$code.wikipedia.org"
|
||||
}
|
||||
else -> "$wiki.wikipedia.org" // fallback for codes like "en"
|
||||
// Extract wiki code for prefix (e.g., "en" from "en.wikipedia.org" or "enwiki")
|
||||
val wikiCode = when {
|
||||
wiki.contains(".") -> wiki.substringBefore(".") // e.g., "en" from "en.wikipedia.org"
|
||||
wiki == "commonswiki" -> "commons"
|
||||
wiki.endsWith("wiki") -> wiki.removeSuffix("wiki")
|
||||
else -> wiki
|
||||
}
|
||||
|
||||
val normalizedTitle = Uri.encode(title.replace(" ", "_"))
|
||||
// Create prefixed title, e.g., "(en) Changi East Depot"
|
||||
val prefixedTitle = "($wikiCode) $title"
|
||||
|
||||
// construct full URL
|
||||
val url = "https://$domain/wiki/$normalizedTitle"
|
||||
Timber.d("Generated URL for GlobalFileUsage: %s", url)
|
||||
// Determine the domain for the URL
|
||||
val domain = when {
|
||||
wiki.contains(".") -> wiki // Already a full domain, e.g., "en.wikipedia.org"
|
||||
wiki == "commonswiki" -> "commons.wikimedia.org"
|
||||
wiki.endsWith("wiki") -> wiki.removeSuffix("wiki") + ".wikipedia.org"
|
||||
else -> "$wiki.wikipedia.org" // Fallback for simple codes like "en"
|
||||
}
|
||||
|
||||
return FileUsagesUiModel(title = title, link = url)
|
||||
// Normalize title: replace spaces with underscores and URL-encode
|
||||
val encodedTitle = Uri.encode(title.replace(" ", "_"))
|
||||
|
||||
// Build the full URL
|
||||
val url = "https://$domain/wiki/$encodedTitle"
|
||||
Timber.d("Generated URL: $url")
|
||||
|
||||
return FileUsagesUiModel(title = prefixedTitle, link = url)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue