diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-06-08 23:05:05 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-06-17 14:15:15 +0000 |
| commit | 8e0c2a39a38d8c5c41a0bd87e3f70e7b106ec3cc (patch) | |
| tree | 35bcb052358d47fcec7fcbeb94a021b10ead1640 /android/src | |
| parent | c0c0e9577e3ec810271706a7f77541c3d34138b2 (diff) | |
| download | mullvadvpn-8e0c2a39a38d8c5c41a0bd87e3f70e7b106ec3cc.tar.xz mullvadvpn-8e0c2a39a38d8c5c41a0bd87e3f70e7b106ec3cc.zip | |
Use `autoSubscribable` delegate
Diffstat (limited to 'android/src')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt | 40 |
1 files changed, 15 insertions, 25 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt index c352361e76..5312af91f9 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt @@ -11,6 +11,7 @@ import kotlin.properties.Delegates.observable import net.mullvad.mullvadvpn.model.TunnelState import net.mullvad.mullvadvpn.service.notifications.TunnelStateNotification import net.mullvad.talpid.util.EventNotifier +import net.mullvad.talpid.util.autoSubscribable class ForegroundNotificationManager( val service: MullvadVpnService, @@ -29,30 +30,15 @@ class ForegroundNotificationManager( } } - private var connectionProxy by observable<ConnectionProxy?>(null) { _, oldValue, newValue -> - if (oldValue != newValue) { - oldValue?.onStateChange?.unsubscribe(this) - - newValue?.onStateChange?.subscribe(this) { state -> - tunnelState = state - } - } + private var accountNumberEvents by autoSubscribable<String?>(this, null) { accountNumber -> + loggedIn = accountNumber != null } - private var settingsListener by observable<SettingsListener?>(null) { _, oldValue, newValue -> - if (oldValue != newValue) { - oldValue?.accountNumberNotifier?.unsubscribe(this) - - newValue?.accountNumberNotifier?.subscribe(this) { accountNumber -> - loggedIn = accountNumber != null - } + private var tunnelStateEvents + by autoSubscribable<TunnelState>(this, TunnelState.Disconnected()) { newState -> + tunnelStateNotification.tunnelState = newState + updateNotification() } - } - - private var tunnelState by observable<TunnelState>(TunnelState.Disconnected()) { _, _, state -> - tunnelStateNotification.tunnelState = state - updateNotification() - } private var deviceIsUnlocked by observable(!keyguardManager.isDeviceLocked) { _, _, _ -> updateNotificationAction() @@ -62,6 +48,9 @@ class ForegroundNotificationManager( private var onForeground = false + private val tunnelState + get() = tunnelStateEvents?.latestEvent ?: TunnelState.Disconnected() + private val shouldBeOnForeground get() = lockedToForeground || !(tunnelState is TunnelState.Disconnected) @@ -69,8 +58,8 @@ class ForegroundNotificationManager( init { serviceNotifier.subscribe(this) { newServiceInstance -> - connectionProxy = newServiceInstance?.connectionProxy - settingsListener = newServiceInstance?.settingsListener + accountNumberEvents = newServiceInstance?.settingsListener?.accountNumberNotifier + tunnelStateEvents = newServiceInstance?.connectionProxy?.onStateChange } service.apply { @@ -85,8 +74,9 @@ class ForegroundNotificationManager( fun onDestroy() { serviceNotifier.unsubscribe(this) - connectionProxy = null - settingsListener = null + + accountNumberEvents = null + tunnelStateEvents = null service.unregisterReceiver(deviceLockListener) |
