Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

- Fix Network Store calling unregister on already unregistered callbacks #1274

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions app/src/main/java/mozilla/lockbox/store/NetworkStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,8 @@ open class NetworkStore(

var isConnectedState: Boolean = true
internal set
private val connectivityCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
isConnectedState = connectivityManager.isOnline(network)
}

override fun onLost(network: Network) {
isConnectedState = connectivityManager.isOnline(network)
}
}
private var connectivityCallback: ConnectivityManager.NetworkCallback? = null

companion object {
val shared = NetworkStore()
Expand All @@ -54,7 +47,14 @@ open class NetworkStore(
isConnected = isConnectedSubject
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe { checkConnectivity() }
.doFinally { connectivityManager.unregisterNetworkCallback(connectivityCallback) }
.doFinally {

connectivityCallback?.let {
connectivityManager.unregisterNetworkCallback(it)
}

connectivityCallback = null
}
}

private fun checkConnectivity() {
Expand All @@ -63,6 +63,23 @@ open class NetworkStore(

override fun injectContext(context: Context) {
connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager.registerDefaultNetworkCallback(connectivityCallback)
if(connectivityCallback == null){
createNewConnectivityCallback()
}
connectivityCallback?.let {
connectivityManager.registerDefaultNetworkCallback(it)
}
}

private fun createNewConnectivityCallback() {
connectivityCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
isConnectedState = connectivityManager.isOnline(network)
}

override fun onLost(network: Network) {
isConnectedState = connectivityManager.isOnline(network)
}
}
}
}