Only monitor when necessary
This commit is contained in:
@@ -29,6 +29,7 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
|
||||
binding.navigation.setOnNavigationItemSelectedListener(this)
|
||||
if (savedInstanceState == null) displayFragment(TetheringFragment())
|
||||
val model by viewModels<ClientViewModel>()
|
||||
lifecycle.addObserver(model)
|
||||
if (Services.p2p != null) ServiceForegroundConnector(this, model, RepeaterService::class)
|
||||
model.clients.observe(this) { clients ->
|
||||
val count = clients.count {
|
||||
|
||||
@@ -31,6 +31,14 @@ class ClientViewModel : ViewModel(), ServiceConnection, IpNeighbourMonitor.Callb
|
||||
private var p2p: Collection<WifiP2pDevice> = emptyList()
|
||||
private var neighbours: Collection<IpNeighbour> = emptyList()
|
||||
val clients = MutableLiveData<List<Client>>()
|
||||
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<Pair<String, MacAddressCompat>, 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
|
||||
|
||||
@@ -225,7 +225,7 @@ class ClientsFragment : Fragment() {
|
||||
binding.swipeRefresher.setColorSchemeResources(R.color.colorSecondary)
|
||||
binding.swipeRefresher.setOnRefreshListener { IpNeighbourMonitor.instance?.flushAsync() }
|
||||
activityViewModels<ClientViewModel>().value.apply {
|
||||
lifecycle.addObserver(this)
|
||||
lifecycle.addObserver(fullMode)
|
||||
clients.observe(viewLifecycleOwner) { adapter.submitList(it.toMutableList()) }
|
||||
}
|
||||
return binding.root
|
||||
|
||||
Reference in New Issue
Block a user