summaryrefslogtreecommitdiffhomepage
path: root/android
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2019-12-11 15:03:37 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-01-07 14:33:53 +0000
commita714a0c95f5898b416cc965820c6dd63d38d45e8 (patch)
treef18a996c990853ec15bdf409dcbe41c852dcc45f /android
parent41a05f1fef46d6effbd112783271505a3045c741 (diff)
downloadmullvadvpn-a714a0c95f5898b416cc965820c6dd63d38d45e8.tar.xz
mullvadvpn-a714a0c95f5898b416cc965820c6dd63d38d45e8.zip
Remove listener handling in `RemainingTimeLabel`
Diffstat (limited to 'android')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/ui/RemainingTimeLabel.kt29
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/ui/SettingsFragment.kt24
2 files changed, 21 insertions, 32 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/RemainingTimeLabel.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/RemainingTimeLabel.kt
index ca927e50eb..b80573c5ec 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/RemainingTimeLabel.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/RemainingTimeLabel.kt
@@ -3,17 +3,12 @@ package net.mullvad.mullvadvpn.ui
import android.content.Context
import android.view.View
import android.widget.TextView
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.GlobalScope
-import kotlinx.coroutines.Job
-import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.R
-import net.mullvad.mullvadvpn.dataproxy.AccountCache
import org.joda.time.DateTime
import org.joda.time.Duration
import org.joda.time.PeriodType
-class RemainingTimeLabel(val context: Context, val accountCache: AccountCache, val view: View) {
+class RemainingTimeLabel(val context: Context, val view: View) {
private val resources = context.resources
private val expiredColor = resources.getColor(R.color.red)
@@ -21,25 +16,15 @@ class RemainingTimeLabel(val context: Context, val accountCache: AccountCache, v
private val label = view.findViewById<TextView>(R.id.remaining_time)
- private var updateJob: Job? = null
-
- fun onResume() {
- accountCache.apply {
- refetch()
-
- onAccountDataChange = { _, accountExpiry ->
- updateJob?.cancel()
- updateJob = updateLabel(accountExpiry)
- }
+ var accountExpiry: DateTime? = null
+ set(value) {
+ field = value
+ updateLabel()
}
- }
- fun onPause() {
- accountCache.onAccountDataChange = null
- updateJob?.cancel()
- }
+ private fun updateLabel() {
+ val expiry = accountExpiry
- private fun updateLabel(expiry: DateTime?) = GlobalScope.launch(Dispatchers.Main) {
if (expiry != null) {
val remainingTime = Duration(DateTime.now(), expiry)
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/SettingsFragment.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/SettingsFragment.kt
index b52e8432fa..fbaae8e557 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/SettingsFragment.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/SettingsFragment.kt
@@ -17,6 +17,7 @@ import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.dataproxy.AccountCache
import net.mullvad.mullvadvpn.dataproxy.AppVersionInfoCache
+import org.joda.time.DateTime
class SettingsFragment : ServiceAwareFragment() {
private lateinit var accountCache: AccountCache
@@ -29,7 +30,7 @@ class SettingsFragment : ServiceAwareFragment() {
private lateinit var remainingTimeLabel: RemainingTimeLabel
private lateinit var wireguardKeysMenu: View
- private var updateLoggedInStatusJob: Job? = null
+ private var updateAccountInfoJob: Job? = null
private var updateVersionInfoJob: Job? = null
override fun onNewServiceConnection(serviceConnection: ServiceConnection) {
@@ -73,7 +74,7 @@ class SettingsFragment : ServiceAwareFragment() {
appVersionWarning = view.findViewById(R.id.app_version_warning)
appVersionLabel = view.findViewById<TextView>(R.id.app_version_label)
appVersionFooter = view.findViewById(R.id.app_version_footer)
- remainingTimeLabel = RemainingTimeLabel(parentActivity, accountCache, view)
+ remainingTimeLabel = RemainingTimeLabel(parentActivity, view)
return view
}
@@ -81,11 +82,9 @@ class SettingsFragment : ServiceAwareFragment() {
override fun onResume() {
super.onResume()
- remainingTimeLabel.onResume()
-
- accountCache.onAccountDataChange = { account, _ ->
- updateLoggedInStatusJob?.cancel()
- updateLoggedInStatusJob = updateLoggedInStatus(account != null)
+ accountCache.onAccountDataChange = { account, expiry ->
+ updateAccountInfoJob?.cancel()
+ updateAccountInfoJob = updateAccountInfo(account != null, expiry)
}
versionInfoCache.onUpdate = {
@@ -97,12 +96,11 @@ class SettingsFragment : ServiceAwareFragment() {
override fun onPause() {
versionInfoCache.onUpdate = null
accountCache.onAccountDataChange = null
- remainingTimeLabel.onPause()
super.onPause()
}
override fun onDestroyView() {
- updateLoggedInStatusJob?.cancel()
+ updateAccountInfoJob?.cancel()
updateVersionInfoJob?.cancel()
super.onDestroyView()
}
@@ -127,7 +125,13 @@ class SettingsFragment : ServiceAwareFragment() {
startActivity(intent)
}
- private fun updateLoggedInStatus(loggedIn: Boolean) = GlobalScope.launch(Dispatchers.Main) {
+ private fun updateAccountInfo(loggedIn: Boolean, expiry: DateTime?)
+ = GlobalScope.launch(Dispatchers.Main) {
+ updateLoggedInStatus(loggedIn)
+ remainingTimeLabel.accountExpiry = expiry
+ }
+
+ private fun updateLoggedInStatus(loggedIn: Boolean) {
val visibility = if (loggedIn) {
View.VISIBLE
} else {