diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-08-23 18:38:56 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-08-29 11:13:17 +0000 |
| commit | 84a66d4a27b16561aa33b91f89222d1b963e16ce (patch) | |
| tree | a59db020dbef8d866c4e761a991ae4a3178cb7d4 /android/src | |
| parent | e9f28bba48483c1bcb9aca0a664100c8d175e0fb (diff) | |
| download | mullvadvpn-84a66d4a27b16561aa33b91f89222d1b963e16ce.tar.xz mullvadvpn-84a66d4a27b16561aa33b91f89222d1b963e16ce.zip | |
Use `EventNotifier` in `ConnectionProxy`
Diffstat (limited to 'android/src')
3 files changed, 22 insertions, 19 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt index 1e7f16e94d..fcf3788971 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt @@ -39,9 +39,10 @@ class ConnectFragment : Fragment() { private lateinit var versionInfoCache: AppVersionInfoCache private lateinit var updateKeyStatusJob: Job - private lateinit var updateTunnelStateJob: Job + private var updateTunnelStateJob: Job? = null private var isTunnelInfoExpanded = false + private var tunnelStateListener: Int? = null override fun onAttach(context: Context) { super.onAttach(context) @@ -97,10 +98,10 @@ class ConnectFragment : Fragment() { override fun onResume() { super.onResume() - notificationBanner.onResume() - locationInfo.isTunnelInfoExpanded = isTunnelInfoExpanded + notificationBanner.onResume() + keyStatusListener.onKeyStatusChange = { keyStatus -> updateKeyStatusJob.cancel() updateKeyStatusJob = updateKeyStatus(keyStatus) @@ -114,9 +115,8 @@ class ConnectFragment : Fragment() { switchLocationButton.location = selectedRelayItem } - updateTunnelStateJob = updateTunnelState(connectionProxy.uiState) - connectionProxy.onUiStateChange = { uiState -> - updateTunnelStateJob.cancel() + tunnelStateListener = connectionProxy.onUiStateChange.subscribe { uiState -> + updateTunnelStateJob?.cancel() updateTunnelStateJob = updateTunnelState(uiState) } } @@ -126,14 +126,15 @@ class ConnectFragment : Fragment() { locationInfoCache.onNewLocation = null relayListListener.onRelayListChange = null - connectionProxy.onUiStateChange = null - updateTunnelStateJob.cancel() + tunnelStateListener?.let { listener -> + connectionProxy.onUiStateChange.unsubscribe(listener) + } + updateTunnelStateJob?.cancel() + notificationBanner.onPause() isTunnelInfoExpanded = locationInfo.isTunnelInfoExpanded - notificationBanner.onPause() - super.onPause() } diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/WireguardKeyFragment.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/WireguardKeyFragment.kt index 9dd6e304e4..917cbf37c9 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/WireguardKeyFragment.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/WireguardKeyFragment.kt @@ -28,6 +28,7 @@ class WireguardKeyFragment : Fragment() { private var keyState: KeygenEvent? = null private var currentJob: Job? = null private var updateViewsJob: Job? = null + private var tunnelStateListener: Int? = null private lateinit var parentActivity: MainActivity private lateinit var connectionProxy: ConnectionProxy private lateinit var keyStatusListener: KeyStatusListener @@ -205,7 +206,10 @@ class WireguardKeyFragment : Fragment() { } override fun onPause() { - connectionProxy.onUiStateChange = null + tunnelStateListener?.let { listener -> + connectionProxy.onUiStateChange.unsubscribe(listener) + } + keyStatusListener.onKeyStatusChange = null currentJob?.cancel() updateViewsJob?.cancel() @@ -216,7 +220,8 @@ class WireguardKeyFragment : Fragment() { override fun onResume() { super.onResume() - connectionProxy.onUiStateChange = { _ -> + + tunnelStateListener = connectionProxy.onUiStateChange.subscribe { _ -> updateViewsJob?.cancel() updateViewsJob = updateViewJob() } diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/ConnectionProxy.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/ConnectionProxy.kt index fe7e2bf308..2c46e34292 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/ConnectionProxy.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/ConnectionProxy.kt @@ -15,6 +15,7 @@ import net.mullvad.mullvadvpn.MainActivity import net.mullvad.mullvadvpn.MullvadDaemon import net.mullvad.mullvadvpn.model.ActionAfterDisconnect import net.mullvad.mullvadvpn.model.TunnelState +import net.mullvad.mullvadvpn.util.EventNotifier class ConnectionProxy(val context: Context, val daemon: Deferred<MullvadDaemon>) { var mainActivity: MainActivity? = null @@ -38,15 +39,10 @@ class ConnectionProxy(val context: Context, val daemon: Deferred<MullvadDaemon>) var uiState: TunnelState = TunnelState.Disconnected() private set(value) { field = value - onUiStateChange?.invoke(value) - } - - var onUiStateChange: ((TunnelState) -> Unit)? = null - set(value) { - field = value - value?.invoke(uiState) + onUiStateChange.notify(value) } + var onUiStateChange = EventNotifier(uiState) var vpnPermission = CompletableDeferred<Boolean>() fun connect() { @@ -77,6 +73,7 @@ class ConnectionProxy(val context: Context, val daemon: Deferred<MullvadDaemon>) } fun onDestroy() { + onUiStateChange.unsubscribeAll() attachListenerJob.cancel() detachListener() fetchInitialStateJob.cancel() |
