Skip to content

Commit ae52267

Browse files
pshnicolas-raoul
andauthored
Convert wikidata/mwapi to kotlin (part 2) (commons-app#5999)
* Convert DepictSearchResponse to kotlin * Convert Entities to kotlin * Convert WikiSite to kotlin --------- Co-authored-by: Nicolas Raoul <nicolas.raoul@gmail.com>
1 parent f8d519e commit ae52267

File tree

8 files changed

+348
-427
lines changed

8 files changed

+348
-427
lines changed

app/src/androidTest/java/fr/free/nrw/commons/AboutActivityTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class AboutActivityTest {
105105
fun testLaunchTranslate() {
106106
Espresso.onView(ViewMatchers.withId(R.id.about_translate)).perform(ViewActions.click())
107107
Espresso.onView(ViewMatchers.withId(android.R.id.button1)).perform(ViewActions.click())
108-
val langCode = CommonsApplication.instance.languageLookUpTable!!.codes[0]
108+
val langCode = CommonsApplication.instance.languageLookUpTable!!.getCodes()[0]
109109
Intents.intended(
110110
CoreMatchers.allOf(
111111
IntentMatchers.hasAction(Intent.ACTION_VIEW),

app/src/main/java/fr/free/nrw/commons/di/NetworkingModule.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import okhttp3.logging.HttpLoggingInterceptor
4444
import okhttp3.logging.HttpLoggingInterceptor.Level
4545
import timber.log.Timber
4646
import java.io.File
47-
import java.util.Locale
4847
import java.util.concurrent.TimeUnit
4948
import javax.inject.Named
5049
import javax.inject.Singleton
@@ -293,9 +292,8 @@ class NetworkingModule {
293292
@Provides
294293
@Singleton
295294
@Named(NAMED_LANGUAGE_WIKI_PEDIA_WIKI_SITE)
296-
fun provideLanguageWikipediaSite(): WikiSite {
297-
return WikiSite.forLanguageCode(Locale.getDefault().language)
298-
}
295+
fun provideLanguageWikipediaSite(): WikiSite =
296+
WikiSite.forDefaultLocaleLanguageCode()
299297

300298
companion object {
301299
private const val WIKIDATA_SPARQL_QUERY_URL = "https://query.wikidata.org/sparql"

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

-24
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package fr.free.nrw.commons.wikidata.model
2+
3+
/**
4+
* Model class for API response obtained from search for depictions
5+
*/
6+
class DepictSearchResponse(
7+
/**
8+
* @return List<DepictSearchItem> for the DepictSearchResponse
9+
</DepictSearchItem>
10+
*/
11+
val search: List<DepictSearchItem>
12+
)

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

-106
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package fr.free.nrw.commons.wikidata.model
2+
3+
import com.google.gson.annotations.SerializedName
4+
import fr.free.nrw.commons.wikidata.mwapi.MwResponse
5+
import org.apache.commons.lang3.StringUtils
6+
7+
class Entities : MwResponse() {
8+
private val entities: Map<String, Entity>? = null
9+
val success: Int = 0
10+
11+
fun entities(): Map<String, Entity> = entities ?: emptyMap()
12+
13+
private val first : Entity?
14+
get() = entities?.values?.iterator()?.next()
15+
16+
override fun postProcess() {
17+
first?.let {
18+
if (it.isMissing()) throw RuntimeException("The requested entity was not found.")
19+
}
20+
}
21+
22+
class Entity {
23+
private val type: String? = null
24+
private val id: String? = null
25+
private val labels: Map<String, Label>? = null
26+
private val descriptions: Map<String, Label>? = null
27+
private val sitelinks: Map<String, SiteLink>? = null
28+
29+
@SerializedName(value = "statements", alternate = ["claims"])
30+
val statements: Map<String, List<StatementPartial>>? = null
31+
private val missing: String? = null
32+
33+
fun id(): String =
34+
StringUtils.defaultString(id)
35+
36+
fun labels(): Map<String, Label> =
37+
labels ?: emptyMap()
38+
39+
fun descriptions(): Map<String, Label> =
40+
descriptions ?: emptyMap()
41+
42+
fun sitelinks(): Map<String, SiteLink> =
43+
sitelinks ?: emptyMap()
44+
45+
fun isMissing(): Boolean =
46+
"-1" == id && missing != null
47+
}
48+
49+
class Label(private val language: String?, private val value: String?) {
50+
fun language(): String =
51+
StringUtils.defaultString(language)
52+
53+
fun value(): String =
54+
StringUtils.defaultString(value)
55+
}
56+
57+
class SiteLink {
58+
val site: String? = null
59+
get() = StringUtils.defaultString(field)
60+
61+
private val title: String? = null
62+
get() = StringUtils.defaultString(field)
63+
}
64+
}

0 commit comments

Comments
 (0)