[UI] Fix home cards order not saving.

This commit is contained in:
Kuba Szczodrzyński 2022-11-03 22:28:59 +01:00
parent d65c6db954
commit 05c7c0012c
No known key found for this signature in database
GPG Key ID: 70CB8A85BA1633CB
2 changed files with 11 additions and 2 deletions

View File

@ -115,9 +115,9 @@ class ConfigDelegate<T>(
is Boolean -> value is Boolean -> value
// enums, maps & collections // enums, maps & collections
is Enum<*> -> value.toInt() is Enum<*> -> value.toInt()
is Collection<*> -> JsonArray(value.map { is Collection<*> -> value.map {
if (it is Number || it is Boolean) it else serialize(it, serializeObjects = false) if (it is Number || it is Boolean) it else serialize(it, serializeObjects = false)
}) }.toJsonElement()
is Map<*, *> -> gson.toJson(value.mapValues { (_, it) -> is Map<*, *> -> gson.toJson(value.mapValues { (_, it) ->
if (it is Number || it is Boolean) it else serialize(it, serializeObjects = false) if (it is Number || it is Boolean) it else serialize(it, serializeObjects = false)
}) })

View File

@ -5,6 +5,7 @@
package pl.szczodrzynski.edziennik.ext package pl.szczodrzynski.edziennik.ext
import android.os.Bundle import android.os.Bundle
import com.google.gson.Gson
import com.google.gson.JsonArray import com.google.gson.JsonArray
import com.google.gson.JsonElement import com.google.gson.JsonElement
import com.google.gson.JsonObject import com.google.gson.JsonObject
@ -48,6 +49,11 @@ fun JsonObject.putEnum(key: String, value: Enum<*>) = addProperty(key, value.toI
fun String.toJsonObject(): JsonObject? = try { JsonParser.parseString(this).asJsonObject } catch (ignore: Exception) { null } fun String.toJsonObject(): JsonObject? = try { JsonParser.parseString(this).asJsonObject } catch (ignore: Exception) { null }
fun String.toJsonArray(): JsonArray? = try { JsonParser.parseString(this).asJsonArray } catch (ignore: Exception) { null } fun String.toJsonArray(): JsonArray? = try { JsonParser.parseString(this).asJsonArray } catch (ignore: Exception) { null }
fun Any?.toJsonElement(): JsonElement = when (this) {
is Collection<*> -> JsonArray(this)
else -> Gson().toJsonTree(this)
}
operator fun JsonObject.set(key: String, value: JsonElement) = this.add(key, value) operator fun JsonObject.set(key: String, value: JsonElement) = this.add(key, value)
operator fun JsonObject.set(key: String, value: Boolean) = this.addProperty(key, value) operator fun JsonObject.set(key: String, value: Boolean) = this.addProperty(key, value)
operator fun JsonObject.set(key: String, value: String?) = this.addProperty(key, value) operator fun JsonObject.set(key: String, value: String?) = this.addProperty(key, value)
@ -67,6 +73,7 @@ fun JsonObject(vararg properties: Pair<String, Any?>): JsonObject {
is Number -> addProperty(key, value) is Number -> addProperty(key, value)
is Boolean -> addProperty(key, value) is Boolean -> addProperty(key, value)
is Enum<*> -> addProperty(key, value.toInt()) is Enum<*> -> addProperty(key, value.toInt())
else -> add(key, property.toJsonElement())
} }
} }
} }
@ -98,6 +105,8 @@ fun JsonArray(properties: Collection<Any?>): JsonArray {
is Char -> add(property as Char?) is Char -> add(property as Char?)
is Number -> add(property as Number?) is Number -> add(property as Number?)
is Boolean -> add(property as Boolean?) is Boolean -> add(property as Boolean?)
is Enum<*> -> add(property.toInt())
else -> add(property.toJsonElement())
} }
} }
} }