Skip to content

Commit 015c5d5

Browse files
authored
Convert wikidata/mwapi to kotlin (part 3) (#6004)
* Convert Edit to kotlin along with deleting unused class * Converted ExtMetadata to kotlin * Convert ImageInfo to kotlin * Removed unused class * Convert Notification to kotlin * Convert PageProperties to kotlin * Convert PageTitle to kotlin * Convert Namespace to kotlin
1 parent 64354fb commit 015c5d5

File tree

22 files changed

+832
-1133
lines changed

22 files changed

+832
-1133
lines changed

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import fr.free.nrw.commons.wikidata.model.Entities
1111
import fr.free.nrw.commons.wikidata.model.gallery.ExtMetadata
1212
import fr.free.nrw.commons.wikidata.model.gallery.ImageInfo
1313
import fr.free.nrw.commons.wikidata.mwapi.MwQueryPage
14-
import org.apache.commons.lang3.StringUtils
1514
import java.text.ParseException
1615
import java.util.Date
1716
import javax.inject.Inject
@@ -24,24 +23,24 @@ class MediaConverter
2423
entity: Entities.Entity,
2524
imageInfo: ImageInfo,
2625
): Media {
27-
val metadata = imageInfo.metadata
26+
val metadata = imageInfo.getMetadata()
2827
requireNotNull(metadata) { "No metadata" }
2928
// Stores mapping of title attribute to hidden attribute of each category
3029
val myMap = mutableMapOf<String, Boolean>()
3130
page.categories()?.forEach { myMap[it.title()] = (it.hidden()) }
3231

3332
return Media(
3433
page.pageId().toString(),
35-
imageInfo.thumbUrl.takeIf { it.isNotBlank() } ?: imageInfo.originalUrl,
36-
imageInfo.originalUrl,
34+
imageInfo.getThumbUrl().takeIf { it.isNotBlank() } ?: imageInfo.getOriginalUrl(),
35+
imageInfo.getOriginalUrl(),
3736
page.title(),
3837
metadata.imageDescription(),
3938
safeParseDate(metadata.dateTime()),
4039
metadata.licenseShortName(),
4140
metadata.prefixedLicenseUrl,
4241
getAuthor(metadata),
4342
getAuthor(metadata),
44-
MediaDataExtractorUtil.extractCategoriesFromList(metadata.categories),
43+
MediaDataExtractorUtil.extractCategoriesFromList(metadata.categories()),
4544
metadata.latLng,
4645
entity.labels().mapValues { it.value.value() },
4746
entity.descriptions().mapValues { it.value.value() },
@@ -104,9 +103,5 @@ private val ExtMetadata.prefixedLicenseUrl: String
104103
}
105104

106105
private val ExtMetadata.latLng: LatLng?
107-
get() =
108-
if (!StringUtils.isBlank(gpsLatitude) && !StringUtils.isBlank(gpsLongitude)) {
109-
LatLng(gpsLatitude.toDouble(), gpsLongitude.toDouble(), 0.0f)
110-
} else {
111-
null
112-
}
106+
get() = LatLng.latLongOrNull(gpsLatitude(), gpsLongitude())
107+

app/src/main/java/fr/free/nrw/commons/location/LatLng.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ data class LatLng(
4141
* Accepts a non-null [Location] and converts it to a [LatLng].
4242
*/
4343
companion object {
44+
fun latLongOrNull(latitude: String?, longitude: String?): LatLng? =
45+
if (!latitude.isNullOrBlank() && !longitude.isNullOrBlank()) {
46+
LatLng(latitude.toDouble(), longitude.toDouble(), 0.0f)
47+
} else {
48+
null
49+
}
50+
4451
/**
4552
* gets the latitude and longitude of a given non-null location
4653
* @param location the non-null location of the user

app/src/main/java/fr/free/nrw/commons/notification/NotificationClient.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ class NotificationClient
6464
return Notification(
6565
notificationType = notificationType,
6666
notificationText = notificationText,
67-
date = DateUtil.getMonthOnlyDateString(timestamp),
68-
link = contents?.links?.primary?.url ?: "",
67+
date = DateUtil.getMonthOnlyDateString(getTimestamp()),
68+
link = contents?.links?.getPrimary()?.url ?: "",
6969
iconUrl = "",
7070
notificationId = id().toString(),
7171
)

app/src/main/java/fr/free/nrw/commons/wikidata/model/edit/Edit.java

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package fr.free.nrw.commons.wikidata.model.edit
2+
3+
import fr.free.nrw.commons.wikidata.mwapi.MwPostResponse
4+
5+
class Edit : MwPostResponse() {
6+
private val edit: Result? = null
7+
8+
fun edit(): Result? = edit
9+
10+
class Result {
11+
private val result: String? = null
12+
private val code: String? = null
13+
private val info: String? = null
14+
private val warning: String? = null
15+
16+
fun editSucceeded(): Boolean =
17+
"Success" == result
18+
19+
fun code(): String? = code
20+
21+
fun info(): String? = info
22+
23+
fun warning(): String? = warning
24+
}
25+
}

app/src/main/java/fr/free/nrw/commons/wikidata/model/edit/EditResult.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

app/src/main/java/fr/free/nrw/commons/wikidata/model/gallery/ExtMetadata.java

Lines changed: 0 additions & 102 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package fr.free.nrw.commons.wikidata.model.gallery
2+
3+
import com.google.gson.annotations.SerializedName
4+
import org.apache.commons.lang3.StringUtils
5+
6+
class ExtMetadata {
7+
@SerializedName("DateTime") private val dateTime: Values? = null
8+
@SerializedName("ObjectName") private val objectName: Values? = null
9+
@SerializedName("CommonsMetadataExtension") private val commonsMetadataExtension: Values? = null
10+
@SerializedName("Categories") private val categories: Values? = null
11+
@SerializedName("Assessments") private val assessments: Values? = null
12+
@SerializedName("GPSLatitude") private val gpsLatitude: Values? = null
13+
@SerializedName("GPSLongitude") private val gpsLongitude: Values? = null
14+
@SerializedName("ImageDescription") private val imageDescription: Values? = null
15+
@SerializedName("DateTimeOriginal") private val dateTimeOriginal: Values? = null
16+
@SerializedName("Artist") private val artist: Values? = null
17+
@SerializedName("Credit") private val credit: Values? = null
18+
@SerializedName("Permission") private val permission: Values? = null
19+
@SerializedName("AuthorCount") private val authorCount: Values? = null
20+
@SerializedName("LicenseShortName") private val licenseShortName: Values? = null
21+
@SerializedName("UsageTerms") private val usageTerms: Values? = null
22+
@SerializedName("LicenseUrl") private val licenseUrl: Values? = null
23+
@SerializedName("AttributionRequired") private val attributionRequired: Values? = null
24+
@SerializedName("Copyrighted") private val copyrighted: Values? = null
25+
@SerializedName("Restrictions") private val restrictions: Values? = null
26+
@SerializedName("License") private val license: Values? = null
27+
28+
fun licenseShortName(): String = licenseShortName?.value ?: ""
29+
30+
fun licenseUrl(): String = licenseUrl?.value ?: ""
31+
32+
fun license(): String = license?.value ?: ""
33+
34+
fun imageDescription(): String = imageDescription?.value ?: ""
35+
36+
fun imageDescriptionSource(): String = imageDescription?.source ?: ""
37+
38+
fun objectName(): String = objectName?.value ?: ""
39+
40+
fun usageTerms(): String = usageTerms?.value ?: ""
41+
42+
fun dateTimeOriginal(): String = dateTimeOriginal?.value ?: ""
43+
44+
fun dateTime(): String = dateTime?.value ?: ""
45+
46+
fun artist(): String = artist?.value ?: ""
47+
48+
fun categories(): String = categories?.value ?: ""
49+
50+
fun gpsLatitude(): String = gpsLatitude?.value ?: ""
51+
52+
fun gpsLongitude(): String = gpsLongitude?.value ?: ""
53+
54+
fun credit(): String = credit?.value ?: ""
55+
56+
class Values {
57+
val value: String? = null
58+
val source: String? = null
59+
val hidden: String? = null
60+
}
61+
}

0 commit comments

Comments
 (0)