Can write it better ??:slightly_smiling_face: ```...
# announcements
a
Can write it better ??🙂
Copy code
val listType = object : TypeToken<List<Cookie>>() {}.type
val encodedCookie = sharedPreferences.getString(url.host, null)
val cookies: List<Cookie>? = encodedCookie?.run { gson.fromJson(encodedCookie, listType) }
return cookies ?: ArrayList()
d
Yes 🙂
orEmpty()
is a thing.
👍 1
Could also return early if encoded cookie is null.
a
like this is better ? :
Copy code
return sharedPreferences.getString(url.host, null)?.run { gson.fromJson(this, listType) } ?: ArrayList()
s
I would use
emptyList()
instead of ArrayList
👍 2
j
Also i would make a nice kotlin function with reified to get the type token for gson
Copy code
fun <reified T: Any> gsonTypeToken() = object: TypeToken<T>() {}.type
👍 1
This should work, cannot test though