Migrate to LiveData

Benefit includes: no more flush after changing nickname. Yep.
This commit is contained in:
Mygod
2019-01-26 14:13:19 +08:00
parent e1e44f468a
commit 94114f7a4b
12 changed files with 68 additions and 63 deletions

View File

@@ -3,6 +3,7 @@ package be.mygod.vpnhotspot.client
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.StrikethroughSpan
import androidx.lifecycle.Transformations
import androidx.recyclerview.widget.DiffUtil
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.R
@@ -10,42 +11,42 @@ import be.mygod.vpnhotspot.net.InetAddressComparator
import be.mygod.vpnhotspot.net.IpNeighbour
import be.mygod.vpnhotspot.net.TetherType
import be.mygod.vpnhotspot.room.AppDatabase
import be.mygod.vpnhotspot.room.lookup
import be.mygod.vpnhotspot.room.macToLong
import be.mygod.vpnhotspot.util.onEmpty
import java.net.InetAddress
import java.util.*
abstract class Client {
open class Client(val mac: String, val iface: String) {
companion object DiffCallback : DiffUtil.ItemCallback<Client>() {
override fun areItemsTheSame(oldItem: Client, newItem: Client) =
oldItem.iface == newItem.iface && oldItem.mac == newItem.mac
override fun areContentsTheSame(oldItem: Client, newItem: Client) = oldItem == newItem
}
abstract val iface: String
abstract val mac: String
private val macIface get() = "$mac%$iface"
val ip = TreeMap<InetAddress, IpNeighbour.State>(InetAddressComparator)
val record by lazy { AppDatabase.instance.clientRecordDao.lookup(mac.macToLong()) }
val record = AppDatabase.instance.clientRecordDao.lookupSync(mac.macToLong())
val nickname get() = record.value?.nickname ?: ""
val blocked get() = record.value?.blocked == true
open val icon get() = TetherType.ofInterface(iface).icon
val title: CharSequence get() {
val result = SpannableStringBuilder(record.nickname.onEmpty(macIface))
if (record.blocked) result.setSpan(StrikethroughSpan(), 0, result.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
return result
}
val description: String get() {
val result = StringBuilder(if (record.nickname.isEmpty()) "" else "$macIface\n")
ip.entries.forEach { (ip, state) ->
result.appendln(app.getString(when (state) {
IpNeighbour.State.INCOMPLETE -> R.string.connected_state_incomplete
IpNeighbour.State.VALID -> R.string.connected_state_valid
IpNeighbour.State.FAILED -> R.string.connected_state_failed
else -> throw IllegalStateException("Invalid IpNeighbour.State: $state")
}, ip.hostAddress))
val title = Transformations.map(record) { record ->
SpannableStringBuilder(record.nickname.onEmpty(macIface)).apply {
if (record.blocked) setSpan(StrikethroughSpan(), 0, length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
}
return result.toString().trimEnd()
}
val description = Transformations.map(record) { record ->
StringBuilder(if (record.nickname.isEmpty()) "" else "$macIface\n").apply {
ip.entries.forEach { (ip, state) ->
appendln(app.getString(when (state) {
IpNeighbour.State.INCOMPLETE -> R.string.connected_state_incomplete
IpNeighbour.State.VALID -> R.string.connected_state_valid
IpNeighbour.State.FAILED -> R.string.connected_state_failed
else -> throw IllegalStateException("Invalid IpNeighbour.State: $state")
}, ip.hostAddress))
}
}.toString().trimEnd()
}
override fun equals(other: Any?): Boolean {
@@ -57,9 +58,9 @@ abstract class Client {
if (iface != other.iface) return false
if (mac != other.mac) return false
if (ip != other.ip) return false
if (record != other.record) return false
if (record.value != other.record.value) return false
return true
}
override fun hashCode() = Objects.hash(iface, mac, ip, record)
override fun hashCode() = Objects.hash(iface, mac, ip, record.value)
}