moveFocus method

void moveFocus(
  1. String direction
)

Implementation

void moveFocus(String direction) {
  final active = getActiveMedia();
  if (active.isEmpty) return;

  Media? currentMedia;
  if (_currentFocusNodeId != null) {
    currentMedia =
        active.where((m) => m.id == _currentFocusNodeId).firstOrNull;
  }
  if (currentMedia == null) {
    final focusable =
        active.where((m) => getFocusIndexForMedia(m) != null).toList();
    if (focusable.isNotEmpty) {
      focusable.sort((a, b) {
        final fa = int.tryParse(getFocusIndexForMedia(a)!) ?? 9999;
        final fb = int.tryParse(getFocusIndexForMedia(b)!) ?? 9999;
        return fa.compareTo(fb);
      });
      setFocus(focusable.first.id ?? '');
    }
    return;
  }

  final desc = getDescriptorForMedia(currentMedia);
  if (desc == null) return;

  String? targetIndex;
  final dir = direction.toUpperCase();
  if (dir == 'RIGHT' || dir == 'CURSOR_RIGHT') {
    targetIndex = desc.moveRight;
  } else if (dir == 'LEFT' || dir == 'CURSOR_LEFT') {
    targetIndex = desc.moveLeft;
  } else if (dir == 'UP' || dir == 'CURSOR_UP') {
    targetIndex = desc.moveUp;
  } else if (dir == 'DOWN' || dir == 'CURSOR_DOWN') {
    targetIndex = desc.moveDown;
  }

  if (targetIndex != null && targetIndex.isNotEmpty) {
    final targetMedia = getActiveMediaByFocusIndex(targetIndex);
    if (targetMedia != null && targetMedia.id != null) {
      setFocus(targetMedia.id!);
    }
  }
}