Change 'ConnectivityChangeListener' interface

Distinguish between 'connectivity changed' and 'connectivity lost' events.
This commit is contained in:
cketti 2021-07-04 21:12:19 +02:00
parent d408a5670e
commit 830b358ff3
6 changed files with 26 additions and 7 deletions

View file

@ -45,7 +45,10 @@ class PushController internal constructor(
private val pushers = mutableMapOf<String, AccountPushController>()
private val autoSyncListener = AutoSyncListener(::onAutoSyncChanged)
private val connectivityChangeListener = ConnectivityChangeListener(::onConnectivityChanged)
private val connectivityChangeListener = object : ConnectivityChangeListener {
override fun onConnectivityChanged() = this@PushController.onConnectivityChanged()
override fun onConnectivityLost() = this@PushController.onConnectivityChanged()
}
/**
* Initialize [PushController].

View file

@ -11,8 +11,9 @@ interface ConnectivityManager {
fun removeListener(listener: ConnectivityChangeListener)
}
fun interface ConnectivityChangeListener {
interface ConnectivityChangeListener {
fun onConnectivityChanged()
fun onConnectivityLost()
}
internal fun ConnectivityManager(systemConnectivityManager: SystemConnectivityManager): ConnectivityManager {

View file

@ -33,7 +33,11 @@ internal class ConnectivityManagerApi21(
if (networkType != lastNetworkType || isConnected != wasConnected) {
lastNetworkType = networkType
wasConnected = isConnected
notifyListeners()
if (isConnected) {
notifyOnConnectivityChanged()
} else {
notifyOnConnectivityLost()
}
}
}
}

View file

@ -36,7 +36,11 @@ internal class ConnectivityManagerApi23(
if (activeNetwork != lastActiveNetwork || isConnected != wasConnected) {
lastActiveNetwork = activeNetwork
wasConnected = isConnected
notifyListeners()
if (isConnected) {
notifyOnConnectivityChanged()
} else {
notifyOnConnectivityLost()
}
}
}
}

View file

@ -20,7 +20,7 @@ internal class ConnectivityManagerApi24(
Timber.v("Network available: $network")
synchronized(this@ConnectivityManagerApi24) {
isNetworkAvailable = true
notifyListeners()
notifyOnConnectivityChanged()
}
}
@ -28,7 +28,7 @@ internal class ConnectivityManagerApi24(
Timber.v("Network lost: $network")
synchronized(this@ConnectivityManagerApi24) {
isNetworkAvailable = false
notifyListeners()
notifyOnConnectivityLost()
}
}
}

View file

@ -16,9 +16,16 @@ internal abstract class ConnectivityManagerBase : ConnectivityManager {
}
@Synchronized
protected fun notifyListeners() {
protected fun notifyOnConnectivityChanged() {
for (listener in listeners) {
listener.onConnectivityChanged()
}
}
@Synchronized
protected fun notifyOnConnectivityLost() {
for (listener in listeners) {
listener.onConnectivityLost()
}
}
}