diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientViewModel.kt b/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientViewModel.kt index a107c078..ce1122f5 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientViewModel.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientViewModel.kt @@ -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() 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) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt index 911b47ec..b98dfb49 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt @@ -224,8 +224,9 @@ class ClientsFragment : Fragment() { binding.clients.adapter = adapter binding.swipeRefresher.setColorSchemeResources(R.color.colorSecondary) binding.swipeRefresher.setOnRefreshListener { IpNeighbourMonitor.instance?.flushAsync() } - activityViewModels().value.clients.observe(viewLifecycleOwner) { - adapter.submitList(it.toMutableList()) + activityViewModels().value.apply { + lifecycle.addObserver(this) + clients.observe(viewLifecycleOwner) { adapter.submitList(it.toMutableList()) } } return binding.root }