From a2b6bd0f62bd33d052623bec105494279a07ae00 Mon Sep 17 00:00:00 2001 From: ganfra Date: Fri, 2 Aug 2019 11:35:58 +0200 Subject: [PATCH] Fix network reconnection with sync --- matrix-sdk-android/build.gradle | 2 +- .../network/NetworkConnectivityChecker.kt | 50 ++++++++++++------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/matrix-sdk-android/build.gradle b/matrix-sdk-android/build.gradle index f9f89388..1755fb50 100644 --- a/matrix-sdk-android/build.gradle +++ b/matrix-sdk-android/build.gradle @@ -110,7 +110,7 @@ dependencies { implementation 'com.squareup.retrofit2:converter-moshi:2.4.0' implementation 'com.squareup.okhttp3:okhttp:3.14.1' implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0' - implementation 'com.novoda:merlin:1.1.6' + implementation 'com.novoda:merlin:1.2.0' implementation "com.squareup.moshi:moshi-adapters:$moshi_version" kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version" diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/NetworkConnectivityChecker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/NetworkConnectivityChecker.kt index 9502cb28..9b5da44b 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/NetworkConnectivityChecker.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/NetworkConnectivityChecker.kt @@ -19,35 +19,45 @@ package im.vector.matrix.android.internal.network import android.content.Context import com.novoda.merlin.Merlin import com.novoda.merlin.MerlinsBeard -import com.novoda.merlin.registerable.connection.Connectable import im.vector.matrix.android.internal.di.MatrixScope +import timber.log.Timber +import java.util.* import javax.inject.Inject +import kotlin.collections.ArrayList @MatrixScope internal class NetworkConnectivityChecker @Inject constructor(context: Context) { - private val merlin = Merlin.Builder().withConnectableCallbacks().build(context) - private val merlinsBeard = MerlinsBeard.from(context) + private val merlin = Merlin.Builder() + .withConnectableCallbacks() + .withDisconnectableCallbacks() + .build(context) - private val listeners = ArrayList() + private val merlinsBeard = MerlinsBeard.Builder().build(context) + private val listeners = Collections.synchronizedList(ArrayList()) - fun register(listener: Listener) { - if (listeners.isEmpty()) { - merlin.bind() - } - listeners.add(listener) - val connectable = Connectable { - if (listeners.contains(listener)) { - listener.onConnect() + init { + merlin.bind() + merlin.registerDisconnectable { + Timber.v("On Disconnect") + listeners.forEach { + it.onDisconnect() } } - merlin.registerConnectable(connectable) + merlin.registerConnectable { + Timber.v("On Connect") + listeners.forEach { + it.onConnect() + } + } + } + + fun register(listener: Listener) { + listeners.add(listener) } fun unregister(listener: Listener) { - if (listeners.remove(listener) && listeners.isEmpty()) { - merlin.unbind() - } + listeners.remove(listener) } fun isConnected(): Boolean { @@ -55,7 +65,13 @@ internal class NetworkConnectivityChecker @Inject constructor(context: Context) } interface Listener { - fun onConnect() + fun onConnect() { + + } + + fun onDisconnect() { + + } }