Only monitor when necessary
This commit is contained in:
@@ -29,6 +29,7 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
|
|||||||
binding.navigation.setOnNavigationItemSelectedListener(this)
|
binding.navigation.setOnNavigationItemSelectedListener(this)
|
||||||
if (savedInstanceState == null) displayFragment(TetheringFragment())
|
if (savedInstanceState == null) displayFragment(TetheringFragment())
|
||||||
val model by viewModels<ClientViewModel>()
|
val model by viewModels<ClientViewModel>()
|
||||||
|
lifecycle.addObserver(model)
|
||||||
if (Services.p2p != null) ServiceForegroundConnector(this, model, RepeaterService::class)
|
if (Services.p2p != null) ServiceForegroundConnector(this, model, RepeaterService::class)
|
||||||
model.clients.observe(this) { clients ->
|
model.clients.observe(this) { clients ->
|
||||||
val count = clients.count {
|
val count = clients.count {
|
||||||
|
|||||||
@@ -31,6 +31,14 @@ class ClientViewModel : ViewModel(), ServiceConnection, IpNeighbourMonitor.Callb
|
|||||||
private var p2p: Collection<WifiP2pDevice> = emptyList()
|
private var p2p: Collection<WifiP2pDevice> = emptyList()
|
||||||
private var neighbours: Collection<IpNeighbour> = emptyList()
|
private var neighbours: Collection<IpNeighbour> = emptyList()
|
||||||
val clients = MutableLiveData<List<Client>>()
|
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() {
|
private fun populateClients() {
|
||||||
val clients = HashMap<Pair<String, MacAddressCompat>, Client>()
|
val clients = HashMap<Pair<String, MacAddressCompat>, Client>()
|
||||||
@@ -61,20 +69,14 @@ class ClientViewModel : ViewModel(), ServiceConnection, IpNeighbourMonitor.Callb
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
app.registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
|
app.registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
|
||||||
IpNeighbourMonitor.registerCallback(this)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStart(owner: LifecycleOwner) {
|
override fun onStart(owner: LifecycleOwner) {
|
||||||
IpNeighbourMonitor.registerCallback(this, true)
|
|
||||||
}
|
|
||||||
override fun onStop(owner: LifecycleOwner) {
|
|
||||||
IpNeighbourMonitor.registerCallback(this, false)
|
IpNeighbourMonitor.registerCallback(this, false)
|
||||||
}
|
}
|
||||||
|
override fun onStop(owner: LifecycleOwner) = IpNeighbourMonitor.unregisterCallback(this)
|
||||||
|
|
||||||
override fun onCleared() {
|
override fun onCleared() = app.unregisterReceiver(receiver)
|
||||||
IpNeighbourMonitor.unregisterCallback(this)
|
|
||||||
app.unregisterReceiver(receiver)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
|
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
|
||||||
val binder = service as RepeaterService.Binder
|
val binder = service as RepeaterService.Binder
|
||||||
|
|||||||
@@ -225,7 +225,7 @@ class ClientsFragment : Fragment() {
|
|||||||
binding.swipeRefresher.setColorSchemeResources(R.color.colorSecondary)
|
binding.swipeRefresher.setColorSchemeResources(R.color.colorSecondary)
|
||||||
binding.swipeRefresher.setOnRefreshListener { IpNeighbourMonitor.instance?.flushAsync() }
|
binding.swipeRefresher.setOnRefreshListener { IpNeighbourMonitor.instance?.flushAsync() }
|
||||||
activityViewModels<ClientViewModel>().value.apply {
|
activityViewModels<ClientViewModel>().value.apply {
|
||||||
lifecycle.addObserver(this)
|
lifecycle.addObserver(fullMode)
|
||||||
clients.observe(viewLifecycleOwner) { adapter.submitList(it.toMutableList()) }
|
clients.observe(viewLifecycleOwner) { adapter.submitList(it.toMutableList()) }
|
||||||
}
|
}
|
||||||
return binding.root
|
return binding.root
|
||||||
|
|||||||
Reference in New Issue
Block a user