From a0f9c70036da746e7a33d7d54ace56a1f711a8b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sun, 27 Sep 2020 20:59:27 +0200 Subject: [PATCH] Migrate from gson to moshi (#969) --- app/build.gradle | 7 +++-- .../io/github/wulkanowy/data/db/Converters.kt | 30 ++++++++++++------- .../wulkanowy/data/pojos/Contributor.kt | 8 ++++- .../appcreator/AppCreatorRepository.kt | 11 +++---- .../main/res/layout/fragment_contributor.xml | 10 ++++--- app/src/main/res/layout/item_contributor.xml | 3 +- 6 files changed, 46 insertions(+), 23 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 11a82fbd..c0a4e9f3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -119,6 +119,7 @@ ext { room = "2.2.5" chucker = "3.2.0" mockk = "1.10.0" + moshi = "1.9.3" } configurations.all { @@ -126,7 +127,7 @@ configurations.all { } dependencies { - implementation "io.github.wulkanowy:sdk:0.20.5" + implementation "io.github.wulkanowy:sdk:9364c2e" coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10' @@ -170,7 +171,9 @@ dependencies { implementation "com.ncapdevi:frag-nav:3.3.0" implementation "com.github.YarikSOffice:lingver:1.2.2" - implementation "com.google.code.gson:gson:2.8.6" + implementation "com.squareup.moshi:moshi:$moshi" + implementation "com.squareup.moshi:moshi-adapters:$moshi" + kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi" implementation "com.jakewharton.timber:timber:4.7.1" implementation "at.favre.lib:slf4j-timber:1.0.1" implementation "fr.bipi.treessence:treessence:0.3.2" diff --git a/app/src/main/java/io/github/wulkanowy/data/db/Converters.kt b/app/src/main/java/io/github/wulkanowy/data/db/Converters.kt index b21c4834..834a9636 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/Converters.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/Converters.kt @@ -1,8 +1,8 @@ package io.github.wulkanowy.data.db import androidx.room.TypeConverter -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken +import com.squareup.moshi.Moshi +import com.squareup.moshi.Types import java.time.Instant import java.time.LocalDate import java.time.LocalDateTime @@ -12,6 +12,16 @@ import java.util.Date class Converters { + private val moshi by lazy { Moshi.Builder().build() } + + private val integerListAdapter by lazy { + moshi.adapter>(Types.newParameterizedType(List::class.java, Integer::class.java)) + } + + private val stringMapAdapter by lazy { + moshi.adapter>(Types.newParameterizedType(MutableMap::class.java, String::class.java, String::class.java)) + } + @TypeConverter fun timestampToDate(value: Long?): LocalDate? = value?.run { Date(value).toInstant().atZone(ZoneOffset.UTC).toLocalDate() @@ -39,22 +49,22 @@ class Converters { fun intToMonth(value: Int?) = value?.let { Month.of(it) } @TypeConverter - fun intListToGson(list: List): String { - return Gson().toJson(list) + fun intListToJson(list: List): String { + return integerListAdapter.toJson(list) } @TypeConverter - fun gsonToIntList(value: String): List { - return Gson().fromJson(value, object : TypeToken>() {}.type) + fun jsonToIntList(value: String): List { + return integerListAdapter.fromJson(value).orEmpty() } @TypeConverter - fun stringPairListToGson(list: List>): String { - return Gson().toJson(list) + fun stringPairListToJson(list: List>): String { + return stringMapAdapter.toJson(list.toMap()) } @TypeConverter - fun gsonToStringPairList(value: String): List> { - return Gson().fromJson(value, object : TypeToken>>() {}.type) + fun jsonToStringPairList(value: String): List> { + return stringMapAdapter.fromJson(value).orEmpty().toList() } } diff --git a/app/src/main/java/io/github/wulkanowy/data/pojos/Contributor.kt b/app/src/main/java/io/github/wulkanowy/data/pojos/Contributor.kt index e792bde4..d2338c28 100644 --- a/app/src/main/java/io/github/wulkanowy/data/pojos/Contributor.kt +++ b/app/src/main/java/io/github/wulkanowy/data/pojos/Contributor.kt @@ -1,3 +1,9 @@ package io.github.wulkanowy.data.pojos -class Contributor(val displayName: String, val githubUsername: String) +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) +class Contributor( + val displayName: String, + val githubUsername: String +) diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/appcreator/AppCreatorRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/appcreator/AppCreatorRepository.kt index d1956557..ff538969 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/appcreator/AppCreatorRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/appcreator/AppCreatorRepository.kt @@ -1,7 +1,8 @@ package io.github.wulkanowy.data.repositories.appcreator import android.content.res.AssetManager -import com.google.gson.Gson +import com.squareup.moshi.Moshi +import com.squareup.moshi.Types import io.github.wulkanowy.data.pojos.Contributor import io.github.wulkanowy.utils.DispatchersProvider import kotlinx.coroutines.withContext @@ -15,9 +16,9 @@ class AppCreatorRepository @Inject constructor( ) { suspend fun getAppCreators() = withContext(dispatchers.backgroundThread) { - Gson().fromJson( - assets.open("contributors.json").bufferedReader().use { it.readText() }, - Array::class.java - ).toList() + val moshi = Moshi.Builder().build() + val type = Types.newParameterizedType(List::class.java, Contributor::class.java) + val adapter = moshi.adapter>(type) + adapter.fromJson(assets.open("contributors.json").bufferedReader().use { it.readText() }) } } diff --git a/app/src/main/res/layout/fragment_contributor.xml b/app/src/main/res/layout/fragment_contributor.xml index 399ab599..d913c5a4 100644 --- a/app/src/main/res/layout/fragment_contributor.xml +++ b/app/src/main/res/layout/fragment_contributor.xml @@ -1,5 +1,6 @@ @@ -20,17 +21,18 @@ android:id="@+id/creatorRecycler" android:layout_width="match_parent" android:layout_height="0dp" - android:layout_weight="1" /> + android:layout_weight="1" + tools:listitem="@layout/item_contributor" /> diff --git a/app/src/main/res/layout/item_contributor.xml b/app/src/main/res/layout/item_contributor.xml index 36eb64ec..9b126343 100644 --- a/app/src/main/res/layout/item_contributor.xml +++ b/app/src/main/res/layout/item_contributor.xml @@ -15,7 +15,8 @@ android:layout_marginStart="16dp" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" - android:contentDescription="@string/contributor_avatar_description" /> + android:contentDescription="@string/contributor_avatar_description" + tools:src="@tools:sample/avatars" />