From 91c318fa9006c59f66490eb57ef14d975e68c032 Mon Sep 17 00:00:00 2001 From: Mygod Date: Sun, 28 Jun 2020 11:09:54 -0400 Subject: [PATCH] Only monitor when necessary --- .../java/be/mygod/vpnhotspot/MainActivity.kt | 1 + .../mygod/vpnhotspot/client/ClientViewModel.kt | 18 ++++++++++-------- .../mygod/vpnhotspot/client/ClientsFragment.kt | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt b/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt index f69e5175..2c24f107 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt @@ -29,6 +29,7 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS binding.navigation.setOnNavigationItemSelectedListener(this) if (savedInstanceState == null) displayFragment(TetheringFragment()) val model by viewModels() + lifecycle.addObserver(model) if (Services.p2p != null) ServiceForegroundConnector(this, model, RepeaterService::class) model.clients.observe(this) { clients -> val count = clients.count { 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 ce1122f5..e58b3bc0 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientViewModel.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientViewModel.kt @@ -31,6 +31,14 @@ class ClientViewModel : ViewModel(), ServiceConnection, IpNeighbourMonitor.Callb private var p2p: Collection = emptyList() private var neighbours: Collection = emptyList() val clients = MutableLiveData>() + val fullMode = object : DefaultLifecycleObserver { + override fun onStart(owner: LifecycleOwner) { + IpNeighbourMonitor.registerCallback(this@ClientViewModel, true) + } + override fun onStop(owner: LifecycleOwner) { + IpNeighbourMonitor.registerCallback(this@ClientViewModel, false) + } + } private fun populateClients() { val clients = HashMap, Client>() @@ -61,20 +69,14 @@ 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 onStop(owner: LifecycleOwner) = IpNeighbourMonitor.unregisterCallback(this) - override fun onCleared() { - IpNeighbourMonitor.unregisterCallback(this) - app.unregisterReceiver(receiver) - } + override fun onCleared() = app.unregisterReceiver(receiver) override fun onServiceConnected(name: ComponentName?, service: IBinder?) { val binder = service as RepeaterService.Binder 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 b98dfb49..ed1aba79 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt @@ -225,7 +225,7 @@ class ClientsFragment : Fragment() { binding.swipeRefresher.setColorSchemeResources(R.color.colorSecondary) binding.swipeRefresher.setOnRefreshListener { IpNeighbourMonitor.instance?.flushAsync() } activityViewModels().value.apply { - lifecycle.addObserver(this) + lifecycle.addObserver(fullMode) clients.observe(viewLifecycleOwner) { adapter.submitList(it.toMutableList()) } } return binding.root