package eu.friedcutlet.yokan.ui.screens.together import eu.friedcutlet.yokan.services.together.ProfileTogetherSession import eu.friedcutlet.yokan.services.together.RoomClient import eu.friedcutlet.yokan.services.together.RoomConnection import eu.friedcutlet.yokan.services.together.RoomRole import eu.friedcutlet.yokan.services.together.RoomState import kotlinx.coroutines.Job internal fun roomDrives(room: RoomState?, driving: Boolean): Boolean = room?.connection == RoomConnection.Open && (room.role == RoomRole.Host || (room.role == RoomRole.CoHost && driving)) /** Captured room capability. Old clients, disposed screens and stale room snapshots fail closed. */ internal class TogetherUiBinding( private val together: ProfileTogetherSession, private val client: RoomClient?, private val job: Job, private val isUiActive: () -> Boolean = { true }, ) { fun active(): Boolean = job.isActive && isUiActive() && client != null && together.owns(client) fun current(room: RoomState?): Boolean = active() && room != null && client?.state?.value == room fun drives(): Boolean = active() && roomDrives(client?.state?.value, together.driving.value) fun followsMedia(room: RoomState?): Boolean = active() && room != null && client?.state?.value?.let { current -> current.connection == RoomConnection.Open && current.media == room.media && together.following.value && !roomDrives(current, together.driving.value) } == true fun follows(room: RoomState?): Boolean = followsMedia(room) && client?.state?.value?.position == room?.position fun close() { job.cancel() } }