From 69edebdaa945d59bb10e3c6fa0ee21c8d48fd695 Mon Sep 17 00:00:00 2001 From: Mygod Date: Sat, 20 Jul 2019 18:23:07 +0800 Subject: [PATCH] Refine client counting --- .../be/mygod/vpnhotspot/LocalOnlyHotspotService.kt | 4 +++- .../src/main/java/be/mygod/vpnhotspot/MainActivity.kt | 11 ++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt index 7761059c..b5ea1898 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt @@ -134,7 +134,9 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope { override fun onIpNeighbourAvailable(neighbours: List) { super.onIpNeighbourAvailable(neighbours) - if (Build.VERSION.SDK_INT >= 28) timeoutMonitor?.onClientsChanged(neighbours.isEmpty()) + if (Build.VERSION.SDK_INT >= 28) timeoutMonitor?.onClientsChanged(neighbours.none { + it.state != IpNeighbour.State.FAILED + }) } override fun onDestroy() { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt b/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt index 28466802..7a23b692 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt @@ -14,10 +14,12 @@ import be.mygod.vpnhotspot.client.ClientViewModel import be.mygod.vpnhotspot.client.ClientsFragment import be.mygod.vpnhotspot.databinding.ActivityMainBinding import be.mygod.vpnhotspot.manage.TetheringFragment +import be.mygod.vpnhotspot.net.IpNeighbour import be.mygod.vpnhotspot.net.wifi.WifiDoubleLock import be.mygod.vpnhotspot.util.ServiceForegroundConnector import be.mygod.vpnhotspot.widget.SmartSnackbar import com.google.android.material.bottomnavigation.BottomNavigationView +import java.net.Inet4Address class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener { private lateinit var binding: ActivityMainBinding @@ -30,11 +32,14 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS if (savedInstanceState == null) displayFragment(TetheringFragment()) val model = ViewModelProviders.of(this).get() if (RepeaterService.supported) ServiceForegroundConnector(this, model, RepeaterService::class) - model.clients.observe(this) { - if (it.isNotEmpty()) binding.navigation.getOrCreateBadge(R.id.navigation_clients).apply { + model.clients.observe(this) { clients -> + val count = clients.count { + it.ip.any { (ip, state) -> ip is Inet4Address && state != IpNeighbour.State.FAILED } + } + if (count > 0) binding.navigation.getOrCreateBadge(R.id.navigation_clients).apply { backgroundColor = ContextCompat.getColor(this@MainActivity, R.color.colorSecondary) badgeTextColor = ContextCompat.getColor(this@MainActivity, R.color.primary_text_default_material_light) - number = it.size + number = count } else binding.navigation.removeBadge(R.id.navigation_clients) } SmartSnackbar.Register(lifecycle, binding.fragmentHolder)