mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 14:53:59 +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) | ||||
|     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. | ||||
|      * @return returns a singleton Gson instance | ||||
|      */ | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     fun provideGson(): Gson = GsonUtil.getDefaultGson() | ||||
|     fun provideGson(): Gson = GsonUtil.defaultGson | ||||
| 
 | ||||
|     @Provides | ||||
|     @Singleton | ||||
|  |  | |||
|  | @ -10,11 +10,10 @@ class CommonsServiceFactory( | |||
| ) { | ||||
|     val builder: Retrofit.Builder by lazy { | ||||
|         // All instances of retrofit share this configuration, but create it lazily | ||||
|         Retrofit | ||||
|             .Builder() | ||||
|         Retrofit.Builder() | ||||
|             .client(okHttpClient) | ||||
|             .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | ||||
|             .addConverterFactory(GsonConverterFactory.create(GsonUtil.getDefaultGson())) | ||||
|             .addConverterFactory(GsonConverterFactory.create(GsonUtil.defaultGson)) | ||||
|     } | ||||
| 
 | ||||
|     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; | ||||
|             } | ||||
|             if (primaryLink == null && primary instanceof JsonObject) { | ||||
|                 primaryLink = GsonUtil.getDefaultGson().fromJson(primary, Link.class); | ||||
|                 primaryLink = GsonUtil.INSTANCE.getDefaultGson().fromJson(primary, Link.class); | ||||
|             } | ||||
|             return primaryLink; | ||||
|         } | ||||
|  |  | |||
|  | @ -69,7 +69,7 @@ public abstract class MockWebServerTest { | |||
|                 .baseUrl(url) | ||||
|                 .callbackExecutor(new ImmediateExecutor()) | ||||
|                 .client(okHttpClient) | ||||
|                 .addConverterFactory(GsonConverterFactory.create(GsonUtil.getDefaultGson())) | ||||
|                 .addConverterFactory(GsonConverterFactory.create(GsonUtil.INSTANCE.getDefaultGson())) | ||||
|                 .build() | ||||
|                 .create(clazz); | ||||
|     } | ||||
|  |  | |||
|  | @ -120,26 +120,16 @@ class NotificationClientTest { | |||
|     ) = Notification().apply { | ||||
|         setId(notificationId) | ||||
| 
 | ||||
|         setTimestamp( | ||||
|             Notification.Timestamp().apply { | ||||
|                 setUtciso8601(timestamp) | ||||
|             }, | ||||
|         ) | ||||
|         setTimestamp(Notification.Timestamp().apply { setUtciso8601(timestamp) }) | ||||
| 
 | ||||
|         contents = | ||||
|             Notification.Contents().apply { | ||||
|                 setCompactHeader(compactHeader) | ||||
|         contents = Notification.Contents().apply { | ||||
|             setCompactHeader(compactHeader) | ||||
| 
 | ||||
|                 links = | ||||
|                     Notification.Links().apply { | ||||
|                         setPrimary( | ||||
|                             GsonUtil.getDefaultGson().toJsonTree( | ||||
|                                 Notification.Link().apply { | ||||
|                                     setUrl(primaryUrl) | ||||
|                                 }, | ||||
|                             ), | ||||
|                         ) | ||||
|                     } | ||||
|             links = Notification.Links().apply { | ||||
|                 setPrimary(GsonUtil.defaultGson.toJsonTree( | ||||
|                     Notification.Link().apply { setUrl(primaryUrl) } | ||||
|                 )) | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Paul Hawke
						Paul Hawke