Skip to content

Commit e2c8f85

Browse files
Make "File usages" items clickable with correct URLs #6307 (#6405)
* Fix: URL generation for GlobalFileUsage in FileUsagesUiModel.kt for issue #6307 * Add clickable functionality to 'Usages on Other Wikis' in FileUsagesContainer for issue #6307 --------- Co-authored-by: Nicolas Raoul <[email protected]>
1 parent dd96c64 commit e2c8f85

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed
Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,48 @@
11
package fr.free.nrw.commons.fileusages
22

3+
import android.net.Uri
4+
import timber.log.Timber
5+
36
/**
4-
* Show where file is being used on Commons and oher wikis.
7+
* shows where file is being used on Commons and other wikis.
58
*/
69
data class FileUsagesUiModel(
710
val title: String,
811
val link: String?
912
)
1013

1114
fun FileUsage.toUiModel(): FileUsagesUiModel {
12-
return FileUsagesUiModel(title = title, link = "https://commons.wikimedia.org/wiki/$title")
15+
return FileUsagesUiModel(
16+
title = title,
17+
link = "https://commons.wikimedia.org/wiki/${Uri.encode(title.replace(" ", "_"))}"
18+
)
1319
}
1420

1521
fun GlobalFileUsage.toUiModel(): FileUsagesUiModel {
16-
// link is associated with sub items under wiki group (which is not used ATM)
17-
return FileUsagesUiModel(title = wiki, link = null)
18-
}
22+
Timber.d("GlobalFileUsage: wiki=%s, title=%s", wiki, title)
23+
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)
27+
return FileUsagesUiModel(title = title, link = null)
28+
}
29+
30+
// determines the domain
31+
val domain = when {
32+
wiki.contains(".") -> wiki // Already a full domain like "en.wikipedia.org"
33+
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"
39+
}
40+
41+
val normalizedTitle = Uri.encode(title.replace(" ", "_"))
42+
43+
// construct full URL
44+
val url = "https://$domain/wiki/$normalizedTitle"
45+
Timber.d("Generated URL for GlobalFileUsage: %s", url)
46+
47+
return FileUsagesUiModel(title = title, link = url)
48+
}

app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.kt

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,22 +2128,17 @@ fun FileUsagesContainer(
21282128
val uriHandle = LocalUriHandler.current
21292129

21302130
Column(modifier = modifier) {
2131-
21322131
Row(
21332132
modifier = Modifier.fillMaxWidth(),
21342133
verticalAlignment = Alignment.CenterVertically,
21352134
horizontalArrangement = Arrangement.SpaceBetween
21362135
) {
2137-
21382136
Text(
21392137
text = stringResource(R.string.usages_on_commons_heading),
21402138
textAlign = TextAlign.Center,
21412139
style = MaterialTheme.typography.titleSmall
21422140
)
2143-
2144-
IconButton(onClick = {
2145-
isCommonsListExpanded = !isCommonsListExpanded
2146-
}) {
2141+
IconButton(onClick = { isCommonsListExpanded = !isCommonsListExpanded }) {
21472142
Icon(
21482143
imageVector = if (isCommonsListExpanded) Icons.Default.KeyboardArrowUp
21492144
else Icons.Default.KeyboardArrowDown,
@@ -2157,11 +2152,8 @@ fun FileUsagesContainer(
21572152
MediaDetailViewModel.FileUsagesContainerState.Loading -> {
21582153
LinearProgressIndicator()
21592154
}
2160-
21612155
is MediaDetailViewModel.FileUsagesContainerState.Success -> {
2162-
21632156
val data = commonsContainerState.data
2164-
21652157
if (data.isNullOrEmpty()) {
21662158
ListItem(headlineContent = {
21672159
Text(
@@ -2181,19 +2173,19 @@ fun FileUsagesContainer(
21812173
headlineContent = {
21822174
Text(
21832175
modifier = Modifier.clickable {
2184-
uriHandle.openUri(usage.link!!)
2176+
usage.link?.let { uriHandle.openUri(it) }
21852177
},
21862178
text = usage.title,
21872179
style = MaterialTheme.typography.titleSmall.copy(
21882180
color = Color(0xFF5A6AEC),
21892181
textDecoration = TextDecoration.Underline
21902182
)
21912183
)
2192-
})
2184+
}
2185+
)
21932186
}
21942187
}
21952188
}
2196-
21972189
is MediaDetailViewModel.FileUsagesContainerState.Error -> {
21982190
ListItem(headlineContent = {
21992191
Text(
@@ -2203,12 +2195,10 @@ fun FileUsagesContainer(
22032195
)
22042196
})
22052197
}
2206-
22072198
MediaDetailViewModel.FileUsagesContainerState.Initial -> {}
22082199
}
22092200
}
22102201

2211-
22122202
Row(
22132203
modifier = Modifier.fillMaxWidth(),
22142204
verticalAlignment = Alignment.CenterVertically,
@@ -2219,10 +2209,7 @@ fun FileUsagesContainer(
22192209
textAlign = TextAlign.Center,
22202210
style = MaterialTheme.typography.titleSmall
22212211
)
2222-
2223-
IconButton(onClick = {
2224-
isOtherWikisListExpanded = !isOtherWikisListExpanded
2225-
}) {
2212+
IconButton(onClick = { isOtherWikisListExpanded = !isOtherWikisListExpanded }) {
22262213
Icon(
22272214
imageVector = if (isOtherWikisListExpanded) Icons.Default.KeyboardArrowUp
22282215
else Icons.Default.KeyboardArrowDown,
@@ -2236,11 +2223,8 @@ fun FileUsagesContainer(
22362223
MediaDetailViewModel.FileUsagesContainerState.Loading -> {
22372224
LinearProgressIndicator()
22382225
}
2239-
22402226
is MediaDetailViewModel.FileUsagesContainerState.Success -> {
2241-
22422227
val data = globalContainerState.data
2243-
22442228
if (data.isNullOrEmpty()) {
22452229
ListItem(headlineContent = {
22462230
Text(
@@ -2259,16 +2243,20 @@ fun FileUsagesContainer(
22592243
},
22602244
headlineContent = {
22612245
Text(
2246+
modifier = Modifier.clickable {
2247+
usage.link?.let { uriHandle.openUri(it) }
2248+
},
22622249
text = usage.title,
22632250
style = MaterialTheme.typography.titleSmall.copy(
2251+
color = Color(0xFF5A6AEC),
22642252
textDecoration = TextDecoration.Underline
22652253
)
22662254
)
2267-
})
2255+
}
2256+
)
22682257
}
22692258
}
22702259
}
2271-
22722260
is MediaDetailViewModel.FileUsagesContainerState.Error -> {
22732261
ListItem(headlineContent = {
22742262
Text(
@@ -2278,10 +2266,8 @@ fun FileUsagesContainer(
22782266
)
22792267
})
22802268
}
2281-
22822269
MediaDetailViewModel.FileUsagesContainerState.Initial -> {}
22832270
}
22842271
}
2285-
22862272
}
2287-
}
2273+
}

0 commit comments

Comments
 (0)