Fix network reconnection with sync

This commit is contained in:
ganfra 2019-08-02 11:35:58 +02:00
parent 9cc922a8a2
commit a2b6bd0f62
2 changed files with 34 additions and 18 deletions

View File

@ -110,7 +110,7 @@ dependencies {
implementation 'com.squareup.retrofit2:converter-moshi:2.4.0' implementation 'com.squareup.retrofit2:converter-moshi:2.4.0'
implementation 'com.squareup.okhttp3:okhttp:3.14.1' implementation 'com.squareup.okhttp3:okhttp:3.14.1'
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0' 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" implementation "com.squareup.moshi:moshi-adapters:$moshi_version"
kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version" kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"



View File

@ -19,35 +19,45 @@ package im.vector.matrix.android.internal.network
import android.content.Context import android.content.Context
import com.novoda.merlin.Merlin import com.novoda.merlin.Merlin
import com.novoda.merlin.MerlinsBeard import com.novoda.merlin.MerlinsBeard
import com.novoda.merlin.registerable.connection.Connectable
import im.vector.matrix.android.internal.di.MatrixScope import im.vector.matrix.android.internal.di.MatrixScope
import timber.log.Timber
import java.util.*
import javax.inject.Inject import javax.inject.Inject
import kotlin.collections.ArrayList


@MatrixScope @MatrixScope
internal class NetworkConnectivityChecker @Inject constructor(context: Context) { internal class NetworkConnectivityChecker @Inject constructor(context: Context) {


private val merlin = Merlin.Builder().withConnectableCallbacks().build(context) private val merlin = Merlin.Builder()
private val merlinsBeard = MerlinsBeard.from(context) .withConnectableCallbacks()
.withDisconnectableCallbacks()
.build(context)


private val listeners = ArrayList<Listener>() private val merlinsBeard = MerlinsBeard.Builder().build(context)
private val listeners = Collections.synchronizedList(ArrayList<Listener>())

init {
merlin.bind()
merlin.registerDisconnectable {
Timber.v("On Disconnect")
listeners.forEach {
it.onDisconnect()
}
}
merlin.registerConnectable {
Timber.v("On Connect")
listeners.forEach {
it.onConnect()
}
}
}


fun register(listener: Listener) { fun register(listener: Listener) {
if (listeners.isEmpty()) {
merlin.bind()
}
listeners.add(listener) listeners.add(listener)
val connectable = Connectable {
if (listeners.contains(listener)) {
listener.onConnect()
}
}
merlin.registerConnectable(connectable)
} }


fun unregister(listener: Listener) { fun unregister(listener: Listener) {
if (listeners.remove(listener) && listeners.isEmpty()) { listeners.remove(listener)
merlin.unbind()
}
} }


fun isConnected(): Boolean { fun isConnected(): Boolean {
@ -55,7 +65,13 @@ internal class NetworkConnectivityChecker @Inject constructor(context: Context)
} }


interface Listener { interface Listener {
fun onConnect() fun onConnect() {

}

fun onDisconnect() {

}
} }