mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-11-02 15:53:55 +01:00
Convert GsonUtil to kotlin
This commit is contained in:
parent
79de03964e
commit
a783a82058
7 changed files with 42 additions and 59 deletions
|
|
@ -170,14 +170,13 @@ class NetworkingModule {
|
||||||
@Named(NAMED_WIKI_DATA_WIKI_SITE)
|
@Named(NAMED_WIKI_DATA_WIKI_SITE)
|
||||||
fun provideWikidataWikiSite(): WikiSite = WikiSite(BuildConfig.WIKIDATA_URL)
|
fun provideWikidataWikiSite(): WikiSite = WikiSite(BuildConfig.WIKIDATA_URL)
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gson objects are very heavy. The app should ideally be using just one instance of it instead of creating new instances everywhere.
|
* Gson objects are very heavy. The app should ideally be using just one instance of it instead of creating new instances everywhere.
|
||||||
* @return returns a singleton Gson instance
|
* @return returns a singleton Gson instance
|
||||||
*/
|
*/
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideGson(): Gson = GsonUtil.getDefaultGson()
|
fun provideGson(): Gson = GsonUtil.defaultGson
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,10 @@ class CommonsServiceFactory(
|
||||||
) {
|
) {
|
||||||
val builder: Retrofit.Builder by lazy {
|
val builder: Retrofit.Builder by lazy {
|
||||||
// All instances of retrofit share this configuration, but create it lazily
|
// All instances of retrofit share this configuration, but create it lazily
|
||||||
Retrofit
|
Retrofit.Builder()
|
||||||
.Builder()
|
|
||||||
.client(okHttpClient)
|
.client(okHttpClient)
|
||||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||||
.addConverterFactory(GsonConverterFactory.create(GsonUtil.getDefaultGson()))
|
.addConverterFactory(GsonConverterFactory.create(GsonUtil.defaultGson))
|
||||||
}
|
}
|
||||||
|
|
||||||
val retrofitCache: MutableMap<String, Retrofit> = mutableMapOf()
|
val retrofitCache: MutableMap<String, Retrofit> = mutableMapOf()
|
||||||
|
|
|
||||||
|
|
@ -1,34 +0,0 @@
|
||||||
package fr.free.nrw.commons.wikidata;
|
|
||||||
|
|
||||||
import android.net.Uri;
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.GsonBuilder;
|
|
||||||
import fr.free.nrw.commons.wikidata.json.RequiredFieldsCheckOnReadTypeAdapterFactory;
|
|
||||||
import fr.free.nrw.commons.wikidata.model.DataValue;
|
|
||||||
import fr.free.nrw.commons.wikidata.model.WikiSite;
|
|
||||||
import fr.free.nrw.commons.wikidata.json.NamespaceTypeAdapter;
|
|
||||||
import fr.free.nrw.commons.wikidata.json.PostProcessingTypeAdapter;
|
|
||||||
import fr.free.nrw.commons.wikidata.json.UriTypeAdapter;
|
|
||||||
import fr.free.nrw.commons.wikidata.json.WikiSiteTypeAdapter;
|
|
||||||
import fr.free.nrw.commons.wikidata.model.page.Namespace;
|
|
||||||
|
|
||||||
public final class GsonUtil {
|
|
||||||
private static final String DATE_FORMAT = "MMM dd, yyyy HH:mm:ss";
|
|
||||||
|
|
||||||
private static final GsonBuilder DEFAULT_GSON_BUILDER = new GsonBuilder()
|
|
||||||
.setDateFormat(DATE_FORMAT)
|
|
||||||
.registerTypeAdapterFactory(DataValue.getPolymorphicTypeAdapter())
|
|
||||||
.registerTypeHierarchyAdapter(Uri.class, new UriTypeAdapter().nullSafe())
|
|
||||||
.registerTypeHierarchyAdapter(Namespace.class, new NamespaceTypeAdapter().nullSafe())
|
|
||||||
.registerTypeAdapter(WikiSite.class, new WikiSiteTypeAdapter().nullSafe())
|
|
||||||
.registerTypeAdapterFactory(new RequiredFieldsCheckOnReadTypeAdapterFactory())
|
|
||||||
.registerTypeAdapterFactory(new PostProcessingTypeAdapter());
|
|
||||||
|
|
||||||
private static final Gson DEFAULT_GSON = DEFAULT_GSON_BUILDER.create();
|
|
||||||
|
|
||||||
public static Gson getDefaultGson() {
|
|
||||||
return DEFAULT_GSON;
|
|
||||||
}
|
|
||||||
|
|
||||||
private GsonUtil() { }
|
|
||||||
}
|
|
||||||
29
app/src/main/java/fr/free/nrw/commons/wikidata/GsonUtil.kt
Normal file
29
app/src/main/java/fr/free/nrw/commons/wikidata/GsonUtil.kt
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
package fr.free.nrw.commons.wikidata
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.GsonBuilder
|
||||||
|
import fr.free.nrw.commons.wikidata.json.NamespaceTypeAdapter
|
||||||
|
import fr.free.nrw.commons.wikidata.json.PostProcessingTypeAdapter
|
||||||
|
import fr.free.nrw.commons.wikidata.json.RequiredFieldsCheckOnReadTypeAdapterFactory
|
||||||
|
import fr.free.nrw.commons.wikidata.json.UriTypeAdapter
|
||||||
|
import fr.free.nrw.commons.wikidata.json.WikiSiteTypeAdapter
|
||||||
|
import fr.free.nrw.commons.wikidata.model.DataValue.Companion.polymorphicTypeAdapter
|
||||||
|
import fr.free.nrw.commons.wikidata.model.WikiSite
|
||||||
|
import fr.free.nrw.commons.wikidata.model.page.Namespace
|
||||||
|
|
||||||
|
object GsonUtil {
|
||||||
|
private const val DATE_FORMAT = "MMM dd, yyyy HH:mm:ss"
|
||||||
|
|
||||||
|
private val DEFAULT_GSON_BUILDER: GsonBuilder by lazy {
|
||||||
|
GsonBuilder().setDateFormat(DATE_FORMAT)
|
||||||
|
.registerTypeAdapterFactory(polymorphicTypeAdapter)
|
||||||
|
.registerTypeHierarchyAdapter(Uri::class.java, UriTypeAdapter().nullSafe())
|
||||||
|
.registerTypeHierarchyAdapter(Namespace::class.java, NamespaceTypeAdapter().nullSafe())
|
||||||
|
.registerTypeAdapter(WikiSite::class.java, WikiSiteTypeAdapter().nullSafe())
|
||||||
|
.registerTypeAdapterFactory(RequiredFieldsCheckOnReadTypeAdapterFactory())
|
||||||
|
.registerTypeAdapterFactory(PostProcessingTypeAdapter())
|
||||||
|
}
|
||||||
|
|
||||||
|
val defaultGson: Gson by lazy { DEFAULT_GSON_BUILDER.create() }
|
||||||
|
}
|
||||||
|
|
@ -148,7 +148,7 @@ public class Notification {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (primaryLink == null && primary instanceof JsonObject) {
|
if (primaryLink == null && primary instanceof JsonObject) {
|
||||||
primaryLink = GsonUtil.getDefaultGson().fromJson(primary, Link.class);
|
primaryLink = GsonUtil.INSTANCE.getDefaultGson().fromJson(primary, Link.class);
|
||||||
}
|
}
|
||||||
return primaryLink;
|
return primaryLink;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,7 @@ public abstract class MockWebServerTest {
|
||||||
.baseUrl(url)
|
.baseUrl(url)
|
||||||
.callbackExecutor(new ImmediateExecutor())
|
.callbackExecutor(new ImmediateExecutor())
|
||||||
.client(okHttpClient)
|
.client(okHttpClient)
|
||||||
.addConverterFactory(GsonConverterFactory.create(GsonUtil.getDefaultGson()))
|
.addConverterFactory(GsonConverterFactory.create(GsonUtil.INSTANCE.getDefaultGson()))
|
||||||
.build()
|
.build()
|
||||||
.create(clazz);
|
.create(clazz);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -120,26 +120,16 @@ class NotificationClientTest {
|
||||||
) = Notification().apply {
|
) = Notification().apply {
|
||||||
setId(notificationId)
|
setId(notificationId)
|
||||||
|
|
||||||
setTimestamp(
|
setTimestamp(Notification.Timestamp().apply { setUtciso8601(timestamp) })
|
||||||
Notification.Timestamp().apply {
|
|
||||||
setUtciso8601(timestamp)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
contents =
|
contents = Notification.Contents().apply {
|
||||||
Notification.Contents().apply {
|
setCompactHeader(compactHeader)
|
||||||
setCompactHeader(compactHeader)
|
|
||||||
|
|
||||||
links =
|
links = Notification.Links().apply {
|
||||||
Notification.Links().apply {
|
setPrimary(GsonUtil.defaultGson.toJsonTree(
|
||||||
setPrimary(
|
Notification.Link().apply { setUrl(primaryUrl) }
|
||||||
GsonUtil.getDefaultGson().toJsonTree(
|
))
|
||||||
Notification.Link().apply {
|
|
||||||
setUrl(primaryUrl)
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue