get rid of gson

This commit is contained in:
Bnyro 2022-07-21 23:11:22 +02:00
parent 56b7a8e5cc
commit da38c83993
4 changed files with 34 additions and 27 deletions

View File

@ -90,5 +90,4 @@ dependencies {
implementation libs.cronet.embedded implementation libs.cronet.embedded
implementation libs.cronet.okhttp implementation libs.cronet.okhttp
implementation libs.coil implementation libs.coil
implementation libs.gson
} }

View File

@ -3,13 +3,11 @@ package com.github.libretube.preferences
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.obj.CustomInstance import com.github.libretube.obj.CustomInstance
import com.github.libretube.obj.Streams import com.github.libretube.obj.Streams
import com.github.libretube.obj.WatchHistoryItem import com.github.libretube.obj.WatchHistoryItem
import com.github.libretube.obj.WatchPosition import com.github.libretube.obj.WatchPosition
import com.google.common.reflect.TypeToken
import com.google.gson.Gson
import java.lang.reflect.Type
object PreferenceHelper { object PreferenceHelper {
private val TAG = "PreferenceHelper" private val TAG = "PreferenceHelper"
@ -92,21 +90,25 @@ object PreferenceHelper {
} }
fun saveCustomInstance(customInstance: CustomInstance) { fun saveCustomInstance(customInstance: CustomInstance) {
val gson = Gson() val mapper = ObjectMapper()
val customInstancesList = getCustomInstances() val customInstancesList = getCustomInstances()
customInstancesList += customInstance customInstancesList += customInstance
val json = gson.toJson(customInstancesList) val json = mapper.writeValueAsString(customInstancesList)
editor.putString("customInstances", json).apply() editor.putString("customInstances", json).apply()
} }
fun getCustomInstances(): ArrayList<CustomInstance> { fun getCustomInstances(): ArrayList<CustomInstance> {
val gson = Gson() val mapper = ObjectMapper()
val json: String = settings.getString("customInstances", "")!! val json: String = settings.getString("customInstances", "")!!
val type: Type = object : TypeToken<List<CustomInstance?>?>() {}.type val type = mapper.typeFactory.constructCollectionType(
List::class.java,
CustomInstance::class.java
)
return try { return try {
gson.fromJson(json, type) mapper.readValue(json, type)
} catch (e: Exception) { } catch (e: Exception) {
arrayListOf() arrayListOf()
} }
@ -127,7 +129,7 @@ object PreferenceHelper {
} }
fun addToWatchHistory(videoId: String, streams: Streams) { fun addToWatchHistory(videoId: String, streams: Streams) {
val gson = Gson() val mapper = ObjectMapper()
val watchHistoryItem = WatchHistoryItem( val watchHistoryItem = WatchHistoryItem(
videoId, videoId,
@ -151,16 +153,21 @@ object PreferenceHelper {
watchHistory += watchHistoryItem watchHistory += watchHistoryItem
val json = gson.toJson(watchHistory) val json = mapper.writeValueAsString(watchHistory)
editor.putString("watch_history", json).apply() editor.putString("watch_history", json).apply()
} }
fun getWatchHistory(): ArrayList<WatchHistoryItem> { fun getWatchHistory(): ArrayList<WatchHistoryItem> {
val gson = Gson() val mapper = ObjectMapper()
val json: String = settings.getString("watch_history", "")!! val json: String = settings.getString("watch_history", "")!!
val type: Type = object : TypeToken<List<WatchHistoryItem?>?>() {}.type val type = mapper.typeFactory.constructCollectionType(
List::class.java,
WatchHistoryItem::class.java
)
return try { return try {
gson.fromJson(json, type) mapper.readValue(json, type)
} catch (e: Exception) { } catch (e: Exception) {
arrayListOf() arrayListOf()
} }
@ -179,8 +186,8 @@ object PreferenceHelper {
watchPositions += watchPositionItem watchPositions += watchPositionItem
val gson = Gson() val mapper = ObjectMapper()
val json = gson.toJson(watchPositions) val json = mapper.writeValueAsString(watchPositions)
editor.putString("watch_positions", json).commit() editor.putString("watch_positions", json).commit()
} }
@ -194,19 +201,22 @@ object PreferenceHelper {
if (indexToRemove != null) watchPositions.removeAt(indexToRemove!!) if (indexToRemove != null) watchPositions.removeAt(indexToRemove!!)
val gson = Gson() val mapper = ObjectMapper()
val json = gson.toJson(watchPositions) val json = mapper.writeValueAsString(watchPositions)
editor.putString("watch_positions", json).commit() editor.putString("watch_positions", json).commit()
} }
fun getWatchPositions(): ArrayList<WatchPosition> { fun getWatchPositions(): ArrayList<WatchPosition> {
val gson = Gson() val mapper = ObjectMapper()
val json: String = settings.getString("watch_positions", "")!! val json: String = settings.getString("watch_positions", "")!!
val type: Type = object : TypeToken<List<WatchPosition?>?>() {}.type val type = mapper.typeFactory.constructCollectionType(
List::class.java,
WatchPosition::class.java
)
return try { return try {
gson.fromJson(json, type) mapper.readValue(json, type)
} catch (e: Exception) { } catch (e: Exception) {
arrayListOf() arrayListOf()
} }

View File

@ -1,7 +1,7 @@
package com.github.libretube.update package com.github.libretube.update
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.GITHUB_API_URL import com.github.libretube.GITHUB_API_URL
import com.google.gson.Gson
import java.net.URL import java.net.URL
object UpdateChecker { object UpdateChecker {
@ -29,7 +29,7 @@ object UpdateChecker {
val json = latestVersionApiUrl.readText() val json = latestVersionApiUrl.readText()
// Parse and return the json data // Parse and return the json data
val gson = Gson() val mapper = ObjectMapper()
return gson.fromJson(json, UpdateInfo::class.java) return mapper.readValue(json, UpdateInfo::class.java)
} }
} }

View File

@ -18,7 +18,6 @@ cronetEmbedded = "101.4951.41"
cronetOkHttp = "0.1.0" cronetOkHttp = "0.1.0"
coil = "2.1.0" coil = "2.1.0"
leakcanary = "2.8.1" leakcanary = "2.8.1"
gson = "2.9.0"
[libraries] [libraries]
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" } androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
@ -43,5 +42,4 @@ exoplayer-extension-cronet = { group = "com.google.android.exoplayer", name = "e
cronet-embedded = { group = "org.chromium.net", name = "cronet-embedded", version.ref = "cronetEmbedded" } cronet-embedded = { group = "org.chromium.net", name = "cronet-embedded", version.ref = "cronetEmbedded" }
cronet-okhttp = { group = "com.google.net.cronet", name = "cronet-okhttp", version.ref = "cronetOkHttp" } cronet-okhttp = { group = "com.google.net.cronet", name = "cronet-okhttp", version.ref = "cronetOkHttp" }
coil = { group = "io.coil-kt", name = "coil", version.ref="coil" } coil = { group = "io.coil-kt", name = "coil", version.ref="coil" }
square-leakcanary = { group = "com.squareup.leakcanary", name = "leakcanary-android", version.ref = "leakcanary" } square-leakcanary = { group = "com.squareup.leakcanary", name = "leakcanary-android", version.ref = "leakcanary" }
gson = { group = "com.google.code.gson", name="gson", version.ref = "gson"}