diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/client/Client.kt b/mobile/src/main/java/be/mygod/vpnhotspot/client/Client.kt index 2631e70f..9c905a21 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/client/Client.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/client/Client.kt @@ -27,7 +27,7 @@ open class Client(val mac: Long, val iface: String) { val ip = TreeMap(InetAddressComparator) val macString by lazy { mac.macToString() } - private val record = AppDatabase.instance.clientRecordDao.lookupSync(mac) + private val record = AppDatabase.instance.clientRecordDao.lookupOrDefaultSync(mac) private val macIface get() = SpannableStringBuilder(makeMacSpan(macString)).apply { append('%') append(iface) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt index 51ccf72c..73979e1b 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt @@ -200,9 +200,9 @@ class Routing(private val caller: Any, private val downstream: String) : IpNeigh override fun onIpNeighbourAvailable(neighbours: List) = synchronized(this) { val toRemove = HashSet(clients.keys) for (neighbour in neighbours) { - if (neighbour.dev != downstream || neighbour.ip !is Inet4Address || - runBlocking { AppDatabase.instance.clientRecordDao.lookup(neighbour.lladdr) } - ?.blocked == true) continue + if (neighbour.dev != downstream || neighbour.ip !is Inet4Address || runBlocking { + AppDatabase.instance.clientRecordDao.lookupOrDefault(neighbour.lladdr) + }.blocked) continue toRemove.remove(neighbour.ip) try { clients.computeIfAbsentCompat(neighbour.ip) { Client(neighbour.ip, neighbour.lladdr) } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/room/ClientRecord.kt b/mobile/src/main/java/be/mygod/vpnhotspot/room/ClientRecord.kt index 786e6dc6..4504eca1 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/room/ClientRecord.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/room/ClientRecord.kt @@ -1,6 +1,7 @@ package be.mygod.vpnhotspot.room import androidx.lifecycle.LiveData +import androidx.lifecycle.map import androidx.room.* @Entity @@ -12,12 +13,12 @@ data class ClientRecord(@PrimaryKey @androidx.room.Dao abstract class Dao { @Query("SELECT * FROM `ClientRecord` WHERE `mac` = :mac") - abstract suspend fun lookup(mac: Long): ClientRecord? - + protected abstract suspend fun lookup(mac: Long): ClientRecord? suspend fun lookupOrDefault(mac: Long) = lookup(mac) ?: ClientRecord(mac) @Query("SELECT * FROM `ClientRecord` WHERE `mac` = :mac") - abstract fun lookupSync(mac: Long): LiveData + protected abstract fun lookupSync(mac: Long): LiveData + fun lookupOrDefaultSync(mac: Long) = lookupSync(mac).map { it ?: ClientRecord(mac) } @Insert(onConflict = OnConflictStrategy.REPLACE) protected abstract suspend fun updateInternal(value: ClientRecord): Long