@@ -4,45 +4,65 @@ import android.net.Uri
44import timber.log.Timber
55
66/* *
7- * shows where file is being used on Commons and other wikis .
7+ * Data model for displaying file usage information in the UI, including the title and link to the page .
88 */
99data class FileUsagesUiModel (
1010 val title : String ,
1111 val link : String?
1212)
1313
14+ /* *
15+ * Converts a FileUsage object to a UI model for Commons file usages.
16+ * Creates a link to the file's page on Commons.
17+ */
1418fun FileUsage.toUiModel (): FileUsagesUiModel {
19+ // Replace spaces with underscores and URL-encode the title for the link
20+ val encodedTitle = Uri .encode(title.replace(" " , " _" ))
1521 return FileUsagesUiModel (
1622 title = title,
17- link = " https://commons.wikimedia.org/wiki/${ Uri .encode(title.replace( " " , " _ " ))} "
23+ link = " https://commons.wikimedia.org/wiki/$encodedTitle "
1824 )
1925}
2026
27+ /* *
28+ * Converts a GlobalFileUsage object to a UI model for file usages on other wikis.
29+ * Generates a link to the page and prefixes the title with the wiki code (e.g., "(en) Title").
30+ */
2131fun GlobalFileUsage.toUiModel (): FileUsagesUiModel {
22- Timber .d(" GlobalFileUsage: wiki=%s, title=%s" , wiki, title)
32+ // Log input values for debugging
33+ Timber .d(" Converting GlobalFileUsage: wiki=$wiki , title=$title " )
2334
24- // handles the empty or invalid wiki/title
25- if (wiki.isEmpty () || title.isEmpty ()) {
26- Timber .w(" Invalid GlobalFileUsage : wiki=%s , title=%s " , wiki, title)
35+ // Check for invalid or empty inputs
36+ if (wiki.isBlank () || title.isBlank ()) {
37+ Timber .w(" Invalid input : wiki=$wiki , title=$ title" )
2738 return FileUsagesUiModel (title = title, link = null )
2839 }
2940
30- // determines the domain
41+ // Extract wiki code for prefix (e.g., "en" from "en.wikipedia.org" or "enwiki")
42+ val wikiCode = when {
43+ wiki.contains(" ." ) -> wiki.substringBefore(" ." ) // e.g., "en" from "en.wikipedia.org"
44+ wiki == " commonswiki" -> " commons"
45+ wiki.endsWith(" wiki" ) -> wiki.removeSuffix(" wiki" )
46+ else -> wiki
47+ }
48+
49+ // Create prefixed title, e.g., "(en) Changi East Depot"
50+ val prefixedTitle = " ($wikiCode ) $title "
51+
52+ // Determine the domain for the URL
3153 val domain = when {
32- wiki.contains(" ." ) -> wiki // Already a full domain like "en.wikipedia.org"
54+ wiki.contains(" ." ) -> wiki // Already a full domain, e.g., "en.wikipedia.org"
3355 wiki == " commonswiki" -> " commons.wikimedia.org"
34- wiki.endsWith(" wiki" ) -> {
35- val code = wiki.removeSuffix(" wiki" )
36- " $code .wikipedia.org"
37- }
38- else -> " $wiki .wikipedia.org" // fallback for codes like "en"
56+ wiki.endsWith(" wiki" ) -> wiki.removeSuffix(" wiki" ) + " .wikipedia.org"
57+ else -> " $wiki .wikipedia.org" // Fallback for simple codes like "en"
3958 }
4059
41- val normalizedTitle = Uri .encode(title.replace(" " , " _" ))
60+ // Normalize title: replace spaces with underscores and URL-encode
61+ val encodedTitle = Uri .encode(title.replace(" " , " _" ))
4262
43- // construct full URL
44- val url = " https://$domain /wiki/$normalizedTitle "
45- Timber .d(" Generated URL for GlobalFileUsage: %s " , url)
63+ // Build the full URL
64+ val url = " https://$domain /wiki/$encodedTitle "
65+ Timber .d(" Generated URL: $ url" )
4666
47- return FileUsagesUiModel (title = title , link = url)
67+ return FileUsagesUiModel (title = prefixedTitle , link = url)
4868}
0 commit comments