Only register full callback when ClientsFragment is active
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user