diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-12-02 15:08:00 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-12-03 10:46:15 +0000 |
| commit | cd4c3e3f55855be68189168e33684224567bd6a8 (patch) | |
| tree | f24d839ff9ed7cf64c5e24f29d623423f27b4ac7 /android/src | |
| parent | 332536ee833e5d3917f855c81972fe445ac93758 (diff) | |
| download | mullvadvpn-cd4c3e3f55855be68189168e33684224567bd6a8.tar.xz mullvadvpn-cd4c3e3f55855be68189168e33684224567bd6a8.zip | |
Replace setters with observable delegates
Diffstat (limited to 'android/src')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/LocationInfoCache.kt | 66 |
1 files changed, 29 insertions, 37 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/LocationInfoCache.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/LocationInfoCache.kt index d6860bf72f..8ec3260680 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/LocationInfoCache.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/LocationInfoCache.kt @@ -1,5 +1,6 @@ package net.mullvad.mullvadvpn.service +import kotlin.properties.Delegates.observable import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.TimeoutCancellationException @@ -36,50 +37,41 @@ class LocationInfoCache( private var lastKnownRealLocation: GeoIpLocation? = null private var selectedRelayLocation: GeoIpLocation? = null - var onNewLocation: ((GeoIpLocation?) -> Unit)? = null - set(value) { - field = value - value?.invoke(location) - } - - var location: GeoIpLocation? = null - set(value) { - field = value - onNewLocation?.invoke(value) - } + var onNewLocation by observable<((GeoIpLocation?) -> Unit)?>(null) { _, _, callback -> + callback?.invoke(location) + } - var state: TunnelState = TunnelState.Disconnected() - set(value) { - field = value + var location: GeoIpLocation? by observable(null) { _, _, newLocation -> + onNewLocation?.invoke(newLocation) + } - when (value) { - is TunnelState.Disconnected -> { - location = lastKnownRealLocation - fetchRequestChannel.sendBlocking(RequestFetch.ForRealLocation) - } - is TunnelState.Connecting -> location = value.location - is TunnelState.Connected -> { - location = value.location - fetchRequestChannel.sendBlocking(RequestFetch.ForRelayLocation) - } - is TunnelState.Disconnecting -> { - when (value.actionAfterDisconnect) { - ActionAfterDisconnect.Nothing -> location = lastKnownRealLocation - ActionAfterDisconnect.Block -> location = null - ActionAfterDisconnect.Reconnect -> location = selectedRelayLocation - } + var state by observable<TunnelState>(TunnelState.Disconnected()) { _, _, newState -> + when (newState) { + is TunnelState.Disconnected -> { + location = lastKnownRealLocation + fetchRequestChannel.sendBlocking(RequestFetch.ForRealLocation) + } + is TunnelState.Connecting -> location = newState.location + is TunnelState.Connected -> { + location = newState.location + fetchRequestChannel.sendBlocking(RequestFetch.ForRelayLocation) + } + is TunnelState.Disconnecting -> { + when (newState.actionAfterDisconnect) { + ActionAfterDisconnect.Nothing -> location = lastKnownRealLocation + ActionAfterDisconnect.Block -> location = null + ActionAfterDisconnect.Reconnect -> location = selectedRelayLocation } - is TunnelState.Error -> location = null } + is TunnelState.Error -> location = null } + } - var selectedRelay: RelayItem? = null - set(value) { - if (field != value) { - field = value - updateSelectedRelayLocation(value) - } + var selectedRelay by observable<RelayItem?>(null) { _, oldRelay, newRelay -> + if (newRelay != oldRelay) { + updateSelectedRelayLocation(newRelay) } + } init { connectivityListener.connectivityNotifier.subscribe(this) { isConnected -> |
