createAppFolder function

Future<SolidFunctionCallStatus> createAppFolder({
  1. required String folderName,
  2. required BuildContext context,
  3. bool createInitFile = true,
  4. required void onProgressChange(
    1. bool
    ),
  5. required void onSuccess(),
})

Creates an app folder in POD with initialisation file.

Returns a Future<SolidFunctionCallStatus> indicating the creation result. The folderName parameter specifies which folder to create (e.g. 'movies'). If createInitFile is true, creates an initialisation file in the folder.

Implementation

Future<SolidFunctionCallStatus> createAppFolder({
  required String folderName,
  required BuildContext context,
  bool createInitFile = true,
  required void Function(bool) onProgressChange,
  required void Function() onSuccess,
}) async {
  try {
    onProgressChange.call(true);

    // Check current resources.

    final dirUrl = await getDirUrl(basePath);
    final resources = await getResourcesInContainer(dirUrl);

    // Check if exists as directory.

    bool existsAsDir = resources.subDirs.contains(folderName);
    if (existsAsDir) {
      debugPrint('App folder $folderName already exists as directory');
      onSuccess.call();
      return SolidFunctionCallStatus.success;
    }

    // Check if exists as file and delete if necessary.

    bool existsAsFile = resources.files.contains(folderName);
    if (existsAsFile) {
      debugPrint(
        'Removing existing file $folderName before creating directory',
      );
      if (!context.mounted) return SolidFunctionCallStatus.fail;

      // Full path for deletion needs to include basePath (e.g. moviestar/data).

      await deleteFile('$basePath/$folderName');
    }

    if (!context.mounted) {
      debugPrint('Widget is no longer mounted, skipping folder creation.');
      return SolidFunctionCallStatus.fail;
    }

    // Create the app folder structure.

    final result = await writePod(
      '$folderName/.init',
      '',
      context,
      const Text('Creating folder'),
      encrypted: false,
    );

    // If folder creation was successful and initialization file is requested.

    if (result == SolidFunctionCallStatus.success && createInitFile) {
      String initContent;

      // Initialisation content for all folders in Turtle format.

      initContent = '''
@prefix : <#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

:folder "$folderName" ;
        :created "${DateTime.now().toIso8601String()}"^^xsd:dateTime ;
        :version "1.0" .
''';

      if (!context.mounted) return result;

      final initResult = await writePod(
        '$folderName/init.ttl',
        initContent,
        context,
        const Text('Creating initialization file'),
        encrypted: true,
      );

      if (initResult == SolidFunctionCallStatus.success) {
        onSuccess.call();
        return SolidFunctionCallStatus.success;
      }
    }

    return result;
  } catch (e) {
    debugPrint('Error creating app folder: $e');
    return SolidFunctionCallStatus.fail;
  } finally {
    onProgressChange.call(false);
  }
}