From 6ef60942e04c8d517ec738199a7df2aeaf2e20e3 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Mon, 23 Oct 2023 16:55:42 +0200 Subject: [PATCH] fix: Use built in serializer to parse AppSettings --- .../java/app/myzel394/alibi/db/AppSettings.kt | 70 ++----------------- 1 file changed, 6 insertions(+), 64 deletions(-) diff --git a/app/src/main/java/app/myzel394/alibi/db/AppSettings.kt b/app/src/main/java/app/myzel394/alibi/db/AppSettings.kt index f1fda67..cda7f9a 100644 --- a/app/src/main/java/app/myzel394/alibi/db/AppSettings.kt +++ b/app/src/main/java/app/myzel394/alibi/db/AppSettings.kt @@ -7,6 +7,7 @@ import app.myzel394.alibi.R import com.arthenica.ffmpegkit.FFmpegKit import com.arthenica.ffmpegkit.ReturnCode import kotlinx.serialization.Serializable +import kotlinx.serialization.json.Json import org.json.JSONObject import java.io.File import java.time.LocalDateTime @@ -42,45 +43,18 @@ data class AppSettings( DARK, } - fun toJSONObject(): JSONObject { - return JSONObject( - mapOf( - "audioRecorderSettings" to audioRecorderSettings.toJSONObject(), - "hasSeenOnboarding" to hasSeenOnboarding, - "showAdvancedSettings" to showAdvancedSettings, - "theme" to theme.name, - ) - ) - } - fun exportToString(): String { - return JSONObject( - mapOf( - "_meta" to mapOf( - "version" to 1, - "date" to LocalDateTime.now().format(ISO_DATE_TIME), - "app" to "app.myzel394.alibi", - ), - "data" to toJSONObject(), - ) - ).toString(0) + return Json.encodeToString(serializer(), this) } companion object { fun getDefaultInstance(): AppSettings = AppSettings() - fun fromJSONObject(data: JSONObject): AppSettings { - return AppSettings( - audioRecorderSettings = AudioRecorderSettings.fromJSONObject(data.getJSONObject("audioRecorderSettings")), - hasSeenOnboarding = data.getBoolean("hasSeenOnboarding"), - showAdvancedSettings = data.getBoolean("showAdvancedSettings"), - theme = Theme.valueOf(data.getString("theme")), - ) - } - fun fromExportedString(data: String): AppSettings { - val json = JSONObject(data) - return fromJSONObject(json.getJSONObject("data")) + return Json.decodeFromString( + serializer(), + data, + ) } } } @@ -332,20 +306,6 @@ data class AudioRecorderSettings( return supportedFormats.contains(outputFormat) } - fun toJSONObject(): JSONObject { - return JSONObject( - mapOf( - "maxDuration" to maxDuration, - "intervalDuration" to intervalDuration, - "forceExactMaxDuration" to forceExactMaxDuration, - "bitRate" to bitRate, - "samplingRate" to samplingRate, - "outputFormat" to outputFormat, - "encoder" to encoder, - ) - ) - } - companion object { fun getDefaultInstance(): AudioRecorderSettings = AudioRecorderSettings() val EXAMPLE_MAX_DURATIONS = listOf( @@ -452,24 +412,6 @@ data class AudioRecorderSettings( } } }).toMap() - - fun fromJSONObject(data: JSONObject): AudioRecorderSettings { - return AudioRecorderSettings( - maxDuration = data.getLong("maxDuration"), - intervalDuration = data.getLong("intervalDuration"), - forceExactMaxDuration = data.getBoolean("forceExactMaxDuration"), - bitRate = data.getInt("bitRate"), - samplingRate = data.optInt("samplingRate", -1).let { - if (it == -1) null else it - }, - outputFormat = data.optInt("outputFormat", -1).let { - if (it == -1) null else it - }, - encoder = data.optInt("encoder", -1).let { - if (it == -1) null else it - }, - ) - } } }