Fix last seen indicator not live updating.

This commit is contained in:
darken
2022-01-05 20:25:21 +01:00
parent 6240a02c6c
commit caeefb626d
6 changed files with 33 additions and 10 deletions
@@ -31,6 +31,7 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import java.time.Instant
import java.util.* import java.util.*
import javax.inject.Inject import javax.inject.Inject
@@ -95,7 +96,7 @@ class OverviewFragmentVM @Inject constructor(
requiredPermissions, requiredPermissions,
pods, pods,
debugSettings.isDebugModeEnabled.flow, debugSettings.isDebugModeEnabled.flow,
) { _, permissions, pods, isDebugMode -> ) { tick, permissions, pods, isDebugMode ->
val items = mutableListOf<OverviewAdapter.Item>() val items = mutableListOf<OverviewAdapter.Item>()
permissions permissions
@@ -107,13 +108,29 @@ class OverviewFragmentVM @Inject constructor(
} }
.forEach { items.add(it) } .forEach { items.add(it) }
val now = Instant.now()
pods pods
.map { .map {
when (it) { when (it) {
is DualApplePods -> DualApplePodsCardVH.Item(it, showDebug = isDebugMode) is DualApplePods -> DualApplePodsCardVH.Item(
is SingleApplePods -> SingleApplePodsCardVH.Item(it, showDebug = isDebugMode) now = now,
is BasicSingleApplePods -> BasicSingleApplePodsCardVH.Item(it, showDebug = isDebugMode) device = it,
else -> UnknownPodDeviceCardVH.Item(it) showDebug = isDebugMode
)
is SingleApplePods -> SingleApplePodsCardVH.Item(
now = now,
device = it,
showDebug = isDebugMode
)
is BasicSingleApplePods -> BasicSingleApplePodsCardVH.Item(
now = now,
device = it,
showDebug = isDebugMode
)
else -> UnknownPodDeviceCardVH.Item(
now = now,
device = it
)
} }
} }
.run { items.addAll(this) } .run { items.addAll(this) }
@@ -27,7 +27,7 @@ class BasicSingleApplePodsCardVH(parent: ViewGroup) :
name.text = device.getLabel(context) name.text = device.getLabel(context)
deviceIcon.setImageResource(device.iconRes) deviceIcon.setImageResource(device.iconRes)
val duration = Duration.between(device.lastSeenAt, Instant.now()) val duration = Duration.between(device.lastSeenAt, item.now)
lastSeen.text = lastSeenFormatter.format( lastSeen.text = lastSeenFormatter.format(
duration.seconds.toDouble(), duration.seconds.toDouble(),
RelativeDateTimeFormatter.Direction.LAST, RelativeDateTimeFormatter.Direction.LAST,
@@ -54,6 +54,7 @@ class BasicSingleApplePodsCardVH(parent: ViewGroup) :
} }
data class Item( data class Item(
override val now: Instant,
override val device: BasicSingleApplePods, override val device: BasicSingleApplePods,
override val showDebug: Boolean, override val showDebug: Boolean,
) : PodDeviceVH.Item ) : PodDeviceVH.Item
@@ -27,7 +27,6 @@ class DualApplePodsCardVH(parent: ViewGroup) :
override val onBindData = binding(payload = true) { item: Item -> override val onBindData = binding(payload = true) { item: Item ->
val device = item.device val device = item.device
name.apply { name.apply {
val sb = StringBuilder(device.getLabel(context)) val sb = StringBuilder(device.getLabel(context))
if (device.deviceColor != DeviceColor.UNKNOWN) { if (device.deviceColor != DeviceColor.UNKNOWN) {
@@ -37,7 +36,7 @@ class DualApplePodsCardVH(parent: ViewGroup) :
} }
deviceIcon.setImageResource(device.iconRes) deviceIcon.setImageResource(device.iconRes)
val duration = Duration.between(device.lastSeenAt, Instant.now()) val duration = Duration.between(device.lastSeenAt, item.now)
lastSeen.text = lastSeenFormatter.format( lastSeen.text = lastSeenFormatter.format(
duration.seconds.toDouble(), duration.seconds.toDouble(),
RelativeDateTimeFormatter.Direction.LAST, RelativeDateTimeFormatter.Direction.LAST,
@@ -98,6 +97,7 @@ class DualApplePodsCardVH(parent: ViewGroup) :
} }
data class Item( data class Item(
override val now: Instant,
override val device: DualApplePods, override val device: DualApplePods,
override val showDebug: Boolean, override val showDebug: Boolean,
) : PodDeviceVH.Item ) : PodDeviceVH.Item
@@ -8,6 +8,7 @@ import eu.darken.capod.common.lists.differ.DifferItem
import eu.darken.capod.common.lists.modular.ModularAdapter import eu.darken.capod.common.lists.modular.ModularAdapter
import eu.darken.capod.main.ui.overview.OverviewAdapter import eu.darken.capod.main.ui.overview.OverviewAdapter
import eu.darken.capod.pods.core.PodDevice import eu.darken.capod.pods.core.PodDevice
import java.time.Instant
abstract class PodDeviceVH<D : PodDeviceVH.Item, B : ViewBinding>( abstract class PodDeviceVH<D : PodDeviceVH.Item, B : ViewBinding>(
@LayoutRes layoutId: Int, @LayoutRes layoutId: Int,
@@ -16,6 +17,8 @@ abstract class PodDeviceVH<D : PodDeviceVH.Item, B : ViewBinding>(
interface Item : OverviewAdapter.Item { interface Item : OverviewAdapter.Item {
val now: Instant
val device: PodDevice val device: PodDevice
val showDebug: Boolean val showDebug: Boolean
@@ -27,7 +27,7 @@ class SingleApplePodsCardVH(parent: ViewGroup) :
name.text = device.getLabel(context) name.text = device.getLabel(context)
deviceIcon.setImageResource(device.iconRes) deviceIcon.setImageResource(device.iconRes)
val duration = Duration.between(device.lastSeenAt, Instant.now()) val duration = Duration.between(device.lastSeenAt, item.now)
lastSeen.text = lastSeenFormatter.format( lastSeen.text = lastSeenFormatter.format(
duration.seconds.toDouble(), duration.seconds.toDouble(),
RelativeDateTimeFormatter.Direction.LAST, RelativeDateTimeFormatter.Direction.LAST,
@@ -59,6 +59,7 @@ class SingleApplePodsCardVH(parent: ViewGroup) :
} }
data class Item( data class Item(
override val now: Instant,
override val device: SingleApplePods, override val device: SingleApplePods,
override val showDebug: Boolean, override val showDebug: Boolean,
) : PodDeviceVH.Item ) : PodDeviceVH.Item
@@ -25,7 +25,7 @@ class UnknownPodDeviceCardVH(parent: ViewGroup) :
val device = item.device val device = item.device
name.text = device.getLabel(context) name.text = device.getLabel(context)
val duration = Duration.between(device.lastSeenAt, Instant.now()) val duration = Duration.between(device.lastSeenAt, item.now)
lastSeen.text = lastSeenFormatter.format( lastSeen.text = lastSeenFormatter.format(
duration.seconds.toDouble(), duration.seconds.toDouble(),
RelativeDateTimeFormatter.Direction.LAST, RelativeDateTimeFormatter.Direction.LAST,
@@ -36,6 +36,7 @@ class UnknownPodDeviceCardVH(parent: ViewGroup) :
} }
data class Item( data class Item(
override val now: Instant,
override val device: PodDevice, override val device: PodDevice,
override val showDebug: Boolean = false override val showDebug: Boolean = false
) : PodDeviceVH.Item ) : PodDeviceVH.Item