Fix nullability issues

This commit is contained in:
Mygod
2019-05-13 18:41:58 +08:00
parent c8c57d9c95
commit 965bd13eac
3 changed files with 8 additions and 7 deletions

View File

@@ -27,7 +27,7 @@ open class Client(val mac: Long, val iface: String) {
val ip = TreeMap<InetAddress, IpNeighbour.State>(InetAddressComparator) val ip = TreeMap<InetAddress, IpNeighbour.State>(InetAddressComparator)
val macString by lazy { mac.macToString() } 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 { private val macIface get() = SpannableStringBuilder(makeMacSpan(macString)).apply {
append('%') append('%')
append(iface) append(iface)

View File

@@ -200,9 +200,9 @@ class Routing(private val caller: Any, private val downstream: String) : IpNeigh
override fun onIpNeighbourAvailable(neighbours: List<IpNeighbour>) = synchronized(this) { override fun onIpNeighbourAvailable(neighbours: List<IpNeighbour>) = synchronized(this) {
val toRemove = HashSet(clients.keys) val toRemove = HashSet(clients.keys)
for (neighbour in neighbours) { for (neighbour in neighbours) {
if (neighbour.dev != downstream || neighbour.ip !is Inet4Address || if (neighbour.dev != downstream || neighbour.ip !is Inet4Address || runBlocking {
runBlocking { AppDatabase.instance.clientRecordDao.lookup(neighbour.lladdr) } AppDatabase.instance.clientRecordDao.lookupOrDefault(neighbour.lladdr)
?.blocked == true) continue }.blocked) continue
toRemove.remove(neighbour.ip) toRemove.remove(neighbour.ip)
try { try {
clients.computeIfAbsentCompat(neighbour.ip) { Client(neighbour.ip, neighbour.lladdr) } clients.computeIfAbsentCompat(neighbour.ip) { Client(neighbour.ip, neighbour.lladdr) }

View File

@@ -1,6 +1,7 @@
package be.mygod.vpnhotspot.room package be.mygod.vpnhotspot.room
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.map
import androidx.room.* import androidx.room.*
@Entity @Entity
@@ -12,12 +13,12 @@ data class ClientRecord(@PrimaryKey
@androidx.room.Dao @androidx.room.Dao
abstract class Dao { abstract class Dao {
@Query("SELECT * FROM `ClientRecord` WHERE `mac` = :mac") @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) suspend fun lookupOrDefault(mac: Long) = lookup(mac) ?: ClientRecord(mac)
@Query("SELECT * FROM `ClientRecord` WHERE `mac` = :mac") @Query("SELECT * FROM `ClientRecord` WHERE `mac` = :mac")
abstract fun lookupSync(mac: Long): LiveData<ClientRecord> protected abstract fun lookupSync(mac: Long): LiveData<ClientRecord?>
fun lookupOrDefaultSync(mac: Long) = lookupSync(mac).map { it ?: ClientRecord(mac) }
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
protected abstract suspend fun updateInternal(value: ClientRecord): Long protected abstract suspend fun updateInternal(value: ClientRecord): Long