loadH5P method

Future<void> loadH5P(
  1. String url
)

Implementation

Future<void> loadH5P(String url) async {
  if (isLoading.value) return;
  isLoading.value = true;
  status.value = H5PLoadStatus.downloading;
  downloadProgress.value = 0;

  try {
    await closeServer();

    debugPrint("⬇️ Downloading H5P from $url ...");
    await _h5pSetup.downloadAndExtract(
      url,
      onProgress: (p) => downloadProgress.value = p,
    );

    status.value = H5PLoadStatus.extracting;
    final dir = await _h5pSetup.copyBaseFiles();

    final server = await startLocalServer(dir);
    _currentServer = server;
    status.value = H5PLoadStatus.ready;

    final newUrl =
        "http://${server.address.address}:${server.port}?t=${DateTime.now().millisecondsSinceEpoch}";
    localServerUrl.value = newUrl;
    downloadProgress.value = 1.0;

    debugPrint("🌐 Local server running at: $newUrl");
  } on DioException catch (e) {
    status.value = H5PLoadStatus.error;
    String message = 'Network error: ${e.message}';
    if (e.error is SocketException) {
      message = 'No internet connection or host not found.';
    }
    debugPrint("❌ $message");
  } catch (e) {
    status.value = H5PLoadStatus.error;
    debugPrint('❌ Error loading H5P: $e');
  } finally {
    isLoading.value = false;
  }
}