Skip to content

Commit 3777f18

Browse files
authored
Convert mwapi/wikidata to kotlin (part 1) (commons-app#5991)
* Convert OkHttpJsonApiClient and CategoryApi to kotlin * Convert GsonUtil to kotlin * Convert WikidataConstants to kotlin * Convert WikidataEditListener to kotlin * Convert WikidataEditService to kotlin * work in progress * Convert RequiredFieldsCheckOnReadTypeAdapterFactory to kotlin * Converted type adapters * Convert WikiSiteTypeAdapter to kotlin * Fixed nullability
1 parent 9dd504e commit 3777f18

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1491
-1747
lines changed

app/src/main/java/fr/free/nrw/commons/campaigns/CampaignsPresenter.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ class CampaignsPresenter @Inject constructor(
5252
return
5353
}
5454

55-
okHttpJsonApiClient.campaigns
55+
okHttpJsonApiClient.getCampaigns()
5656
.observeOn(mainThreadScheduler)
5757
.subscribeOn(ioScheduler)
5858
.doOnSubscribe { disposable = it }
5959
.subscribe({ campaignResponseDTO ->
60-
val campaigns = campaignResponseDTO.campaigns?.toMutableList()
60+
val campaigns = campaignResponseDTO?.campaigns?.toMutableList()
6161
if (campaigns.isNullOrEmpty()) {
6262
Timber.e("The campaigns list is empty")
6363
view!!.showCampaigns(null)

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,13 @@ class NetworkingModule {
170170
@Named(NAMED_WIKI_DATA_WIKI_SITE)
171171
fun provideWikidataWikiSite(): WikiSite = WikiSite(BuildConfig.WIKIDATA_URL)
172172

173-
174173
/**
175174
* Gson objects are very heavy. The app should ideally be using just one instance of it instead of creating new instances everywhere.
176175
* @return returns a singleton Gson instance
177176
*/
178177
@Provides
179178
@Singleton
180-
fun provideGson(): Gson = GsonUtil.getDefaultGson()
179+
fun provideGson(): Gson = GsonUtil.defaultGson
181180

182181
@Provides
183182
@Singleton

app/src/main/java/fr/free/nrw/commons/mwapi/CategoryApi.java

-99
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package fr.free.nrw.commons.mwapi
2+
3+
import com.google.gson.Gson
4+
import fr.free.nrw.commons.BuildConfig
5+
import fr.free.nrw.commons.category.CATEGORY_PREFIX
6+
import fr.free.nrw.commons.category.CategoryItem
7+
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse
8+
import io.reactivex.Single
9+
import okhttp3.HttpUrl
10+
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
11+
import okhttp3.OkHttpClient
12+
import okhttp3.Request
13+
import timber.log.Timber
14+
import javax.inject.Inject
15+
16+
/**
17+
* Uses the OkHttp library to implement calls to the Commons MediaWiki API to match GPS coordinates
18+
* with nearby Commons categories. Parses the results using GSON to obtain a list of relevant
19+
* categories. Note: that caller is responsible for executing the request() method on a background
20+
* thread.
21+
*/
22+
class CategoryApi @Inject constructor(
23+
private val okHttpClient: OkHttpClient,
24+
private val gson: Gson
25+
) {
26+
private val apiUrl : HttpUrl by lazy { BuildConfig.WIKIMEDIA_API_HOST.toHttpUrlOrNull()!! }
27+
28+
fun request(coords: String): Single<List<CategoryItem>> = Single.fromCallable {
29+
val apiUrl = buildUrl(coords)
30+
Timber.d("URL: %s", apiUrl.toString())
31+
32+
val request: Request = Request.Builder().get().url(apiUrl).build()
33+
val response = okHttpClient.newCall(request).execute()
34+
val body = response.body ?: return@fromCallable emptyList<CategoryItem>()
35+
36+
val apiResponse = gson.fromJson(body.charStream(), MwQueryResponse::class.java)
37+
val categories: MutableSet<CategoryItem> = mutableSetOf()
38+
if (apiResponse?.query() != null && apiResponse.query()!!.pages() != null) {
39+
for (page in apiResponse.query()!!.pages()!!) {
40+
if (page.categories() != null) {
41+
for (category in page.categories()!!) {
42+
categories.add(
43+
CategoryItem(
44+
name = category.title().replace(CATEGORY_PREFIX, ""),
45+
description = "",
46+
thumbnail = "",
47+
isSelected = false
48+
)
49+
)
50+
}
51+
}
52+
}
53+
}
54+
ArrayList<CategoryItem>(categories)
55+
}
56+
57+
/**
58+
* Builds URL with image coords for MediaWiki API calls
59+
* Example URL: https://commons.wikimedia.org/w/api.php?action=query&prop=categories|coordinates|pageprops&format=json&clshow=!hidden&coprop=type|name|dim|country|region|globe&codistancefrompoint=38.11386944444445|13.356263888888888&generator=geosearch&redirects=&ggscoord=38.11386944444445|1.356263888888888&ggsradius=100&ggslimit=10&ggsnamespace=6&ggsprop=type|name|dim|country|region|globe&ggsprimary=all&formatversion=2
60+
*
61+
* @param coords Coordinates to build query with
62+
* @return URL for API query
63+
*/
64+
private fun buildUrl(coords: String): HttpUrl = apiUrl.newBuilder()
65+
.addQueryParameter("action", "query")
66+
.addQueryParameter("prop", "categories|coordinates|pageprops")
67+
.addQueryParameter("format", "json")
68+
.addQueryParameter("clshow", "!hidden")
69+
.addQueryParameter("coprop", "type|name|dim|country|region|globe")
70+
.addQueryParameter("codistancefrompoint", coords)
71+
.addQueryParameter("generator", "geosearch")
72+
.addQueryParameter("ggscoord", coords)
73+
.addQueryParameter("ggsradius", "10000")
74+
.addQueryParameter("ggslimit", "10")
75+
.addQueryParameter("ggsnamespace", "6")
76+
.addQueryParameter("ggsprop", "type|name|dim|country|region|globe")
77+
.addQueryParameter("ggsprimary", "all")
78+
.addQueryParameter("formatversion", "2")
79+
.build()
80+
}
81+
82+
83+

0 commit comments

Comments
 (0)