diff --git a/domain/src/main/kotlin/com/flagship/abox/manager/domain/infrared/LearnVirtualRemoteKey.kt b/domain/src/main/kotlin/com/flagship/abox/manager/domain/infrared/LearnVirtualRemoteKey.kt index 0764236..fb453a3 100644 --- a/domain/src/main/kotlin/com/flagship/abox/manager/domain/infrared/LearnVirtualRemoteKey.kt +++ b/domain/src/main/kotlin/com/flagship/abox/manager/domain/infrared/LearnVirtualRemoteKey.kt @@ -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 diff --git a/domain/src/main/kotlin/com/flagship/abox/manager/domain/infrared/SendVirtualRemoteAction.kt b/domain/src/main/kotlin/com/flagship/abox/manager/domain/infrared/SendVirtualRemoteAction.kt index 35bb9ec..c18b2f3 100644 --- a/domain/src/main/kotlin/com/flagship/abox/manager/domain/infrared/SendVirtualRemoteAction.kt +++ b/domain/src/main/kotlin/com/flagship/abox/manager/domain/infrared/SendVirtualRemoteAction.kt @@ -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")) - // 1. Download to scratch slot - val scratchSlot = remote.dynamicScratchSlot - when (val dlResult = gateway.downloadCodes(profileId, listOf(InfraredCode(remote.deviceName, scratchSlot, hexCode)))) { - is DomainResult.Failure -> return dlResult - is DomainResult.Success -> Unit + if (binding.rawHexCode != null) { + // 1. Download to scratch slot + val scratchSlot = remote.dynamicScratchSlot + 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")) } - // 2. Send scratch slot - gateway.sendKey(profileId, remote.deviceName, scratchSlot) } } }