mirror of
https://github.com/Myzel394/Alibi.git
synced 2025-06-19 07:15:25 +02:00
fix: Use built in serializer to parse AppSettings
This commit is contained in:
parent
01a6f49b77
commit
6ef60942e0
@ -7,6 +7,7 @@ import app.myzel394.alibi.R
|
|||||||
import com.arthenica.ffmpegkit.FFmpegKit
|
import com.arthenica.ffmpegkit.FFmpegKit
|
||||||
import com.arthenica.ffmpegkit.ReturnCode
|
import com.arthenica.ffmpegkit.ReturnCode
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
@ -42,45 +43,18 @@ data class AppSettings(
|
|||||||
DARK,
|
DARK,
|
||||||
}
|
}
|
||||||
|
|
||||||
fun toJSONObject(): JSONObject {
|
|
||||||
return JSONObject(
|
|
||||||
mapOf(
|
|
||||||
"audioRecorderSettings" to audioRecorderSettings.toJSONObject(),
|
|
||||||
"hasSeenOnboarding" to hasSeenOnboarding,
|
|
||||||
"showAdvancedSettings" to showAdvancedSettings,
|
|
||||||
"theme" to theme.name,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun exportToString(): String {
|
fun exportToString(): String {
|
||||||
return JSONObject(
|
return Json.encodeToString(serializer(), this)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun getDefaultInstance(): AppSettings = AppSettings()
|
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 {
|
fun fromExportedString(data: String): AppSettings {
|
||||||
val json = JSONObject(data)
|
return Json.decodeFromString(
|
||||||
return fromJSONObject(json.getJSONObject("data"))
|
serializer(),
|
||||||
|
data,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -332,20 +306,6 @@ data class AudioRecorderSettings(
|
|||||||
return supportedFormats.contains(outputFormat)
|
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 {
|
companion object {
|
||||||
fun getDefaultInstance(): AudioRecorderSettings = AudioRecorderSettings()
|
fun getDefaultInstance(): AudioRecorderSettings = AudioRecorderSettings()
|
||||||
val EXAMPLE_MAX_DURATIONS = listOf(
|
val EXAMPLE_MAX_DURATIONS = listOf(
|
||||||
@ -452,24 +412,6 @@ data class AudioRecorderSettings(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).toMap()
|
}).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
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user