fix(infrared): allocate dedicated static slot for physically learned keys

This commit is contained in:
2026-08-25 14:22:47 +08:00
parent 63cac722c2
commit 849b7e4aea
2 changed files with 19 additions and 17 deletions
@@ -25,14 +25,11 @@ class LearnVirtualRemoteKey(
val binding = bindings.find { it.buttonId == buttonId }
?: return DomainResult.Failure(DomainError.InvalidRequest(detail = "Button not found"))
val targetSlot = when (remote.transmissionMode) {
TransmissionMode.STATIC_SLOTS -> {
binding.assignedSlot ?: run {
// Find available slot from quota
"1"
}
}
TransmissionMode.DYNAMIC_SCRATCH -> remote.dynamicScratchSlot
val targetSlot = binding.assignedSlot ?: run {
val allBindings = (repository.getKeyBindings(remoteId) as? DomainResult.Success)?.value.orEmpty()
val usedSlots = allBindings.mapNotNull { it.assignedSlot?.toIntOrNull() }.toSet()
val nextSlot = (1..99).firstOrNull { it !in usedSlots } ?: 1
nextSlot.toString()
}
// Set status to LEARNING while hardware listens
@@ -37,16 +37,21 @@ class SendVirtualRemoteAction(
gateway.sendKey(profileId, remote.deviceName, slot)
}
TransmissionMode.DYNAMIC_SCRATCH -> {
val hexCode = binding.rawHexCode
?: return DomainResult.Failure(DomainError.InvalidRequest(detail = "No hex code available for dynamic transmission"))
if (binding.rawHexCode != null) {
// 1. Download to scratch slot
val scratchSlot = remote.dynamicScratchSlot
when (val dlResult = gateway.downloadCodes(profileId, listOf(InfraredCode(remote.deviceName, scratchSlot, hexCode)))) {
when (val dlResult = gateway.downloadCodes(profileId, listOf(InfraredCode(remote.deviceName, scratchSlot, binding.rawHexCode)))) {
is DomainResult.Failure -> return dlResult
is DomainResult.Success -> Unit
}
// 2. Send scratch slot
gateway.sendKey(profileId, remote.deviceName, scratchSlot)
} else if (binding.assignedSlot != null) {
// Fallback to sending learned hardware slot
gateway.sendKey(profileId, remote.deviceName, binding.assignedSlot)
} else {
DomainResult.Failure(DomainError.InvalidRequest(detail = "No hex code or slot available for transmission"))
}
}
}
}