Only register full callback when ClientsFragment is active

This commit is contained in:
Mygod
2020-06-27 23:00:04 -04:00
parent a13a37a822
commit 5c6b22a6d2
2 changed files with 13 additions and 3 deletions

View File

@@ -5,6 +5,8 @@ import android.content.IntentFilter
import android.content.ServiceConnection
import android.net.wifi.p2p.WifiP2pDevice
import android.os.IBinder
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import be.mygod.vpnhotspot.App.Companion.app
@@ -17,7 +19,7 @@ import be.mygod.vpnhotspot.net.TetheringManager.tetheredIfaces
import be.mygod.vpnhotspot.net.monitor.IpNeighbourMonitor
import be.mygod.vpnhotspot.util.broadcastReceiver
class ClientViewModel : ViewModel(), ServiceConnection, IpNeighbourMonitor.Callback {
class ClientViewModel : ViewModel(), ServiceConnection, IpNeighbourMonitor.Callback, DefaultLifecycleObserver {
private var tetheredInterfaces = emptySet<String>()
private val receiver = broadcastReceiver { _, intent ->
tetheredInterfaces = (intent.tetheredIfaces ?: return@broadcastReceiver).toSet() +
@@ -59,8 +61,15 @@ class ClientViewModel : ViewModel(), ServiceConnection, IpNeighbourMonitor.Callb
init {
app.registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
IpNeighbourMonitor.registerCallback(this)
}
override fun onStart(owner: LifecycleOwner) {
IpNeighbourMonitor.registerCallback(this, true)
}
override fun onStop(owner: LifecycleOwner) {
IpNeighbourMonitor.registerCallback(this, false)
}
override fun onCleared() {
IpNeighbourMonitor.unregisterCallback(this)

View File

@@ -224,8 +224,9 @@ class ClientsFragment : Fragment() {
binding.clients.adapter = adapter
binding.swipeRefresher.setColorSchemeResources(R.color.colorSecondary)
binding.swipeRefresher.setOnRefreshListener { IpNeighbourMonitor.instance?.flushAsync() }
activityViewModels<ClientViewModel>().value.clients.observe(viewLifecycleOwner) {
adapter.submitList(it.toMutableList())
activityViewModels<ClientViewModel>().value.apply {
lifecycle.addObserver(this)
clients.observe(viewLifecycleOwner) { adapter.submitList(it.toMutableList()) }
}
return binding.root
}