From e5661098d9f9b293580cb41840520e134b36bdb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Tue, 29 Sep 2020 11:42:54 +0200 Subject: [PATCH 1/2] Fix string pair list type converter (#970) --- app/build.gradle | 2 +- .../io/github/wulkanowy/data/db/Converters.kt | 11 +-- .../data/db/adapters/PairAdapterFactory.kt | 68 +++++++++++++++++++ .../wulkanowy/data/db/ConvertersTest.kt | 25 +++++++ 4 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/io/github/wulkanowy/data/db/adapters/PairAdapterFactory.kt create mode 100644 app/src/test/java/io/github/wulkanowy/data/db/ConvertersTest.kt diff --git a/app/build.gradle b/app/build.gradle index 0dd315277..440b665a0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -127,7 +127,7 @@ configurations.all { } dependencies { - implementation "io.github.wulkanowy:sdk:0.21.0" + implementation "io.github.wulkanowy:sdk:84c0703" coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10' 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 834a9636a..def0b3715 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 @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.db import androidx.room.TypeConverter import com.squareup.moshi.Moshi import com.squareup.moshi.Types +import io.github.wulkanowy.data.db.adapters.PairAdapterFactory import java.time.Instant import java.time.LocalDate import java.time.LocalDateTime @@ -12,14 +13,14 @@ import java.util.Date class Converters { - private val moshi by lazy { Moshi.Builder().build() } + private val moshi by lazy { Moshi.Builder().add(PairAdapterFactory).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)) + private val stringListPairAdapter by lazy { + moshi.adapter>>(Types.newParameterizedType(List::class.java, Pair::class.java, String::class.java, String::class.java)) } @TypeConverter @@ -60,11 +61,11 @@ class Converters { @TypeConverter fun stringPairListToJson(list: List>): String { - return stringMapAdapter.toJson(list.toMap()) + return stringListPairAdapter.toJson(list) } @TypeConverter fun jsonToStringPairList(value: String): List> { - return stringMapAdapter.fromJson(value).orEmpty().toList() + return stringListPairAdapter.fromJson(value).orEmpty() } } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/adapters/PairAdapterFactory.kt b/app/src/main/java/io/github/wulkanowy/data/db/adapters/PairAdapterFactory.kt new file mode 100644 index 000000000..60ae37c9d --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/data/db/adapters/PairAdapterFactory.kt @@ -0,0 +1,68 @@ +package io.github.wulkanowy.data.db.adapters + +import com.squareup.moshi.JsonAdapter +import com.squareup.moshi.JsonReader +import com.squareup.moshi.JsonWriter +import com.squareup.moshi.Moshi +import com.squareup.moshi.Types +import java.lang.reflect.ParameterizedType +import java.lang.reflect.Type + +object PairAdapterFactory : JsonAdapter.Factory { + + override fun create(type: Type, annotations: MutableSet, moshi: Moshi): JsonAdapter<*>? { + if (type !is ParameterizedType || List::class.java != type.rawType) return null + if (type.actualTypeArguments[0] != Pair::class.java) return null + + val listType = Types.newParameterizedType(List::class.java, Map::class.java, String::class.java) + val listAdapter = moshi.adapter>>(listType) + + val mapType = Types.newParameterizedType(MutableMap::class.java, String::class.java, String::class.java) + val mapAdapter = moshi.adapter>(mapType) + + return PairAdapter(listAdapter, mapAdapter) + } + + private class PairAdapter( + private val listAdapter: JsonAdapter>>, + private val mapAdapter: JsonAdapter>, + ) : JsonAdapter>>() { + + override fun toJson(writer: JsonWriter, value: List>?) { + writer.beginArray() + value?.forEach { + writer.beginObject() + writer.name("first").value(it.first) + writer.name("second").value(it.second) + writer.endObject() + } + writer.endArray() + } + + override fun fromJson(reader: JsonReader): List>? { + return if (reader.peek() == JsonReader.Token.BEGIN_OBJECT) deserializeMoshiMap(reader) + else deserializeGsonPair(reader) + } + + // for compatibility with 0.21.0 + private fun deserializeMoshiMap(reader: JsonReader): List>? { + val map = mapAdapter.fromJson(reader) ?: return null + + return map.entries.map { + it.key to it.value + } + } + + private fun deserializeGsonPair(reader: JsonReader): List>? { + val list = listAdapter.fromJson(reader) ?: return null + + require(list.size == 2 || list.isEmpty()) { + "pair with more or less than two elements: $list" + } + + return list.map { + it["first"].orEmpty() to it["second"].orEmpty() + } + } + } +} diff --git a/app/src/test/java/io/github/wulkanowy/data/db/ConvertersTest.kt b/app/src/test/java/io/github/wulkanowy/data/db/ConvertersTest.kt new file mode 100644 index 000000000..50741b7ab --- /dev/null +++ b/app/src/test/java/io/github/wulkanowy/data/db/ConvertersTest.kt @@ -0,0 +1,25 @@ +package io.github.wulkanowy.data.db + +import org.junit.Assert.assertEquals +import org.junit.Test + +class ConvertersTest { + + @Test + fun stringPairListToJson() { + assertEquals(Converters().stringPairListToJson(listOf("aaa" to "bbb", "ccc" to "ddd")), "[{\"first\":\"aaa\",\"second\":\"bbb\"},{\"first\":\"ccc\",\"second\":\"ddd\"}]") + assertEquals(Converters().stringPairListToJson(listOf()), "[]") + } + + @Test + fun jsonToStringPairList() { + assertEquals(Converters().jsonToStringPairList("[{\"first\":\"aaa\",\"second\":\"bbb\"},{\"first\":\"ccc\",\"second\":\"ddd\"}]"), listOf("aaa" to "bbb", "ccc" to "ddd")) + assertEquals(Converters().jsonToStringPairList("[]"), listOf>()) + } + + @Test + fun jsonToStringPairList_0210() { + assertEquals(Converters().jsonToStringPairList("{\"aaa\":\"bbb\",\"ccc\":\"ddd\"}"), listOf("aaa" to "bbb", "ccc" to "ddd")) + assertEquals(Converters().jsonToStringPairList("{}"), listOf>()) + } +} From d95a33787b39a1215efdf9f6a91cba9edb23df30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Tue, 29 Sep 2020 11:43:49 +0200 Subject: [PATCH 2/2] Version 0.21.1 --- .travis.yml | 2 +- app/build.gradle | 6 +++--- app/src/main/play/release-notes/pl-PL/default.txt | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index c2361d66c..a36c34cfe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ cache: branches: only: - develop - - 0.21.0 + - 0.21.1 android: licenses: diff --git a/app/build.gradle b/app/build.gradle index 440b665a0..cee711d20 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -18,8 +18,8 @@ android { testApplicationId "io.github.tests.wulkanowy" minSdkVersion 17 targetSdkVersion 29 - versionCode 70 - versionName "0.21.0" + versionCode 71 + versionName "0.21.1" multiDexEnabled true resValue "string", "app_name", "Wulkanowy" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -127,7 +127,7 @@ configurations.all { } dependencies { - implementation "io.github.wulkanowy:sdk:84c0703" + implementation "io.github.wulkanowy:sdk:0.21.1" coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10' diff --git a/app/src/main/play/release-notes/pl-PL/default.txt b/app/src/main/play/release-notes/pl-PL/default.txt index 09f44931d..271eac7fd 100644 --- a/app/src/main/play/release-notes/pl-PL/default.txt +++ b/app/src/main/play/release-notes/pl-PL/default.txt @@ -1,4 +1,4 @@ -Wersja 0.21.0 +Wersja 0.21.1 - naprawiliśmy logowanie do tarnowskiego dziennika - naprawiliśmy wyświetlanie podsumowania punktów klasy - dodaliśmy skróty aplikacji