Skip to content

Commit

Permalink
PIA-1904: Hotfix: Crash on missing field
Browse files Browse the repository at this point in the history
  • Loading branch information
kp-iva-nedeleva committed Jun 5, 2024
1 parent cb813f5 commit 292b44c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ plugins {
id("org.jetbrains.kotlinx.kover")
}

val googleAppVersionCode = 669
val googleAppVersionCode = 670
val amazonAppVersionCode = googleAppVersionCode.plus(10000)
val noInAppVersionCode = googleAppVersionCode.plus(10000)
val appVersionName = "4.0.6"
val appVersionName = "4.0.7"

android {
namespace = "com.kape.vpn"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import android.content.Context
import com.kape.regions.data.ServerData
import com.kape.utils.Prefs
import com.kape.utils.vpnserver.VpnServer
import com.kape.utils.vpnserver.VpnServerOutdated
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.MissingFieldException
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

Expand Down Expand Up @@ -47,8 +50,17 @@ class VpnRegionPrefs(context: Context) : Prefs(context, "vpn-regions") {
setVpnReconnect(true)
}

fun getSelectedServer(): VpnServer? = prefs.getString(VPN_SELECTED_SERVER, null)?.let {
Json.decodeFromString(it)
@OptIn(ExperimentalSerializationApi::class)
fun getSelectedServer(): VpnServer? {
return try {
prefs.getString(VPN_SELECTED_SERVER, null)?.let {
Json.decodeFromString<VpnServer>(it)
}
} catch (exception: MissingFieldException) {
prefs.getString(VPN_SELECTED_SERVER, null)?.let {
Json.decodeFromString<VpnServerOutdated>(it).toVpnServer()
}
}
}

fun needsVpnReconnect() = prefs.getBoolean(VPN_RECONNECT, false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.kape.utils.vpnserver

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class VpnServerOutdated(
val name: String,
val iso: String,
val dns: String,
var latency: String?,
val endpoints: Map<VpnServer.ServerGroup, List<VpnServer.ServerEndpointDetails>>,
val key: String,
val latitude: String?,
val longitude: String?,
val isGeo: Boolean,
val isOffline: Boolean,
@SerialName("isAllowsPF")
val allowsPortForwarding: Boolean,
val dipToken: String?,
val dedicatedIp: String?,
val isDedicatedIp: Boolean = !dedicatedIp.isNullOrEmpty(),
) {
fun toVpnServer(): VpnServer = VpnServer(
name,
iso,
dns,
latency,
endpoints,
key,
latitude,
longitude,
isGeo,
isOffline,
allowsPortForwarding,
false,
dipToken,
dedicatedIp,
isDedicatedIp,
)
}

0 comments on commit 292b44c

Please sign in to comment.